Click here to Skip to main content
15,893,487 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
protected void rptsubmenu_ItemCommand(object source, RepeaterCommandEventArgs e)
    {

        Label lblprice = (Label)grdaddcart.FindControl("lblprice");
        if (e.CommandName == "menuitem")
        {


            Response.Cookies["addcart"].Value = sl_con.ExecuteScalar("select iif(isnull(max(Id)),0,max(Id))+1 from tbladdcart", "acs");
            Response.Cookies["addcart"].Expires = DateTime.Now.AddDays(1);
            sl_con.ExecuteNonQuery(@"INSERT INTO tbladdcart
                         (VId, item_id, quant, ad)
VALUES        ("+Response.Cookies["addcart"].ToString()+","+e.CommandArgument.ToString()+","+lblprice.Text+",'"+DateTime.Now.ToString()+"')", "acs");


            Response.Redirect("~/Restaurant_menu.aspx");

        }
    }


[edit]SHOUTING removed - OriginalGriff[/edit]

on click error

The isnull function requires 2 argument(s).
Posted
Updated 28-Aug-14 1:44am
v4
Comments
[no name] 28-Aug-14 7:21am    
Maybe you should add the other argument then.
OriginalGriff 28-Aug-14 7:44am    
DON'T SHOUT. Using all capitals is considered shouting on the internet, and rude (using all lower case is considered childish). Use proper capitalization if you want to be taken seriously.
Sanjay K. Gupta 28-Aug-14 8:22am    
Where you guys learned, passing parameters to the query in this way. Total disgusting. Please tell me the tutor name. :)

1 solution

Start by fixing the SQL Injection[^] vulnerability in your code.

Then, make the VId column an IDENTITY column[^].

Finally, insert the record without specifying a value for the VId column, and return the Scope_Identity() value to get the auto-generated ID.

C#
using (var connection = new SqlConnection("YOUR CONNECTION STRING HERE"))
using (var command = new SqlCommand("INSERT INTO tbladdcart (item_id, quant, ad) VALUES (@ItemId, @quant, @ad); SELECT Scope_Identity();", connection))
{
    command.Parameters.AddWithValue("@ItemId", e.CommandArgument);
    command.Parameters.AddWithValue("@quant", lblPrice.Text);
    command.Parameters.AddWithValue("@ad", DateTime.Now);

    int id = (int)command.ExecuteScalar();
    Response.Cookies["addcart"].Value = id.ToString();
    Response.Cookies["addcart"].Expires = DateTime.Now.AddDays(1);
    
    Response.Redirect("~/Restaurant_menu.aspx");
}
 
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