插入Gridview时必须声明标量变量错误

时间:2014-03-31 18:25:55

标签: asp.net vb.net gridview

我的网页由GridView组成,允许用户查看,编辑和插入数据。使用DetailView-Form插入数据,DetailView-Form通过javaScript类型窗口打开。 GridView中的一列包含company_guid,它通过在登录页面中设置的会话捕获。我试图捕获用户的company_guid并在插入数据时将其记录到网格中。我能够查看和编辑Gridview,但是当我尝试插入时出现以下错误:必须声明标量变量" @ companyguid"

以下是我的超文本 - GridView:

 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
        DataKeyNames="Part_ID" DataSourceID="SqlDataSource1"  
     AutoGenerateEditButton="True" AllowPaging="True" AllowSorting="True">
     <Columns>
        <asp:BoundField DataField="Part_ID" HeaderText="Part_ID" 
             SortExpression="Part_ID" InsertVisible="False" ReadOnly="True" Visible="false" />

        <asp:BoundField DataField="Company_guid" HeaderText="Company_guid" 
             SortExpression="Company_guid" Visible="false"  />

         <asp:BoundField DataField="Part_Name" HeaderText="Part_Name" 
             SortExpression="Part_Name" />
         <asp:BoundField DataField="Part_Desc" HeaderText="Part_Desc" 
             SortExpression="Part_Desc" />

         <asp:CheckBoxField DataField="Active" HeaderText="Active" 
             SortExpression="Active" />

        <asp:BoundField DataField="UpdateDate" HeaderText="UpdateDate" 
             SortExpression="UpdateDate" />

         <asp:BoundField DataField="UpdateBy" HeaderText="UpdateBy" 
             SortExpression="UpdateBy" />

     </Columns>
    </asp:GridView> 

详情视图(这是通过JavaScript窗口打开的内容):

<asp:DetailsView
        id="dtlCarrier"
        DataSourceID="SqlDataSource1"
        AutoGenerateInsertButton="True"
        AutoGenerateRows="False"
        DefaultMode="Insert"
        Runat="server" DataKeyNames="Part_ID">
        <Fields>
           <asp:BoundField DataField="Part_Name" HeaderText="Part_Name" 
                SortExpression="Part_Name" />
            <asp:BoundField DataField="Part_Desc" HeaderText="Part_Desc" 
                SortExpression="Part_Desc" />

              <asp:CheckBoxField
            DataField="Active"
            HeaderText="Active" SortExpression="Active" />

            <asp:BoundField DataField="UpdateDate" HeaderText="UpdateDate" 
                SortExpression="UpdateDate" />
            <asp:BoundField DataField="UpdateBy" HeaderText="UpdateBy" 
                SortExpression="UpdateBy" />

        </Fields>
    </asp:DetailsView> 



<!-- the sql data source -->  
    <asp:SqlDataSource 
    ID="SqlDataSource1" 
    ConnectionString="<%$ ConnectionStrings:ShipperNotificationConnectionString %>" 
    SelectCommand="SELECT * FROM [Part]"
    UpdateCommand="UPDATE Part SET Part_Name=@Part_Name,
    Part_Desc=@Part_Desc, Active=@Active, UpdateDate=@UpdateDate, UpdateBy=@UpdateBy  
    WHERE Part_ID=@Part_ID" 
    InsertCommand="INSERT Part (company_guid,Part_Name,Part_Desc,Active,UpdateDate,UpdateBy)
            VALUES (@companyguid,@Part_Name,@Part_Desc,@Active,@UpdateDate,@UpdateBy)"          
   runat="server" 
        />

背后的代码:

Public Class SupplierPartsMgmt
    Inherits System.Web.UI.Page

    'Globally declare my variable to hold the company_guid
    Dim companyGuid As String = ""

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

        'initialized companyGuid to the session varible, which is capured/stored via the log-in pg
        If Not Page.IsPostBack Then
            companyGuid = Session("numrecord").ToString
        Else
            companyGuid = Session("numrecord").ToString
        End If

    End Sub

    Protected Sub GridView1_RowCommand(ByVal sender As Object, ByVal e As GridViewCommandEventArgs)
        'Dim companyGuid As String = ""

        If Not String.IsNullOrEmpty(companyGuid) Then
            SqlDataSource1.InsertParameters.Add("companyguid", companyGuid)
            SqlDataSource1.Insert()
        End If
    End Sub

End Class

我可以帮忙解决一下我做错了什么吗?

1 个答案:

答案 0 :(得分:1)

将@放在这一行:

SqlDataSource1.InsertParameters.Add("companyguid", companyGuid)
仅在编写查询以指示查询参数时才需要@p。

如果您仍然需要@,我认为您需要声明&lt; InsertParameters&gt; asp:SqlDataSource元素下的元素。

选项#1

您可以直接声明基于会话的插入参数,如下所示:

<InsertParameters>
   <asp:SessionParameter Name="companyguid" SessionField="companyguid" Type="String" />
</InsertParameters>

选项#2

您还可以在oninserting命令中设置公司guid参数值,如下所示:

<InsertParameters>
   <asp:Parameter Name="companyguid" Type="String" />
</InsertParameters>

OnInserting =“On_Inserting” - 将此属性添加到SQL数据源

protected void On_Inserting(Object sender, SqlDataSourceCommandEventArgs e) 
{
    e.Command.Parameters["@companyguid"].Value = valueFromSession;
}