Click here to Skip to main content
15,896,915 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
how to use this code in asp.net to update a table from another one



SQL
UPDATE t1 SET t1.[name] = t2.[name], t1.address = t2.address, t1.[date] = t2.[date]
FROM table1 as t1 INNER JOIN table2 AS t2 ON t1.billno = t2.billno
WHERE t1.billno Like 'A100%'




plzz help me with this
Posted
Comments
10923679 23-Jul-14 2:52am    
using this code .. how will i will be able to update the table ASP.Net

In connectStr, pass your Connection String.

string connectStr = "";//pass your connection string
string updateQuery = "UPDATE T1
SET T1.name = T2.name, T1.address_ = T2.address_
FROM TABLE1 T1
INNER JOIN TABLE2 T2
ON T1.billNo = T2.billNo
WHERE T2.billNo LIKE 'A100%'";

SqlConnection conn = new SqlConnection(connectStr);

SqlCommand cmd = new SqlCommand(updateQuery,conn);
conn.Open();
int result = cmd.ExecuteNonQuery();
conn.Close();

if(result>0)
{
   ClientScript.RegisterClientScriptBlock(this.GetType(), "alertbox", "<script language='javascript'>alert('Updated');</script>");
}


Hope this solves your problem!! :)
 
Share this answer
 
v3
 
Share this answer
 
Comments
10923679 23-Jul-14 5:59am    
well this is okay.

but i need to update from one table to another which having common fields .
Prakriti Goyal 23-Jul-14 9:09am    
Do you mean that you need to insert records in an existing table from another existing table?
10923679 23-Jul-14 9:24am    
yes right.. from existing table from another existing table ..
10923679 23-Jul-14 9:30am    
for eg.. table 1
i have some empty fields which are null.
i need to update those null values from another existing table 2.
comparing few common fields like employID.
Dilan Shaminda 23-Jul-14 14:03pm    
Hi, in your question you have asked "How do i use this Sql Command in Asp.Net to update Table" So that is why i have answered to refer that tutorial. I have checked this below query with two data tables. it updates even in nulls values with given conditions from TEST2 table to TEST1

UPDATE TEST1
SET TEST1.name = TEST2.name, TEST1.address_ = TEST2.address_
FROM TEST1
INNER JOIN TEST2
ON TEST1.billNo = TEST2.billNo
WHERE TEST2.billNo = 1

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