Skip to content

Update the tables sql,asp.net c#

Prakash P

Asked Mar 27, 2013, 10:33 PM EDT1 answer
Share
LinkedInX
hi, i have two table t1,t2 and one target table t3
 
t1 have 3 columns
t2 have 3 columns (different columns)

i inserted t1,t2 tables and stored successfully
  now i want to copy(column values) the all t1,t2 values in t3 table based on (t1.id=t2.id )

and next i want to fetch all  t3 column values in a gridview
 plz help

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Vishal Gilbile
    0
    Hello Friend,
                   If you are using sql server as your database you can simply make use of join query and start copying the data into your t3 table. following would be the query for the same.

    Assume that your t1 contains columns as (Id,Name,Address)

    Assume that your t2 contains column as (Id,DeptId,DeptName)

    Assume that your t3 contains column as (Id,Name,DeptName)

    and now i want to copy id,name and deptname in t3 from t1 and t2 respectively.

    insert into t3(Id,Name,DeptName)
    select m1.Id,m1.Name,m2.DeptName
    from t1 as m1 join t2 as m2
    on m1.Id==m2.Id


    And finally for displaying the records from t3 on gridview. Following code you can make use of

    SqlConnection con=new SqlConnection("ConnectionString here");
    SqlDataAdapter da=new SqlDataAdapter("Select * from t3",con);
    DataSet ds=new DataSet();
    da.Fill(ds);
    if(ds.Tables[0].Rows.Count>0)
          gridview1.DataSource=ds.Tables[0].DefaultView;
    else
            gridview1.EmptyDataText="No Records Found";
    gridview1.DataBind();

    Hope that helps you.

    With Regards,
    Vishal Gilbile.
    0