Problem with parameters

ivo75

New member
Joined
Jan 25, 2009
Messages
2
Programming Experience
Beginner
I want to export to XML file and have problem this is the code but is not work, error is "No value given for one or more required parameters."

VB.NET:
Private Sub GetDataFromAcessFile1()
        Dim aCon As OleDbConnection
        Dim aCmd As OleDbCommand
        Dim strCon As String
        Dim param1 As OleDbParameter
        Dim param2 As OleDbParameter
        Dim param3 As OleDbParameter

        'create connection with mdb file
        strCon = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strAcessFilePath & ";User ID=Admin;Password="
        aCon = New OleDbConnection(strCon)
        aCon.Open()

        strQuery = "Select * from rabotnici where rabotnik_name=? and data_rabota>=? and data_rabota<=?"
        param1 = New OleDbParameter("@rabotnik_name", OleDbType.VarWChar, 100)
        param1.Value = ComboBox1.Text
        param2 = New OleDbParameter("@data_rabota", OleDbType.Date)
        param2.Value = DateTimePicker1.Value.ToShortDateString
        param3 = New OleDbParameter("@data_rabota1", OleDbType.Date)
        param3.Value = DateTimePicker2.Value.ToShortDateString()
        aCmd = New OleDbCommand(strQuery, aCon)
        aCmd.Parameters.Add(param1)
        aCmd.Parameters.Add(param2)
        aCmd.Parameters.Add(param3)
        aCmd.ExecuteNonQuery()

        aCmd = New OleDbCommand(strQuery, aCon)
        Dim da As New OleDbDataAdapter
        da.SelectCommand = aCmd
        Dim ds As DataSet
        ds = New DataSet("Customers1")
        da.Fill(ds, "New Customers1")
        Dim dt As DataTable
        dt = New DataTable()
        dt = ds.Tables("New Customers1")
        CreateXMLFile()
        WriteToXml(dt)
        aCon.Close()
    End Sub
Can You help me
 
aCmd.ExecuteNonQuery()

aCmd = New OleDbCommand(strQuery, aCon)
Why do you call ExecuteNonQuery with a Select query with no out parameters?

The new command you create uses same parameterized query, but you have not added any OleDbParameters to it.

Removing the two lines quoted above will probably solve it.
dt = New DataTable()
dt = ds.Tables("New Customers1")
Why do you create a new table here, only to discard it?
DateTimePicker2.Value.ToShortDateString()
Why do you convert Date values to string? Access has proper date/time fields used to store Date values.
 
this is the all of the code I hope You understand me

