I am unable to figure out the exact problem of the given code when i try to update.
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. "ServiceId" is the primary key for the table.
Please help me. Thanks in advance.
@ServiceIdin yourSqlCommandbut you never add it as a parameter withParameters.Add()method.WHEREclause?