Home > OS >  Table updating in SQL with VB.NET
Table updating in SQL with VB.NET

Time:08-13

I'm updating my table, but I don't know why doesn't work. I trace the program and understand that the code will run, but the table won't be updated.

Dim commandf3 As New SqlCommand("Update Barcode set Tedad=@Tedad,Vazn=@Vazn,Ojrat=@Ojrat,Availability=@Availability where Onvan=@Onvan", connection)
commandf3.Parameters.AddWithValue("@Onvan", txtM.Text)
commandf3.Parameters.AddWithValue("@Tedad", tedadjadid)
commandf3.Parameters.AddWithValue("@Vazn", vaznjadid)
commandf3.Parameters.AddWithValue("@Ojrat", ojratjadid)
commandf3.Parameters.AddWithValue("@Availability", "T")
commandf3.ExecuteNonQuery()

Thanks for helping.

CodePudding user response:

This isn't a definitive answer but it should help you resolve it. Use this code instead:

  Using commandf3 As New SqlCommand("Update Barcode set Tedad=@Tedad,Vazn=@Vazn,Ojrat=@Ojrat,Availability=@Availability where Onvan=@Onvan", connection)
        commandf3.Parameters.AddWithValue("@Onvan", txtM.Text)
        commandf3.Parameters.AddWithValue("@Tedad", tedadjadid)
        commandf3.Parameters.AddWithValue("@Vazn", vaznjadid)
        commandf3.Parameters.AddWithValue("@Ojrat", ojratjadid)
        commandf3.Parameters.AddWithValue("@Availability", "T")
        Try
            connection.Open()
            commandf3.ExecuteNonQuery()
        Catch ex As Exception
            'Handle Exception - check ex.Message for info
        Finally
            connection.Close()
        End Try
    End Using

If the issue is that you didn't open the connection first, this may sort it. If the connection is permanently open, maybe not. But the catch should let you work out exactly what's happening. If there's no error, then break the code before ExecuteNonQuery, check the value of commandf3.CommandText and verify it's really what you want, and try that command directly in SQL if needed.

  • Related