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?
where client_id = ' " + clientId + " 'is not satisfied so nothing is selected & nothing is inserted. (Note the extra spaces you have - another reason this must be rewritten as a parameterized query)'; DROP Table Orders; --and get an error screen all the time now. DON'T use string concatenation. Use parameterized queries. They are a easier to write, faster and safer