K. Kobus K. Kobus - 18 days ago 5
ASP.NET (C#) Question

Insert into Order table where customer_id is taken from Customer table where login == session

I don't know how to do this.

When user bought all products i wanna insert it into Orders table, additionally i wanna insert there customer_id from Customer table where user login = Session["id"]. I tried this but it didn't work :/ What should i do?

protected void ConfirmPurchase_Click(object sender, EventArgs e)
{
if (Session["id"] != null)
{
string username;
username = Convert.ToString(Session["id"]);

SqlConnection con = new SqlConnection(@"Data Source=SOME_SQL;Initial Catalog=Shop;Integrated Security=True");

con.Open();

SqlCommand cmd = new SqlCommand("INSERT INTO Orders (order_date, paid, transact_status, customer_id) VALUES (GETDATE() ,'" + cost.ToString("c") + "' ,'Przyjęto do bazy', SELECT customer_id FROM Customer WHERE login='" + username + "')", con);

cmd.ExecuteNonQuery();

Response.Redirect("Payment/Payment.aspx");
}
else
{
Response.Redirect("InputPersonalData.aspx");
}
}

Answer

You can try by surrounding the user select with "(" and ")"

Instead of

SELECT customer_id FROM Customer WHERE login='" + username + "' 

use

( SELECT customer_id FROM Customer WHERE login='" + username + "' )
Comments