Hi all, I have a gridview and I am doing an update in my gridview but I face an error saying must declare scalar variable @consultID in my update statement. Consultation ID is my primary key but I do not want to display it in my gridView. My codes are as
follows.
Then how your code knows that you want to update only a particular row and not all !
You can initially select the primar key column, and then hide it, so that it will be there for you to use in your code behind
but will not be visible to users (you can hide it page load or the data bound events of grid view like: GridView3.Columns[1].Visible = false; )
your update parameter is missing, so you must provide it. So using the above approach, you can assign @consultID and add it
to parametez just like you are adding other two.
Thiv
Member
5 Points
45 Posts
Must declare scalar variable @consultID in gridview
Jan 21, 2013 06:19 AM|LINK
Hi all, I have a gridview and I am doing an update in my gridview but I face an error saying must declare scalar variable @consultID in my update statement. Consultation ID is my primary key but I do not want to display it in my gridView. My codes are as follows.
//design of gridview
<asp:GridView ID="GridView3" runat="server" Width = "630px"
Font-Names = "Arial" AutoGenerateColumns="false"
Font-Size = "11pt" AlternatingRowStyle-BackColor = "LightSteelBlue" PageSize ="5" OnPageIndexChanging = "OnPaging"
HeaderStyle-BackColor ="Blue" AllowPaging ="true" ShowFooter = "false" onrowediting="EditCurrentDiog" onrowupdating="UpdateCurrDiog"
onrowcancelingedit="CancelEditCurrDiaog" DataKeyNames="">
//update method in gridview
SqlConnection con = new SqlConnection(connectionstring);
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "update currentDiagnosis set history=@history,examination=@examt where consultationID=@consultID";
cmd.Parameters.Add("@history", SqlDbType.VarChar).Value = his;
cmd.Parameters.Add("@exam", SqlDbType.VarChar).Value = exam;
cmd.ExecuteNonQuery();
con.Close();
cmd.Parameters.Clear();
GridView3.EditIndex = -1;
BindData(this, new EventArgs());
Any idea how to solve this?
usman400
Contributor
3493 Points
721 Posts
Re: Must declare scalar variable @consultID in gridview
Jan 21, 2013 06:33 AM|LINK
Then how your code knows that you want to update only a particular row and not all !
You can initially select the primar key column, and then hide it, so that it will be there for you to use in your code behind
but will not be visible to users (you can hide it page load or the data bound events of grid view like: GridView3.Columns[1].Visible = false; )
your update parameter is missing, so you must provide it. So using the above approach, you can assign @consultID and add it
to parametez just like you are adding other two.
shivalthakur
Participant
1857 Points
539 Posts
Re: Must declare scalar variable @consultID in gridview
Jan 21, 2013 06:34 AM|LINK
you need to provide @consultID from your application.
use datakeynames not to show pk......
take a look on this
http://www.codeproject.com/Articles/23833/DataKeyNames
Response.Write("Success");
Best Of Luck
Shival Thakur
oned_gk
All-Star
35874 Points
7331 Posts
Re: Must declare scalar variable @consultID in gridview
Jan 21, 2013 07:38 AM|LINK
cmd.Parameters.Add("@consultID", SqlDbType.Int).Value = Gridview3.DataKeys[Gridview3.EditIndex];Suwandi - Non Graduate Programmer
ManikandanUl...
Participant
850 Points
253 Posts
Re: Must declare scalar variable @consultID in gridview
Jan 21, 2013 07:40 AM|LINK
add parameter for @consultID and assign value.
cmd.Parameters.Add("@consultID", SqlDbType.VarChar).Value = "putvalue//assign value.";
Click "…Mark As Answer" if my reply helpful....
NadeemZee
Participant
942 Points
179 Posts
Re: Must declare scalar variable @consultID in gridview
Jan 21, 2013 07:43 AM|LINK
hi update your update method as below:
//update method in gridview SqlConnection con = new SqlConnection(connectionstring); SqlCommand cmd = new SqlCommand(); cmd.CommandType = CommandType.Text; cmd.CommandText = "update currentDiagnosis set history=@history,examination=@examt where consultationID=@consultID"; cmd.Parameters.Add("@history", SqlDbType.VarChar).Value = his; cmd.Parameters.Add("@exam", SqlDbType.VarChar).Value = exam; cmd.Parameters.Add("@consultID", SqlDbType.Int).Value = Gridview3.DataKeys[Gridview3.EditIndex]; cmd.ExecuteNonQuery(); con.Close(); cmd.Parameters.Clear(); GridView3.EditIndex = -1; BindData(this, new EventArgs());Do FEAR (Face Everything And Rise)
Please mark as Answer if my post helps you..!
Thiv
Member
5 Points
45 Posts
Re: Must declare scalar variable @consultID in gridview
Jan 21, 2013 07:55 AM|LINK
hi @NadeemZee I tried your ur method this is the error I am getting at this line of code:
cmd.Parameters.Add("@consultID", SqlDbType.Int).Value = Gridview3.DataKeys[Gridview3.EditIndex];Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index
Decker Dong ...
All-Star
118619 Points
18779 Posts
Re: Must declare scalar variable @consultID in gridview
Jan 23, 2013 01:53 AM|LINK
You haven't set DataKeyNames for your GridView yet.