Click here to Skip to main content
Rate this: bad
Please Sign up or sign in to vote.
See more: 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 18: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 at 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 at 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 at 11-Jan-13 1:13am
Ok thanks..
Mika Wendelius at 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
0 Sergey Alexandrovich Kryukov 498
1 OriginalGriff 439
2 ChintanShukla 305
3 Richard Deeming 250
4 RyanDev 210
0 Sergey Alexandrovich Kryukov 8,901
1 OriginalGriff 7,571
2 CPallini 2,603
3 Richard MacCutchan 2,095
4 Abhinav S 1,893

Advertise | Privacy | Mobile
Web02 | 2.8.140827.1 | Last Updated 11 Jan 2013
Copyright © CodeProject, 1999-2014
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