Reputation: 1
This is my code
protected void btnnext_Click(object sender, EventArgs e)
{
string clientId = Context.User.Identity.GetUserId();
if (clientId != null)
{
SqlConnection con = new SqlConnection("Data Source=Mickey\\SQLEXPRESS;Initial Catalog=db1;Integrated Security=True;MultipleActiveResultSets=True;Application Name=EntityFramework");
SqlCommand com;
con.Open();
for(int i = 0; i < GridView1.Rows.Count; i++)
{
string fn = GridView1.Rows[i].Cells[2].Text;
string ln = GridView1.Rows[i].Cells[3].Text;
string ad = GridView1.Rows[i].Cells[4].Text;
string pn = GridView1.Rows[i].Cells[6].Text;
string em = GridView1.Rows[i].Cells[7].Text;
string str = "insert into orders
(FirstName, LastName, Address, PhoneNumber, Email,
TotalAmount,ModeofDelivery, ClientId, OrderNo, SpecialMess)
values(' " + fn + " ',' " + ln + " ',' " + ad+
" ',' " + pn + " ',' " + em + " ',' " + Label6.Text +
" ',' " + Label4.Text + " ',' " + clientId +
" ',' " + Label1.Text + " ',' " + TextBox1.Text + " ' )";
com = new SqlCommand(str, con);
com.ExecuteNonQuery();
}
string stri = "insert into orderDetails(orderNo, clientId, productId, quantity) select ' " + Label1.Text + " ',client_id,product_id,amount from cart where client_id = ' " + clientId + " ' ";
com = new SqlCommand(stri, con);
com.ExecuteNonQuery();
con.Close();
Response.Redirect("~/Pages/orders.aspx");
}
}
In this particular code, I am trying to insert data into two different tables.The first section of code is working properly where data is fetched from the gridview but the second section is not working properly even it is not throwing any errors still data does not get stored into the table.
string stri = "insert into orderDetails(orderNo,clientId,productId,quantity) select ' " + Label1.Text + " ',client_id,product_id,amount from cart where client_id = ' " + clientId + " ' ";
com = new SqlCommand(stri, con);
com.ExecuteNonQuery();
con.Close();
Response.Redirect("~/Pages/orders.aspx");
Any solution?
Upvotes: 0
Views: 428
Reputation: 4862
I would guess your subquery is not returning any rows because you're padding your id with spaces.
where client_id = ' " + clientId + " ' "
is going to produce sql that looks like
where client_id = ' 9 '
which probably wont match any records.
Upvotes: 3