Jump to content
Xtreme .Net Talk

Recommended Posts

Posted

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

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...