0
Reply

How can I save unbound datagridview1 to my new table database?

bernard

bernard

Mar 30 2010 5:11 AM
2.3k
Can anyone help me please.. I create a program that make query date range on database after that it shows to
datagridview1 this work fine, but when the result I want to save to a database table I created on ms-access
I don't know how to do it.

here is my code.

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim cmd As OleDbCommand = New OleDbCommand

        With cmd
            .CommandText = "SELECT [Item Code], [Descriptions], [Reference], [Date]," & _
            "[Quantity], [Status] FROM StockTranstbl WHERE ([Date]) >= #" & _
            DateValue(DateTimePicker1.Value) & _
            "# AND ([Date]) <= #" & DateValue(DateTimePicker2.Value) & "#"
            .CommandType = CommandType.Text
            .Connection = dbConn
        End With

        'dtIn.Clear()
        adap = New OleDbDataAdapter(cmd)
        adap.Fill(dtIn)

        DataGridView1.DataSource = dtIn


        Dim dgr1 As DataGridViewRow

        For Each dgr1 In DataGridView1.Rows

 
            cmd.CommandText = "INSERT INTO StockQueryDatetbl([Item Code],[Descriptions],[Reference], [Date]," & _
            "[Quantity], [Status]) VALUES(@a,@b,@c,@d,@e,@f)"
            cmd.CommandType = CommandType.Text
            cmd.Connection = dbConn

            cmd.Parameters.Add("@a", OleDbType.VarChar).Value = dtIn.Columns.Contains("[Item Code]")
            cmd.Parameters.Add("@b", OleDbType.VarChar).Value = dtIn.Columns.Contains("[Descriptions]")
            cmd.Parameters.Add("@c", OleDbType.VarChar).Value = dtIn.Columns.Contains("[Reference]")
            cmd.Parameters.Add("@d", OleDbType.Date).Value = dtIn.Columns.Contains("[Date]")
            cmd.Parameters.Add("@e", OleDbType.Integer).Value = dtIn.Columns.Contains("[Quantity]")
            cmd.Parameters.Add("@f", OleDbType.VarChar).Value = dtIn.Columns.Contains("[Status]")


            cmd.ExecuteNonQuery()
 
        Next


        MsgBox("save sucessfully")

End Sub



Your help is highly appreciated..

Thank you.