Exist
Exist

Reputation: 1

Update MySQL Field in VB.NET

I am trying to update a field in my MySQL database using VB.NET.....

Basically, I can read the field's content, using this code:

SQL = "SELECT * FROM `boomtable` WHERE `Tab1` = 'CLOSED'"

However, I want to update the Tab1 field to have the value OPEN if it's currently closed, and make the value CLOSED if it's currently OPEN.. Right now, it's CLOSED, (I put the value in manually when making my table)

Here is the code I'm using that changes my label based on the Tab1 field's value that works fine... Except I want to actually make it update the field in the database in addition to changing the label text.....

If myData.HasRows = 0 Then
            Label1.Text = "CLOSED"
            myData.Close()
        Else
            'if yes outputs this:
            Label1.Text = "OPEN"
           myData.Close()

        End If

I assume it will look something like this:

    If myData.HasRows = 0 Then
        SQL = "UPDATE boomtable SET Tab1 = 'CLOSED' Where Tab1 = 'OPEN'"
        Label1.Text = "CLOSED"
        myData.Close()
    Else
        'if yes outputs this:
        Label1.Text = "OPEN"

        SQL = "UPDATE boomtable SET Tab1 = 'OPEN' Where Tab1 = 'CLOSED'"
        myData.Close()

    End If

But that update line isn't working for me... Does anyone know the right code/syntax? Thanks.

Upvotes: 0

Views: 750

Answers (1)

Deepak
Deepak

Reputation: 7927

you didn't mention the update command for sqlserver here or you using anywhere.

Please try it if you are not using it.

SQL = "UPDATE boomtable SET Tab1 = 'CLOSED' Where Tab1 = 'OPEN'"
dim connection as new SqlConnection("connectionstring")
dim command as new SqlCommand(sql,connection)
command.ExecuteNonQuery()
connection.close() 

Upvotes: 1

Related Questions