Click to See Complete Forum and Search --> : Datagrid Problem


dr223
May 12th, 2010, 05:08 AM
Hi,

I am trying to insert data to my sql database from a datagrid, and have the following error as highlighted, how can I change the code to be able to insert the data to the table


Private Sub Exportdata()

Dim cmd As New SqlCommand
Dim conn As SqlConnection = GetDbConnection()
Dim query As String

Dim irow As Integer

For irow = 0 To DataGrid1.Item - 1
Try
query = "INSERT INTO dbo.tbltest (Pat_eid, event_date) VALUES (" & _
DataGrid1.Item(irow).Cells(0).Value & ", '" & _
DataGrid1.Item(irow).Cells(1).Value & ")"


cmd = New SqlCommand(query, conn)
cmd.ExecuteNonQuery()

Catch ex As Exception

MsgBox(ex.Message, MsgBoxStyle.Information, "VeriSIS")

End Try

Next irow

End Sub


Thanks

HanneSThEGreaT
May 12th, 2010, 06:08 AM
What error do you get??

Also, please, please use [CODE] tags when posting code.

It is explained here :

http://www.codeguru.com/forum/showthread.php?t=403073

dr223
May 12th, 2010, 06:20 AM
The error message are;

1) Overload resolution failed because no accessible 'item' accepts this number of arguments
2) Value of type 'integer' cannot be converted to 'System.Windows.Forms.DataGridCell'


Thank you and next time I'll make sure I enclose the code.

HanneSThEGreaT
May 12th, 2010, 06:28 AM
Ah, it seems as if you have forgotten to specify the column as well, like this :

DataGrid1.Item(irow, COLUMN HERE)

dr223
May 12th, 2010, 06:33 AM
I still have an error here;

For irow = 0 To DataGrid1.Item - 1