在GridView中获取异常

本文关键字:异常 获取 GridView | 更新日期: 2023-09-27 18:26:05

我使用了GridView控件,在更新(LinkButton)时执行代码。我使用了ObjectDataSource。我是个初学者。所以请解释一下。

正在获取错误-现在得到这个错误-参数化查询"(@pname varchar(7),@pgender varchar(8000),@pcity varchar(8),@pid"需要参数"@pgender",但没有提供。–

这是背后的代码-

 public static int UpdateEmployee(int id, string name, string gender, string city)
{
    string connection = "Data Source=SRM-318;Initial Catalog=employee;User ID=sa;Password=****";
    SqlConnection scon = new SqlConnection(connection);
      string updateQuery = "update emp SET name=@pname, gender=@pgender, city=@pcity WHERE id=@pid";
    SqlCommand scmd = new SqlCommand(updateQuery, scon);
    scmd.CommandType = CommandType.Text;
    scmd.Parameters.Add("@pname", SqlDbType.VarChar).Value = name;
    scmd.Parameters.Add("@pgender", SqlDbType.VarChar).Value = gender;
    scmd.Parameters.Add("@pcity", SqlDbType.VarChar).Value = city;
    scmd.Parameters.Add("@pid", SqlDbType.Int).Value = id;
     scon.Open();
    return scmd.ExecuteNonQuery();
}

参见设计页面代码-

<body>
<form id="form1" runat="server">
<div>
    <asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False"
        BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px"
        CellPadding="3" DataKeyNames="id" DataSourceID="ObjectDataSource1" ShowFooter="True"
        GridLines="Vertical" >
        <RowStyle BackColor="#EEEEEE" ForeColor="#8C4510" />
        <Columns>
            <asp:TemplateField ShowHeader="False">
                <ItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="False" CommandName="Edit"
                        Text="Edit"></asp:LinkButton>
                    &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Delete"
                        Text="Delete"></asp:LinkButton>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:LinkButton ID="LinkButton1" runat="server" CausesValidation="True" CommandName="Update"
                        Text="Update"></asp:LinkButton>
                    &nbsp;<asp:LinkButton ID="LinkButton2" runat="server" CausesValidation="False" CommandName="Cancel"
                        Text="Cancel"></asp:LinkButton>
                </EditItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="id" SortExpression="id">
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("id") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("id") %>'></asp:Label>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:LinkButton ID="lb" OnClick="lb_Click" ValidationGroup="INSERT" runat="server">Insert</asp:LinkButton>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="name" SortExpression="name">
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("name") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("name") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvEditName" ControlToValidate="TextBox1" runat="server"
                        Text="*" ErrorMessage="Name is Required" ForeColor="Red"></asp:RequiredFieldValidator>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="tbname" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvInsertName" ValidationGroup="INSERT" ControlToValidate="tbname"
                        runat="server" Text="*" ErrorMessage="Name is Required" ForeColor="Red"></asp:RequiredFieldValidator>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="gender" SortExpression="gender">
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("gender") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:DropDownList ID="DropDownList1" runat="server">
                        <asp:ListItem>Select Gender</asp:ListItem>
                        <asp:ListItem>Male</asp:ListItem>
                        <asp:ListItem>Female</asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="rfvEditGender" ControlToValidate="DropDownList1"
                        runat="server" Text="*" ErrorMessage="Gender is Required" ForeColor="Red" InitialValue="Select Gender"></asp:RequiredFieldValidator>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:DropDownList ID="DropDownList2" runat="server">
                        <asp:ListItem>Select Gender</asp:ListItem>
                        <asp:ListItem>Male</asp:ListItem>
                        <asp:ListItem>Female</asp:ListItem>
                    </asp:DropDownList>
                    <asp:RequiredFieldValidator ID="rfvInsertGender" ValidationGroup="INSERT" ControlToValidate="DropDownList2"
                        runat="server" Text="*" ErrorMessage="Gender is Required" ForeColor="Red" InitialValue="Select Gender"></asp:RequiredFieldValidator>
                </FooterTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="city" SortExpression="city">
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("city") %>'></asp:Label>
                </ItemTemplate>
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("city") %>'></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvEditCity" ControlToValidate="TextBox3" runat="server"
                        Text="*" ErrorMessage="City is Required" ForeColor="Red"></asp:RequiredFieldValidator>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="tbcity" runat="server"></asp:TextBox>
                    <asp:RequiredFieldValidator ID="rfvInsertCity" ValidationGroup="INSERT" ControlToValidate="tbcity"
                        runat="server" Text="*" ErrorMessage="City is Required" ForeColor="Red"></asp:RequiredFieldValidator>
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
        <FooterStyle BackColor="#CCCCCC" ForeColor="#8C4510" />
        <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" BackColor="#999999" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
        <AlternatingRowStyle BackColor="#DCDCDC" />
    </asp:GridView>
    <asp:ValidationSummary ValidationGroup="INSERT" ID="ValidationSummary1" ForeColor="Red"
        runat="server" />
    <asp:ValidationSummary ID="ForGridViewValidations" runat="server" />
    <br />
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server"
        InsertMethod="InsertEmployee" SelectMethod="GetAllEmployee" TypeName="EmployeeDataAccessLayer"
        UpdateMethod="UpdateEmployee" DeleteMethod="DeleteEmployee">
        <DeleteParameters>
            <asp:Parameter Name="id" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="id" Type="Int32" />
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="gender" Type="String" />
            <asp:Parameter Name="city" Type="String" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="name" Type="String" />
            <asp:Parameter Name="gender" Type="String" />
            <asp:Parameter Name="city" Type="String" />
        </InsertParameters>
    </asp:ObjectDataSource>
</div>
</form>

在GridView中获取异常

您应该添加sqlCommand参数。

ccmd.Parameters.AddWithValue("@name", value of name);
ccmd.Parameters.AddWithValue("@city", value of city);
ccmd.Parameters.AddWithValue("@gender", value of gender);
ccmd.Parameters.AddWithValue("@id", value of id);

您没有将参数namegendercityid传递给您的命令。

它应该是类似的东西

scmd.Parameters.Add["@name", SqlDbType.VarChar];
scmd.Parameters["@name"].Value = name;

依此类推

应该是这样的:

public static int UpdateEmployee(int id, string name, string gender, string city)
{
    string connection = "Data Source=SRM-318;Initial Catalog=employee;User ID=sa;Password=****";
    SqlConnection scon = new SqlConnection(connection);
    string updateQuery = "update emp SET name=@pname, gender=@pgender, city=@pcity WHERE id=@pid";
    SqlCommand scmd = new SqlCommand(updateQuery, scon);
    scmd.CommandType = CommandType.Text;
    scmd.Parameters.Add("@pname", SqlDbType.VarChar).Value = name;
    // Edit Starts
    if (!String.IsNullOrEmpty(gender))
    {
        scmd.Parameters.Add("@pgender", SqlDbType.VarChar).Value = gender;
    }
    else
    {
        scmd.Parameters.Add("@pgender", SqlDbType.VarChar).Value = DBNull.Value;
    }
    // Edit Ends
    scmd.Parameters.Add("@pcity", SqlDbType.VarChar).Value = city;
    scmd.Parameters.Add("@pid", SqlDbType.Int).Value = id;
    scon.Open();
    return scmd.ExecuteNonQuery();
}