3
Answers

Updating multiple tables

Petro Tiburcio

Petro Tiburcio

11y
1.2k
1

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

Answers (3)