0

好的,所以我正在慢慢地在我的项目上取得进展,但我收到了以下错误消息:

“此 SqlParameterCollection 不包含具有 ParameterName 'Name' 的 SqlParameter。”

错误指向我的 vb 代码中的以下语句: e.Command.Parameters("Name").Value = NewName.Text

有人可以看看我的代码并帮助我解决我可能做错的事情。

这是我的vb代码:

Inherits System.Web.UI.Page

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

End Sub

Private Sub GridView1_RowCommand(sender As Object, e As GridViewCommandEventArgs) Handles     Laboratory.RowCommand
    ' Insert data if the CommandName == "Insert"
    ' and the validation controls indicate valid data...
    If e.CommandName = "Insert" AndAlso Page.IsValid Then
        ' Insert new record...
        SqlDataSource2.Insert()
    End If
End Sub

Protected Sub SqlDataSource2_Inserting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceCommandEventArgs) Handles SqlDataSource2.Inserting
    ' Programmatically reference Web controls in the inserting interface...
    Dim NewName As TextBox = Laboratory.FooterRow.FindControl("txtName")
    Dim NewAddress As TextBox = Laboratory.FooterRow.FindControl("txtAddress")
    Dim NewProvidence As TextBox = Laboratory.FooterRow.FindControl("txtProvidence")
    Dim NewCity As TextBox = Laboratory.FooterRow.FindControl("txtCity")
    Dim NewZipCode As TextBox = Laboratory.FooterRow.FindControl("txtZipCode")
    Dim NewCountry As TextBox = Laboratory.FooterRow.FindControl("txtcountry")
    Dim NewPhone As TextBox = Laboratory.FooterRow.FindControl("txtPhone")
    Dim NewFax As TextBox = Laboratory.FooterRow.FindControl("txtFax")
    Dim NewEmail As TextBox = Laboratory.FooterRow.FindControl("txtEmail")

    ' Set the ObjectDataSource's InsertParameters values...
    ' THIS IS WHERE I THINK I AM HAVING MY PROBLEM...
    e.Command.Parameters("Name").Value = NewName.Text
    e.Command.Parameters("Address").Value = NewAddress.Text
    e.Command.Parameters("Providence").Value = NewProvidence.Text
    e.Command.Parameters("City").Value = NewCity.Text
    e.Command.Parameters("ZipCode").Value = NewZipCode.Text
    e.Command.Parameters("Country").Value = NewCountry.Text
    e.Command.Parameters("Phone").Value = NewPhone.Text
    e.Command.Parameters("Fax").Value = NewFax.Text
    e.Command.Parameters("Email").Value = NewEmail.Text

End Sub

这是我的html。

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>

    <br />
    <br />
    <br />
    <br />
    <asp:GridView ID="Laboratory" runat="server" AllowSorting="True" AutoGenerateColumns="False" DataKeyNames="LaboratoryID" DataSourceID="SqlDataSource2" ShowFooter="True">
        <Columns>
            <asp:TemplateField ShowHeader="False">
                <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>
                <FooterTemplate>
                    <asp:Button ID="AddRow" runat="server" CommandName="Insert" Text="Add" />
                </FooterTemplate>
                <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>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Name" SortExpression="Name">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="TxtName" runat="server"></asp:TextBox>
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Address" SortExpression="Address">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Address") %>'></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="txtAddress" runat="server"></asp:TextBox>
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label2" runat="server" Text='<%# Bind("Address") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Providence" SortExpression="Providence">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("Providence") %>'></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="TxtProvidence" runat="server"></asp:TextBox>
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("Providence") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="City" SortExpression="City">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("City") %>'></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="TxtCity" runat="server"></asp:TextBox>
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("City") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="ZipCode" SortExpression="ZipCode">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("ZipCode") %>'></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="TxtZipCode" runat="server"></asp:TextBox>
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label5" runat="server" Text='<%# Bind("ZipCode") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Country" SortExpression="Country">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("Country") %>'></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="TxtCountry" runat="server"></asp:TextBox>
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label6" runat="server" Text='<%# Bind("Country") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Phone" SortExpression="Phone">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("Phone") %>'></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="TxtPhone" runat="server"></asp:TextBox>
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label7" runat="server" Text='<%# Bind("Phone") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Fax" SortExpression="Fax">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox8" runat="server" Text='<%# Bind("Fax") %>'></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="TxtFax" runat="server"></asp:TextBox>
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label8" runat="server" Text='<%# Bind("Fax") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <asp:TemplateField HeaderText="Email" SortExpression="Email">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox9" runat="server" Text='<%# Bind("Email") %>'></asp:TextBox>
                </EditItemTemplate>
                <FooterTemplate>
                    <asp:TextBox ID="TxtEmail" runat="server"></asp:TextBox>
                </FooterTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label9" runat="server" Text='<%# Bind("Email") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:GreatGrizzlyConnectionString1 %>" DeleteCommand="DELETE FROM [Laboratory] WHERE [LaboratoryID] = @LaboratoryID" InsertCommand="INSERT INTO [Laboratory] ([Name], [Address], [Providence], [City], [ZipCode], [Country], [Phone], [Fax], [Email]) VALUES (@Name, @Address, @Providence, @City, @ZipCode, @Country, @Phone, @Fax, @Email)" SelectCommand="SELECT * FROM [Laboratory]" UpdateCommand="UPDATE [Laboratory] SET [Name] = @Name, [Address] = @Address, [Providence] = @Providence, [City] = @City, [ZipCode] = @ZipCode, [Country] = @Country, [Phone] = @Phone, [Fax] = @Fax, [Email] = @Email WHERE [LaboratoryID] = @LaboratoryID">
        <DeleteParameters>
            <asp:Parameter Name="LaboratoryID" Type="Int32" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Address" Type="String" />
            <asp:Parameter Name="Providence" Type="String" />
            <asp:Parameter Name="City" Type="String" />
            <asp:Parameter Name="ZipCode" Type="String" />
            <asp:Parameter Name="Country" Type="String" />
            <asp:Parameter Name="Phone" Type="String" />
            <asp:Parameter Name="Fax" Type="String" />
            <asp:Parameter Name="Email" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Name" Type="String" />
            <asp:Parameter Name="Address" Type="String" />
            <asp:Parameter Name="Providence" Type="String" />
            <asp:Parameter Name="City" Type="String" />
            <asp:Parameter Name="ZipCode" Type="String" />
            <asp:Parameter Name="Country" Type="String" />
            <asp:Parameter Name="Phone" Type="String" />
            <asp:Parameter Name="Fax" Type="String" />
            <asp:Parameter Name="Email" Type="String" />
            <asp:Parameter Name="LaboratoryID" Type="Int32" />
        </UpdateParameters>
    </asp:SqlDataSource>
    <br />
    <br />
    <br />
    <br />
    <br />
    <br />

</div>
</form>
</body>
</html>
4

1 回答 1

0

您需要创建参数,可能有很多变化,但这里是一个简单的

Dim Name As New SqlParameter("@Name", SqlDbType.nvarchar(50))
Name.Value = txtName.text
e.Command.Parameters.Add(Name)

您需要为每个参数执行此操作。

如果参数已经存在,那么您不需要创建它!您只需使用以下内容。我添加的只是原始代码中的“@”。

e.Command.Parameters("@Name").Value = NewName.Text 

(自从我从事这些工作以来已经有一段时间了。)

于 2012-09-21T18:03:00.483 回答