Quantcast
Channel: Windows Forms Data Controls and Databinding forum
Viewing all articles
Browse latest Browse all 2535

Saving Datagridview Data To Database

$
0
0

Hi,

am using odbc to connect to my database and on my form i input data which goes to a datagrid. after which i intend to save the entire collection to my database . the problem is, only the first row is saved to the database. I do not want to use the databinder since on my grid i have a column with a combobox that is loaded with data from another table. What am i missing here. Please help.

The code:

Try

                cn = New OdbcConnection(connector())
                CM = New OdbcCommand

                CM.CommandText = "Insert into mytesttable(strno,strdate,scustid,spcode,spgroup,spqty,spcost) Values( ?,?,?,?,?,?,? )"

                CM.Connection = cn
                cn.Open()

                For i As Integer = 0 To DataGridView1.Rows.Count - 1
                    With CM.Parameters
                        .Add("@strno", OdbcType.VarChar).Value = Trim(TextBox2.Text)
                        .Add("@stdate", OdbcType.DateTime).Value = Trim(DateTimePicker1.Value)
                        .Add("@scustid", OdbcType.VarChar).Value = "jxx"

                        .Add("@spcode", OdbcType.VarChar).Value = DataGridView1.Rows(i).Cells(1).Value
                        .Add("@spgroup", OdbcType.VarChar).Value = DataGridView1.Rows(i).Cells(3).Value
                        .Add("@spqty", OdbcType.Int).Value = DataGridView1.Rows(i).Cells(5).Value
                        .Add("@spcost", OdbcType.Double).Value = DataGridView1.Rows(i).Cells(7).Value
                    End With
                Next

                CM.ExecuteNonQuery()
                CM.Parameters.Clear()

                MsgBox("Data Has Been Saved", MsgBoxStyle.Information)

                cn.Close()


            Catch ex As Exception
                MsgBox(ex.Message, MsgBoxStyle.Exclamation)
  End Try


Viewing all articles
Browse latest Browse all 2535

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>