I have 2 tables namely Profile and Info.
My table looked like this:
PROFILE
| p_Id | FirstName | LastName |
| 1 | Jack | Cole |
| 2 | Cynthia | Cole |
| 3 | Robert | Cole |
INFO
| I_Id | childsID | fathersID | mothersID |
| 1 | 1 | 3 | 2 |
I'm retrieving values from those tables by displaying them in textboxes, my select query is:
SELECT p.p_Id, p.FirstName, p.LastName, i.*, (SELECT pp.FirstName+' '+pp.LastName FROM Profile pp WHERE pp.p_Id=i.childsID) AS child, (SELECT pp.FirstName+' '+pp.LastName FROM Profile pp WHERE pp.p_Id=i.fathersID) AS father, (SELECT pp.FirstName+' '+pp.LastName FROM Profile pp WHERE pp.p_Id=i.mothersID) AS mother FROM Info i INNER JOIN Profile p ON p.p_Id=i.childsID
no problem with select, I'm able to display values on textboxes, but the problem is, I can't update them, I have tried so far this:
using (SqlCommand cmd = con.CreateCommand()) { con.Open(); cmd.CommandText = @"UPDATE Profile SET FirstName=@fname, LastName=@lname WHERE p_Id = @pid; UPDATE Info SET childsID=@child, fathersID=@father, mothersID=@mother, Country=@country WHERE I_Id = @iid;"; cmd.Parameters.Add(new SqlParameter("@fname", txtfname.Text)); cmd.Parameters.Add(new SqlParameter("@lname", txtlname.Text)); cmd.Parameters.Add(new SqlParameter("@child", txtchild.Text)); cmd.Parameters.Add(new SqlParameter("@father", txtfather.Text)); cmd.Parameters.Add(new SqlParameter("@mother", txtmother.Text)); cmd.Parameters.Add(new SqlParameter("@country", txtcountry.Text)); cmd.Parameters.Add(new SqlParameter("@pid", txtpid.Text)); cmd.Parameters.Add(new SqlParameter("@iid", txtiid.Text)); cmd.ExecuteNonQuery(); Response.Write("alert('DATA UPDATED')"); }
I'm using c# and Asp.net Thanks in advance :) God Bless
Parveen SiwachPosted May 16, 2013, 8:20 AM
SqlConnection con = new SqlConnection("Data Source=localhost;Initial Catalog=Northwind;uid=sa;pwd=sa;");
con.Open();
using (SqlCommand cmd = con.CreateCommand())
{
con.Open();
cmd.CommandText = @"UPDATE Profile SET FirstName=@fname, LastName=@lname WHERE p_Id = @pid; ";
cmd.Parameters.Add(new SqlParameter("@fname", txtfname.Text));
cmd.Parameters.Add(new SqlParameter("@lname", txtlname.Text));
cmd.Parameters.Add(new SqlParameter("@pid", txtpid.Text));
cmd.ExecuteNonQuery();
cmd.CommandText = @"UPDATE Info SET childsID=@child, fathersID=@father, mothersID=@mother, Country=@country WHERE I_Id = @iid;";
cmd.Parameters.Add(new SqlParameter("@child", txtchild.Text));
cmd.Parameters.Add(new SqlParameter("@father", txtfather.Text));
cmd.Parameters.Add(new SqlParameter("@mother", txtmother.Text));
cmd.Parameters.Add(new SqlParameter("@country", txtcountry.Text));
cmd.Parameters.Add(new SqlParameter("@iid", txtiid.Text));
cmd.ExecuteNonQuery();
Response.Write("alert('DATA UPDATED')");
}
Note: For these type of scenerios SqlTransactions must be used.
e.g:
SqlTransaction myTrans = con.BeginTransaction();
cmd.Transaction = myTrans;
//For Commit
myTrans.Commit();
//For Rollback
myTrans.Rollback();
Hope this will solve your problem.
Petro TiburcioPosted May 16, 2013, 7:37 AM
I have used stored procedure for my data entry page. Below is my code:
and in my stored procedure I have this:
But I just don't know how to do it with multiple tables update, I know if it is just one table. I have tried bunch of update queries but nothing helps. :(
Shankar MPosted May 16, 2013, 7:23 AM
Hi Pertro,
You can create a stored Procedure that contains the update statements that is created as a
Explicit Transaction using BEGIN TRANSACTION AND COMMIT TRANSACTION.
And, you can make a call to the Stored Procedure using the ExecuteNonQuery() Method of Command object.
Thanks, Shankar M