Click here to Skip to main content
15,891,629 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
table1 ---

column1
1002008000300-53-9

table2---

column1 column2 column3
1002008000300 53 9

how to populate from table1 to table2

SQL and ASP.Net(Csharp)
Posted

Dataset ds = GetYourDataFromTable1
try
{
Create connection
Create command
AddParameters to the command (column1, column2, column3)

C#
SqlTransaction trans;

trans = connection.BeginTransaction
String () split;
foreach (datarow row in ds.tables[0].rows) {
split = String.Split(row["column1"].ToString, "-");
if (split.Length == 3)
//set parameter values from split[0], split[1] and split[2]
    command.Execute()
} // end for each

trans.Commit

Catch Exception e
{
trans.Rollback
}


I would do it via stored procedure with three parameters...or, if the database is Microsoft SQL Server then single parameter xml type with column1 from the first table and doing everything in the database with single insert statement.

The above code is for going 1 for 1

If this helps, accept the solution so others may find it. Thank you.


SQL
UPDATE t2
SET
alldetails_cheque =  t1.chequeno
FROM
table1 t1
INNER JOIN table2 ON t1.id=t2.id and t1.name=t2.name
where t1.adviceno is null


Single update for both conditions assuming SP with parameters @id, @chequeNo and @adviceNO
SQL
UPDATE t2
SET
alldetails_cheque = CASE WHEN ISNULL(@adviceNO, '') = '' THEN t1.chequeno ELSE alldetails_cheque END,
adviceno = CASE WHEN ISNULL(@chequeno, '') = '' THEN t1.adviceNO ELSE adviceno END
FROM
table1 t1
INNER JOIN table2 ON t1.id=t2.id and t1.name=t2.name
WHERE
t2.id = @id 
 
Share this answer
 
v7
Comments
10923679 9-Sep-14 6:50am    
thank you .. i will try with this..
10923679 9-Sep-14 7:15am    
i got my solution ..
anyway thanks a lot..

there was a mistake in if condition
now it is working perfectly


protected void Button7_Click1(object sender, EventArgs e)
{

DataTable dt = new DataTable();
string query = "select * from table1";
using (SqlConnection con = new SqlConnection(con_str))
{
using (SqlCommand cmd = new SqlCommand(query))
{
using (SqlDataAdapter da = new SqlDataAdapter())
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
da.SelectCommand = cmd;
da.Fill(dt);
}
}
}

try
{

string query2;
// string query3;
using (SqlConnection con = new SqlConnection(con_str))
{
con.Open();

foreach (DataRow row in dt.Rows)
{
string str = row["AdviceNo"].ToString();


//The conditions

if (string.IsNullOrEmpty(str))
{


query2 = "update table2 set t2.alldetails_cheque=t1.chequeno from table1 as t1 and table2 as t2
on t1.id=t2.id and t1.name=t2.name where t1.adviceno is null";
SqlCommand cmd2 = new SqlCommand(query2, con);
cmd2.ExecuteNonQuery();
}
else
{
query2 = "update table2 set t2.alldetails_cheque=t1.adviceno from table1 as t1 and table2 as t2
on t1.id=t2.id and t1.name=t2.name where t1.chequeno is null ";
SqlCommand cmd2 = new SqlCommand(query2, con);
cmd2.ExecuteNonQuery();

}

}

con.Close();
}

}
catch (Exception ex)
{
Response.Write(ex.Message);

}
finally
{

Label2.Text = "updated";
}
Sinisa Hajnal 9-Sep-14 7:10am    
I've updated my solution with update query for cheques... just replace the condition and fields for the other one
10923679 10-Sep-14 6:33am    
how would you do via stored procedure with three parameters ??

can u please tell me about it
Sinisa Hajnal 10-Sep-14 7:09am    
CREATE PROCEDURE choose_some_name
@id int,
@all_details varchar(255),
@chequeno int
AS
BEGIN

// here goes update based on adviceNo empty or not

END
You can try using SUBSTRING AND CHARINDEX to separate the string and then insert the values.

SQL
WITH TAG AS (SELECT 'PHP | JAVA | PYTHON' AS Category)
SELECT SUBSTRING(Category, 1, CHARINDEX('|', Category) - 1) Category
FROM TAG


Output

Category
PHP

It has been explained here SQL Server CHARINDEX Function[^]
 
Share this answer
 
v2
Comments
Sinisa Hajnal 9-Sep-14 7:12am    
I've updated my solution with update query for cheques... just replace the condition and fields for the other one
10923679 9-Sep-14 7:25am    
ok. thank you
insert into table2(column1,column2,column3)
Select
Parsename(replace(column1,'-','.'),3) as column1
Parsename(replace(column1,'-','.'),2) as column2
Parsename(replace(column1,'-','.'),1) as column3 from table1


i have got my result..
 
Share this answer
 

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