UPDATING FIELDS

'HERE I AM TRYING TO UPDATE CERTAIN FIELDS.HOW CAN I CHECK IT IS UPDATED OR NOT.WHAT IS THE ERROR IN MY CODING

cmd = New SqlCommand("update tb_customer set customername='" & txtCustName.Text & "' , customerlocation='" & txtlocation.Text & "' , customeraddress='" & txtaddress.Text & "' where CustomerId='" & txtCustomerId.Text & "' ", con)
cmd.ExecuteNonQuery()
check = cmd.ExecuteNonQuery
If check > 0 Then
Dim adap As SqlDataAdapter
adap = New SqlDataAdapter("select * from tb_customer ", con)
Dim ds As New DataSet
adap.Fill(ds, "tb_customer")
DataGridView1.DataSource = TestDataSet19BindingSource
TestDataSet19BindingSource.DataSource = ds
DataGridView1.Refresh()
MessageBox.Show("Successfully updated")
txtaddress.Text = ""
txtCustName.Text = ""
txtlocation.Text = ""
Else
MessageBox.Show("Nothing Updated")
End If

Comments

  • The code below is what I edited yours to be. Ignore the comments, as they were to help me walk through the code. I would try an exception catch...see below:

    [code]
    Try

    cmd = New SqlCommand("update tb_customer set customername='" & txtCustName.Text & "' , customerlocation='" & txtlocation.Text & "' , customeraddress='" & txtaddress.Text & "' where CustomerId='" & txtCustomerId.Text & "' ", con)

    'Execute The Command on update cmd
    cmd.ExecuteNonQuery()

    'This is redundant --- Could simply change the previous line to check = cmd.ExecuenonQuery()
    check = cmd.ExecuteNonQuery

    If check > 0 Then
    Dim adap As SqlDataAdapter
    adap = New SqlDataAdapter("select * from tb_customer ", con)
    Dim ds As New DataSet
    adap.Fill(ds, "tb_customer")
    DataGridView1.DataSource = TestDataSet19BindingSource
    TestDataSet19BindingSource.DataSource = ds
    DataGridView1.Refresh()
    MessageBox.Show("Successfully updated")
    txtaddress.Text = ""
    txtCustName.Text = ""
    txtlocation.Text = ""
    Else
    MessageBox.Show("Nothing Updated")
    End If
    Catch ex As MySql.Data.MySqlClient.MySqlException
    MessageBox.Show(ex.Message)
    End Try
    [/code]

    Let me know how this works for you. This code utilizes the MySQL connector found here:
    http://dev.mysql.com/downloads/connector/net/

    SS
  • its,not working.always i am getting the value of check as 1(if it updated or not).(one more thing.here i am getting the values of the textfield from the grid)
  • seancampbellseancampbell Pennsylvania, USA
    Try removing the line:

    cmd.ExecuteNonQuery()

    You are executing the query twice, and I suspect that the second time time it executes it is returning a different Check code than the first time it executes...

    To clarify, use this code:
    [code]

    cmd = New SqlCommand("update tb_customer set customername='" & txtCustName.Text & "' , customerlocation='" & txtlocation.Text & "' , customeraddress='" & txtaddress.Text & "' where CustomerId='" & txtCustomerId.Text & "' ", con)

    check = cmd.ExecuteNonQuery()
    If check > 0 Then
    Dim adap As SqlDataAdapter
    adap = New SqlDataAdapter("select * from tb_customer ", con)
    Dim ds As New DataSet
    adap.Fill(ds, "tb_customer")
    DataGridView1.DataSource = TestDataSet19BindingSource
    TestDataSet19BindingSource.DataSource = ds
    DataGridView1.Refresh()
    MessageBox.Show("Successfully updated")
    txtaddress.Text = ""
    txtCustName.Text = ""
    txtlocation.Text = ""
    Else
    MessageBox.Show("Nothing Updated")
    End If
    [/code]
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories