how to use foreach loop to get row form datatable

Posted by Shanky11 under ASP.NET on 9/21/2013 | Points: 10 | Views : 6511 | Status : [Member] | Replies : 4
i have created a website for ecommerece
DataTable dt = new DataTable();
string query = "select * from temporder where ipaddress='"+ipAddress+"'";
SqlCommand cmd = new SqlCommand(query, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
da.Fill(dt);
foreach (DataRow row in dt.Rows)
{
row["pid"].ToString();
row["pname"].ToString();
row["quantity"].ToString();
row["amount"].ToString();
insert into orderdetails (pi,pname,quantity,amount)values("+row[pid]+","+row[pname]+","+row[quantity]+","+row[amount]+");
}
is this corect
if not please help me to corect this........
there are two table temporder and orderdetails i have to fetch in datatable and insert it another table name orderdetails




Responses

Posted by: Bandi on: 9/21/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down
foreach (DataRow row in dt.Rows){
insert into orderdetails (pi,pname,quantity,amount)values("+row["pid"].ToString()+","+row["pname"].ToString()+","+int.Parse(row["quantity"].ToString())+","+int.Parse(row["amount"].ToString())+");}

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Bandi on: 9/21/2013 [Member] [MVP] Platinum | Points: 25

Up
0
Down

foreach (DataRow row in dt.Rows){row["pid"].ToString();row["pname"].ToString();row["quantity"].ToString();row["amount"].ToString();insert into orderdetails (pi,pname,quantity,amount)values("+row[pid]+","+row[pname]+","+row[quantity]+","+row[amount]+");}

in the above code you are not executing insert statement... use sqlcommand and execute the statement. then only it will insert into db.....

http://www.codeproject.com/Articles/17169/Copy-Data-from-a-DataTable-to-a-SQLServer-Database
http://www.aspdotnet-suresh.com/2013/08/copy-add-rows-from-one-datatable-to-another-datatable.html

Mark This Response as Answer
--
Chandu
http://www.dotnetfunda.com/images/dnfmvp.gif

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 9/23/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi
try this code

protected void btData_Click(object sender, EventArgs e)
{
DataRow dr;
Dt.Columns.Add("Id");
Dt.Columns.Add("Name");
dr = Dt.NewRow();
dr[0] = 1;
dr[1] = "JK";
Dt.Rows.Add(dr);
dr = Dt.NewRow();
dr[0] = 2;
dr[1] = "JK1";
Dt.Rows.Add(dr);

string strConcate = "";
foreach (DataRow rw in Dt.Rows)
{
strConcate =strConcate + " EmpId : "+ rw[0].ToString() +"-" + "Ename : "+ rw[1].ToString();
}
Response.Write(strConcate);
}

Mark as Answer if its helpful to you

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Jayakumars on: 9/23/2013 [Member] [MVP] Bronze | Points: 25

Up
0
Down
hi

another way

check rows count like this

if (Dt.Rows.Count > 0)
{
string strConcate = "";
foreach (DataRow rw in Dt.Rows)
{
strConcate = strConcate + " EmpId : " + rw[0].ToString() + "-" + "Ename : " + rw[1].ToString();
}
Response.Write(strConcate);
}

Mark as Answer if its helpful to you

Shanky11, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response