P
VB.NET:
ublic Class Form4
    Dim strQuery As String
    Dim strAcessFilePath As String
    Dim strXmlFilePath As String
    Private Sub CrystalReportViewer1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)
        
    End Sub

    Private Sub GetDataFromAcessFile1()
        Dim aCon As OleDbConnection
        Dim aCmd As OleDbCommand
        Dim strCon As String
        Dim param1 As OleDbParameter
        Dim param2 As OleDbParameter
        Dim param3 As OleDbParameter

        'create connection with mdb file
        strCon = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strAcessFilePath & ";User ID=Admin;Password="
        aCon = New OleDbConnection(strCon)
        aCon.Open()

        strQuery = "Select * from rabotnici where rabotnik_name=@rabotnik_name and data_rabota>=@data_rabota and data_rabota<=@data_rabota1"
        param1 = New OleDbParameter("@rabotnik_name", OleDbType.VarWChar, 100)
        param1.Value = ComboBox1.Text
        param2 = New OleDbParameter("@data_rabota", OleDbType.Date)
        param2.Value = DateTimePicker1.Value.ToShortDateString
        param3 = New OleDbParameter("@data_rabota1", OleDbType.Date)
        param3.Value = DateTimePicker2.Value.ToShortDateString()
        aCmd = New OleDbCommand(strQuery, aCon)
        aCmd.Parameters.Add(param1)
        aCmd.Parameters.Add(param2)
        aCmd.Parameters.Add(param3)
        aCmd.ExecuteNonQuery()

        aCmd = New OleDbCommand(strQuery, aCon)
        Dim da As New OleDbDataAdapter
        da.SelectCommand = aCmd
        Dim ds As DataSet
        ds = New DataSet("Customers1")
        da.Fill(ds, "New Customers1")
        Dim dt As DataTable
        dt = New DataTable()
        dt = ds.Tables("New Customers1")
        CreateXMLFile()
        WriteToXml(dt)
        aCon.Close()
    End Sub
    Private Sub WriteToXml(ByVal Acessdt As DataTable)
        'Write the elements to the file
        If Dir(strXmlFilePath).Length = 0 Then
            CreateXMLFile()
        End If
        Dim XmlDoc As New XmlDocument
        Dim xNode As XmlNode
        Dim xElement As XmlElement
        Dim NewxElement As XmlElement
        XmlDoc.Load(strXmlFilePath)
        If XmlDoc.DocumentElement.HasChildNodes = False Then
            Dim inti As Integer
            Dim intCol As Integer
            'Add the node here 
            For inti = 0 To Acessdt.Rows.Count - 1
                xNode = XmlDoc.CreateNode(XmlNodeType.Element, "customer", Nothing)
                xElement = XmlDoc.DocumentElement.AppendChild(xNode)
                xNode = Nothing

                For intCol = 0 To Acessdt.Columns.Count - 1
                    xNode = XmlDoc.CreateNode(XmlNodeType.Element, Acessdt.Columns(intCol).Caption, Nothing)
                    NewxElement = xElement.AppendChild(xNode)



                    NewxElement.SetAttribute("value", Acessdt.Rows.Item(inti).Item(intCol))

                    '**Puts all the data as attributes.
                    'xNode = XmlDoc.CreateNode(XmlNodeType.Attribute, Acessdt.Columns(intCol).Caption, Nothing)
                    'newxNode = xElement.Attributes.Append(xNode)

                    '*Puts all the data as nodes along with customer.
                    'xNode = XmlDoc.CreateElement(Acessdt.Columns(intCol).Caption)
                    'xElement = XmlDoc.DocumentElement.AppendChild(xNode)

                Next intCol
            Next
        End If
        XmlDoc.Save(strXmlFilePath)

    End Sub
    Private Sub CreateXMLFile()
        'Create an XML File 
        If Dir(strXmlFilePath).Length <> 0 Then
            Kill(strXmlFilePath)
        End If
        Dim xTextWriter As New XmlTextWriter(strXmlFilePath, Nothing)
        xTextWriter.WriteStartDocument()    'this line writes the version <?xml version="1.0" ?> 
        xTextWriter.WriteComment("This file consists of Customers data")
        xTextWriter.WriteComment("This data is collected from Access Customers table.")
        xTextWriter.WriteStartElement("Customers")
        xTextWriter.WriteEndElement()

        xTextWriter.Flush()
        xTextWriter.Close()

    End Sub

    Private Sub Form4_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'TODO: This line of code loads data into the 'RabotniciDataSet.Workers1' table. You can move, or remove it, as needed.
        Me.Workers1TableAdapter.Fill(Me.RabotniciDataSet.Workers1)


    End Sub

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        strAcessFilePath = System.Windows.Forms.Application.StartupPath & "\rabotnici.mdb"
        strXmlFilePath = "C:\CustomerData.xml"
        GetDataFromAcessFile1()
        'Me.ОтчетиToolStripMenuItem.HideDropDown()
        Form5.Show()
    End Sub

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

    End Sub

    Private Sub ComboBox1_TextChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles ComboBox1.TextChanged
        If Form5.Visible = True Then
            Form5.Close()
        End If
    End Sub
End Class
 
Are you aware that DataTable has a WriteXml method that writes the schema and/or data of the datatable out to disk?

i.e. your code there, could probably be reduced to about 6 lines... :/
 
Back
Top