Click here to Skip to main content
15,896,500 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

1 solution

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

Addition:

VB
...
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
Else
   statusparam.Value = 0
End If
i = cmd1.ExecuteNonQuery()

con.Close()
 
Share this answer
 
v2
Comments
[no name] 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

Else

End If
con.Close()

This is a part of my code..in this i want that if (i>0) then status(table2 column) should update as 1 else 0
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.
[no name] 11-Jan-13 1:13am    
Ok thanks..
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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900