Hello Frens,
I tried to update change made in database shown DataGridView1 of Visual Studio 2008 with the following codes. But an error message comes as "Object reference not set to an instance of an object".

Public Class Form1
Dim cmd As Odbc.OdbcCommand
Dim adp As Odbc.OdbcDataAdapter

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

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
End
End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

'Update records using a Command Builder
' The variable i gives the number of records updated
Dim cmdbuilder As New Odbc.OdbcCommandBuilder(adp)
Dim i As Integer
Try
i = adp.Update(Db1DataSet, "Table1")
MsgBox("Records Updated= " & i)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
End Class

Plz help me

Recommended Answers

All 2 Replies

Using CommandBuilder it should be:

Dim adp As New Odbc.OdbcDataAdapter("SELECT * FROM tablename WHERE PrimaryKey = '" & Texbox1.Tex &"'", yourconnection)
Dim ds As New DataSet
Dim cmdbuilder As New Odbc.OdbcCommandBuilder(adp)
adp.Fill(ds)

ds.Tables(0).Rows(0).Item("Field1") = Me.Textbox2.Text
'do the rest

adp.Update(ds)

Thanks,

May i know your name. I am Nasir Ahmed. I impressed with your quote "Remember your creator before you die" than the code. I rather request to remember Our Creator all the time.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.