简体   繁体   中英

Table updating in SQL with VB.NET

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM