【发布时间】:2013-03-30 02:06:56
【问题描述】:
我有一个可编辑的网格视图,用户只需按下链接按钮即可编辑每一行。我收到一个错误,它停止了我的工作。请帮帮我。
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
问题出在cmd.ExecuteNonQuery() 它给了我The parameterized query '(@uname nvarchar(7),@dept tinyint,@oldname nvarchar(4000))Update' expects the parameter '@oldname', which was not supplied.
我的查询有什么问题吗?
【问题讨论】:
-
改错错字'@oldname'后,如果您仍然一无所获,请检查您是否在绑定gridview
if (!Page.IsPostBack) {gridviewBIND}时使用page.ispostback@参考satindersinght.blogspot.in/2012/08/… -
@Satindersingh 是的,我以前做过。
-
您的代码看起来不错,但缺少这行
command.commandtype = commandtype.text可能是问题所在 -
如果变量(在本例中为
oldname)为Nothing,则会出现此错误。