Editable gridview not working

I have an editable gridview that the user can edit every row just by clicking the linkbutton button. I get an error message and stops working. please help me.

HTML

<asp:GridView ID="gvList" runat="server" class="gv" CellPadding="2" Font-Names="Calibri" ForeColor="#333333" Font-Size="16px" Width="500px" AutoGenerateColumns="true" OnRowCancelingEdit="gvList_RowCancelingEdit" OnRowEditing="gvList_RowEditing" OnRowUpdating="gvList_RowUpdating"> <Columns> <asp:TemplateField HeaderText="User Name" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" ItemStyle-Width="170px"> <ItemTemplate> <asp:Label runat="server" ID="lblUsername" Text='<%# Eval("cUserName") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:TextBox ID="Editusername" runat="server" Text='<%# Eval("cUserName") %>'></asp:TextBox> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Dept User" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" ItemStyle-Width="170px"> <ItemTemplate> <asp:Label runat="server" ID="lblDept" Text='<%# iif(Eval("lDeptUser"),"Yes","No") %>'></asp:Label> </ItemTemplate> <EditItemTemplate> <asp:RadioButtonList ID="RadioButtonList1" runat="server" > <asp:ListItem>Yes</asp:ListItem> <asp:ListItem>No</asp:ListItem> </asp:RadioButtonList> </EditItemTemplate> </asp:TemplateField> <asp:TemplateField HeaderText="Actions" HeaderStyle-ForeColor="White" HeaderStyle-Font-Bold="true" ItemStyle-Width="160px"> <ItemTemplate> <asp:LinkButton ID="btnedit" CommandName="Edit" runat="server" Text="Edit" /> <asp:LinkButton ID="btnDelete" CommandName="Delete" runat="server" Text="Delete" /> </ItemTemplate> <EditItemTemplate> <asp:LinkButton ID="btnUpdate" CommandName="Update" runat="server" Text="Update" /> <asp:LinkButton ID="btnCancel" CommandName="Cancel" runat="server" Text="Cancel" /> </EditItemTemplate> <HeaderStyle Font-Bold="True" ForeColor="White"></HeaderStyle> <ItemStyle Width="120px"></ItemStyle> </asp:TemplateField> </Columns> <EmptyDataTemplate> No records available </EmptyDataTemplate> <HeaderStyle BackColor="#808380" Font-Bold="True" ForeColor="White" VerticalAlign="Top" /> <EditRowStyle BackColor="#2461BF" /> <AlternatingRowStyle BackColor="#E3E3E3" /> </asp:GridView> 

Vb

 Protected Sub gvList_RowUpdating(sender As Object, e As System.Web.UI.WebControls.GridViewUpdateEventArgs) Handles gvList.RowUpdating Dim oldname = DirectCast(gvList.Rows(e.RowIndex).FindControl("lblUsername"), Label) Dim username As String = DirectCast(gvList.Rows(e.RowIndex).FindControl("Editusername"), TextBox).Text Dim tempUser = DirectCast(gvList.Rows(e.RowIndex).FindControl("RadioButtonList1"), RadioButtonList).SelectedIndex Dim query As String = "Update Intranet.dbo.Gn_ISCoordinators SET cUserName=@uname " + ", lDeptUser=@dept WHERE cUserName=@oldName " Using con = New SqlConnection(ConfigurationManager.ConnectionStrings("IntranetConnectionString").ConnectionString) con.Open() Dim cmd = New SqlCommand(query, con) cmd.Parameters.AddWithValue("@uname", username) cmd.Parameters.AddWithValue("@dept", Convert.ToByte(tempUser)) cmd.Parameters.AddWithValue("@oldname", oldname) cmd.ExecuteNonQuery() End Using gvList.EditIndex = -1 GetList() End Sub 

The problem in cmd.ExecuteNonQuery() gives me The parameterized query '(@uname nvarchar(7),@dept tinyint,@oldname nvarchar(4000))Update' expects the parameter '@oldname', which was not supplied.

Is there something wrong with my request?

+4
source share
5 answers

try it

 Dim oldname as Label =gvList.Rows(e.RowIndex).FindControl("lblUsername") Dim username as TextBox = gvList.Rows(e.RowIndex).FindControl("Editusername") Dim tempUser as RadioButtonList = DirectCast(gvList.Rows(e.RowIndex).FindControl("RadioButtonList1"), RadioButtonList).SelectedIndex Dim query As String = "Update Intranet.dbo.Gn_ISCoordinators SET cUserName=@uname " + ", lDeptUser=@dept WHERE cUserName=@oldname " Using con = New SqlConnection(ConfigurationManager.ConnectionStrings("IntranetConnectionString").ConnectionString) con.Open() Dim cmd = New SqlCommand(query, con) cmd.Parameters.AddWithValue("@uname", username.Text) cmd.Parameters.AddWithValue("@dept", Convert.ToByte(tempUser.SelectedIndex)) cmd.Parameters.AddWithValue("@oldname", oldname.Text) cmd.ExecuteNonQuery() End Using 

you changed the parameter name by making capital "N" in @oldName, so because of this

you get this error

+4
source

there is a typo in your parameter

  Dim query As String = "Update Intranet.dbo.Gn_ISCoordinators SET cUserName=@uname " + ", lDeptUser=@dept WHERE cUserName=@oldName " 

And you have

  cmd.Parameters.AddWithValue("@oldname", oldname) 

must not be

 cmd.Parameters.AddWithValue("@oldName", oldname) 

There is a mistake in capital and in a small letter.

+2
source

Try this. The code looks fine, but this line command.commandtype = commandtype.text missing, possibly a problem.

Also, when passing Addwithvalue instead of username use username.text instead of username for other controls

 Dim oldname As Label = DirectCast(gvList.Rows(e.RowIndex).FindControl("lblUsername"), Label) Dim username As TextBox = DirectCast(gvList.Rows(e.RowIndex).FindControl("Editusername"), TextBox).Text Dim tempUser As RadioButton = DirectCast(gvList.Rows(e.RowIndex).FindControl("RadioButtonList1"), RadioButtonList).SelectedIndex Dim query As String = "Update Intranet.dbo.Gn_ISCoordinators SET cUserName=@uname , lDeptUser=@dept WHERE cUserName=@oldName " Using con = New SqlConnection(ConfigurationManager.ConnectionStrings("IntranetConnectionString").ConnectionString) con.Open() Dim cmd = New SqlCommand(query, con) cmd.commandtype = commandtype.text cmd.Parameters.AddWithValue("@uname", username.text) cmd.Parameters.AddWithValue("@dept", Convert.ToByte(tempUser.selectedvalue)) cmd.Parameters.AddWithValue("@oldname", oldname.text) cmd.ExecuteNonQuery() End Using gvList.EditIndex = -1 GetList() 
+2
source

Sorry, I do not have enough comments for comments, so I have to answer here ...

Why are you labeling the label and trying to use it as a string ?:

 Dim oldname as string = DirectCast(gvList.Rows(e.RowIndex).FindControl("lblUsername"), Label).text 

seems more appropriate.

+2
source

I'm not sure about this, but perhaps because when you tried to click the edit button and the gridview mode is in edit mode, the lblUsername label will be changed to the Editusername text box. So I suggest you try putting a global variable string that will store lblUsername data before updating or editing.

+2
source

All Articles