4

我有一个可编辑的网格视图,用户只需按链接按钮即可编辑每一行。我收到一个错误,它停止了我的工作。请帮我。

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.

我的查询有什么问题吗?

4

5 回答 5

4

尝试这个

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

您已经通过在@oldName 中使用“N”大写来更改参数名称,因此

你得到那个错误

于 2013-04-08T06:05:16.500 回答
2

试试这个
代码看起来不错,但是command.commandtype = commandtype.text缺少这行可能就是问题所在。

同样,在传递参数 Addwithvalue 时,也可以在其他控件username.text上使用相同的参数username

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()
于 2013-04-12T06:01:07.400 回答
2

你的参数有错别字

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

你有

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

是不是应该

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

有大小写错误。

于 2013-04-08T05:37:36.187 回答
2

对不起,我没有足够的代表发表评论,所以我必须在这里回答......

为什么要将标签转换为标签并尝试将其用作字符串?:

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

似乎更合适。

于 2013-04-14T14:44:23.123 回答
2

我不确定这一点,但可能是因为当您尝试点击编辑按钮并且 gridview 的模式处于编辑模式时,lblUsername 标签将更改为 Editusername 文本框。所以我建议您尝试放置一个全局变量字符串,它将在更新或编辑之前存储 lblUsername 的数据..

于 2013-04-17T03:43:48.107 回答