ntgr Posted October 21, 2011 Posted October 21, 2011 hello, i am new to VB.net and I am trying to make a simple program to write to a table (test1) with two fields (ID primary key and name nchar(50)) Also I am using dataset and dataadapter I also have a form that binds the 2 fields. I have this button that binds the fields successufully Private Sub btn_bind_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_bind.Click Dim cmdupdate As New SqlClient.SqlCommand() Dim cmdselect As New SqlClient.SqlCommand() Dim prm As SqlClient.SqlParameter cnn.ConnectionString = "server=(local)\MSSMLBIZ;" & _ "initial catalog=test;" & _ "integrated security=SSPI;UID=kostas;Password=qwedsa" Try cnn.Open() cmdselect = cnn.CreateCommand cmdselect.CommandText = "select * from test1" testda.SelectCommand = cmdselect testda.Fill(testds, "customers") Me.BindingContext(testds.Tables("customers")).Position = 0 txt_id.DataBindings.Add("Text", testds.Tables("customers"), "ID") txt_name.DataBindings.Add("Text", testds.Tables("customers"), "name") Catch ex As Exception MsgBox("error: " & ex.Source & " : " & ex.Message, _ MsgBoxStyle.OkOnly, "btn_bind") End Try End Sub Then I have a button that saves changes and it doesn't work Private Sub btn_save_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_save.Click Dim cmdupdate As New SqlClient.SqlCommand() Dim prm As SqlClient.SqlParameter Try cmdupdate = cnn.CreateCommand cmdupdate.CommandText = "update test1 set name = @name WHERE ID=@ID" prm = cmdupdate.Parameters.Add("@name", SqlDbType.NChar, 50, txt_name.Text) prm = cmdupdate.Parameters.Add("@ID", SqlDbType.UniqueIdentifier, 4, txt_id.Text) testda.UpdateCommand.ExecuteNonQuery() cnn.Close() Catch ex As Exception MsgBox(ex.ToString) End Try End Sub I am getting this error when pressing the second button:A first chance exception of type 'System.NullReferenceException' occurred in book_demo.exe can you please advice thanks Quote
Administrators PlausiblyDamp Posted October 21, 2011 Administrators Posted October 21, 2011 Which line is the one throwing the exception? If you step through the code what is the value of testda on the line testda.UpdateCommand.ExecuteNonQuery() if it is non-null then what about the value of testda.UpdateCommand? Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
ntgr Posted October 21, 2011 Author Posted October 21, 2011 it seems I am missing some kind a connection of the dataset with the datatable. can you please help on this one ? Quote
Administrators PlausiblyDamp Posted October 24, 2011 Administrators Posted October 24, 2011 The DataAdapter needs to be told what command to use when inserting, updating and deleting - you would need to assign your cmdupdate to the adapter's UpdateCommand property/ Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.