Click here to Skip to main content
15,896,496 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
I am unable to figure out the exact problem of the given code when i try to update.


C#
protected void UpdateService(object sender, GridViewUpdateEventArgs e)
        {
            int ServiceId = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("lblServiceID")).Text);
            string ServiceName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtService")).Text;
            decimal Rate = Convert.ToDecimal(((TextBox)GridView1.Rows[e.RowIndex].FindControl("txtRate")).Text);
            string ParentService = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlParentService")).SelectedItem.Text;
            string GrandParentService = ((DropDownList)GridView1.Rows[e.RowIndex].FindControl("ddlGrandParentService")).SelectedItem.Text;
                                   
            SqlConnection con = new SqlConnection(strConnString);
            SqlCommand cmd = new SqlCommand();
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "update Service set ServiceName=@ServiceName,Rate=@Rate,"+
            "ParentService=@ParentService,GrandParentService=@GrandParentService "+
            "where ServiceId = @ServiceId;" +
             "select ServiceId,GrandParentService,ParentService,ServiceName,Rate from Service";
            cmd.Parameters.Add("@ServiceName", SqlDbType.VarChar).Value = ServiceName;
            cmd.Parameters.Add("@Rate", SqlDbType.Decimal).Value = Rate;
            cmd.Parameters.Add("@ParentService", SqlDbType.VarChar).Value = ParentService;
            cmd.Parameters.Add("@GrandParentService", SqlDbType.VarChar).Value = GrandParentService;
           
            
            GridView1.EditIndex = -1;
            GridView1.DataSource = GetData(cmd);
            GridView1.DataBind();
        }



private DataTable GetData(SqlCommand cmd)
        {
            DataTable dt = new DataTable();
            SqlConnection con = new SqlConnection(strConnString);
            SqlDataAdapter sda = new SqlDataAdapter();
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;
            con.Open();
            sda.SelectCommand = cmd;
            sda.Fill(dt);                    //Error "Must declare the scalar variable "@ServiceId".
            return dt;
        }




I am getting an error message "Must declare the scalar variable "@ServiceId" at the time of update.

Please help me. Thanks in advance.
Posted
Comments
apurba001 27-Dec-13 15:24pm    
Many thanks for your reply Richard.
ServiceId is a Autoincremented Primary Key. So i don't want to add from code.
Richard C Bishop 27-Dec-13 15:37pm    
Ok, then go ahead and remove that as a parameter you insert. See my updated solution.
apurba001 27-Dec-13 15:59pm    
Thank you very much.

That exception means you are not declaring the named variable as a parameter of your command.

You need to add this:

cmd.Parameters.Add("@ServiceId", SqlDbType.VarChar).Value = "the value for it";


[EDIT]

If you are not going to declare that variable and give it a value, then you cannot use it as a parameter in your WHERE clause. Your options are to remove it completely or add the variable.
 
Share this answer
 
v3
Comments
apurba001 27-Dec-13 15:52pm    
Actually i am storing a gridview data on ServiceId for updation. How can i overcome then.
Richard C Bishop 27-Dec-13 15:56pm    
You can still use the "@ServiceId", but you need to assign it the id from the table on the record you want to update. You can do some sort of query to get its value if you need it in the WHERE clause for an update.
As i can see in your code. You are using @ServiceId in your where clause

where ServiceId = @ServiceId;
and you are not passing value to this parameter
use below

SQL
cmd.Parameters.Add("@ServiceId", SqlDbType.YourDatatype).value=yourValue;
 
Share this answer
 
v2

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