Click here to Skip to main content
11,928,605 members (48,686 online)
Rate this:
Please Sign up or sign in to vote.
See more: VB Windows MySQL VB.NET
Hi all

I have the below tables

Table1: username, password
Table2: username, password, status

Now i want to insert value to this to tables at a time and want to check if data inserted to table2 successfully then status should become 1 else 0

please tell me how to do that

Thank you
Posted 10-Jan-13 19:15pm

1 solution

Rate this: bad
Please Sign up or sign in to vote.

Solution 1

You can use MySqlConnection and MySqlCommand to execute the insert statements to the database. For an example, have a look at MySqlCommand[^]


Dim i As Integer = cmd1.ExecuteNonQuery()
cmd1.CommandText = "UPDATE Table SET Status = @NewStatus"
Dim statusparam AS MySqlParameter = new MySqlParameter("@NewStatus", MySqlDbType.Long); 
cmd1.Parameters.Add(statusparam )
If (i > 0) Then
   statusparam.Value = 1
   statusparam.Value = 0
End If
i = cmd1.ExecuteNonQuery()
Snehasish Nandy 11-Jan-13 0:41am
Dim cmd1 As MySqlCommand = New MySqlCommand("insert into table1 values username='" + txtusername.Text + "',password='" + txtpassword.Text + "'", con)

Dim i As Integer = cmd1.ExecuteNonQuery()
If (i > 0) Then


End If

This is a part of my this i want that if (i>0) then status(table2 column) should update as 1 else 0
Mika Wendelius 11-Jan-13 0:50am
So can you simply execute an UPDATE statement inside the if? Something like UPDATE Table2 SET Status = @NewStatus.

Note that I have used a parameter in the statement. You should never concatenate values to a Sql statement. This would leave you open to Sql injections, data type conversion problems and so on.

Have a look at the modified answer.
Snehasish Nandy 11-Jan-13 1:13am
Ok thanks..
Mika Wendelius 11-Jan-13 1:20am
You're welcome :)

One more thing, since you're using multiple statements which logically require a single unit of work, don't forget to use transactions. See MySqlTransaction[^]

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy | Mobile
Web03 | 2.8.151126.1 | Last Updated 11 Jan 2013
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100