VB SQL插入查询为所有值插入NULL

时间:2015-07-24 14:54:45

标签: asp.net vb.net sql-insert sqldatasource

我正在Visual Web Developer中开发一个asp.net应用程序,我正在尝试在数据库中插入一个新行。我目前有一个" Insert" GridView的页脚模板中的行。页脚始终可见,我不确定这是否与我的插入查询无效有关。

我的插入查询是在Gridview RowCommand事件中调用的,我在asp.net端有插入命令文本,因此在RowCommand事件上,我只是声明了Command的参数。当我在事件中调试并逐步执行每行代码时,所有参数都接收到正确的值,但在插入之后,当我查看数据库时,整行是NULL。

我无法弄清楚为什么它实际上并没有插入参数中的值,但这可能只是我忽略的一个愚蠢的错误。这是行命令代码

Protected Sub GridView1_RowCommand(sender As Object, e As System.Web.UI.WebControls.GridViewCommandEventArgs) Handles GridView1.RowCommand

    If e.CommandName.Equals("Insert") Then
        Dim oItem As GridViewRow = DirectCast(DirectCast(e.CommandSource, Button).NamingContainer, GridViewRow)
        Dim DateTxt As String = DirectCast(GridView1.FooterRow.FindControl("DateTxt"), TextBox).Text
        Dim First As String = DirectCast(GridView1.FooterRow.FindControl("FirstTxt"), TextBox).Text
        Dim Last As String = DirectCast(GridView1.FooterRow.FindControl("LastTxt"), TextBox).Text
        Dim Product As String = DirectCast(GridView1.FooterRow.FindControl("ProductTxt"), TextBox).Text
        Dim Description As String = DirectCast(GridView1.FooterRow.FindControl("DescriptionTxt"), TextBox).Text
        Dim Quantity As String = DirectCast(GridView1.FooterRow.FindControl("QuantityTxt"), TextBox).Text
        Dim Phone As String = DirectCast(GridView1.FooterRow.FindControl("PhoneTxt"), TextBox).Text
        Dim Email As String = DirectCast(GridView1.FooterRow.FindControl("EmailTxt"), TextBox).Text
        Dim Price As String = DirectCast(GridView1.FooterRow.FindControl("PriceTxt"), TextBox).Text
        Dim Urgency As String = DirectCast(GridView1.FooterRow.FindControl("UrgencyTxt"), TextBox).Text
        Dim Freight As String = DirectCast(GridView1.FooterRow.FindControl("FreightTxt"), TextBox).Text
        Dim PO As String = DirectCast(GridView1.FooterRow.FindControl("POTxt"), TextBox).Text
        Dim Vendor As String = DirectCast(GridView1.FooterRow.FindControl("VendorTxt"), TextBox).Text
        Dim Notes As String = DirectCast(GridView1.FooterRow.FindControl("NotesTxt"), TextBox).Text
        Dim PickupCustomers As String = DirectCast(GridView1.FooterRow.FindControl("PickupCustomerTxt"), TextBox).Text
        'Dim ID As String = GridView1.Rows(oItem.RowIndex).Cells(0).Text

        SqlDataSource1.InsertParameters.Add("@Date", DateTxt)
        SqlDataSource1.InsertParameters.Add("@First", First)
        SqlDataSource1.InsertParameters.Add("@Last", Last)
        SqlDataSource1.InsertParameters.Add("@Product", Product)
        SqlDataSource1.InsertParameters.Add("@Description", Description)
        SqlDataSource1.InsertParameters.Add("@Quantity", Quantity)
        SqlDataSource1.InsertParameters.Add("@Phone", Phone)
        SqlDataSource1.InsertParameters.Add("@Email", Email)
        SqlDataSource1.InsertParameters.Add("@Price", Price)
        SqlDataSource1.InsertParameters.Add("@Urgency", Urgency)
        SqlDataSource1.InsertParameters.Add("@Freight", Freight)
        SqlDataSource1.InsertParameters.Add("@PO", PO)
        SqlDataSource1.InsertParameters.Add("@Vendor", Vendor)
        SqlDataSource1.InsertParameters.Add("@Notes", Notes)
        SqlDataSource1.InsertParameters.Add("@PickupCustomers", PickupCustomers)
        'SqlDataSource1.InsertParameters.Add("@ID", ID)
        SqlDataSource1.Insert()
        SqlDataSource1.DataBind()
        GridView1.DataBind()
    End If
End Sub

这是asp.net sqldatasource插入查询

 InsertCommand="INSERT INTO Special_Orders(Date, First, Last, Product, Description, Quantity, Phone, Email, Price, Urgency, Freight, PO, Vendor, Notes, Ordered, Received, PickedUp, PickupCustomer) VALUES (@Date, @First, @Last, @Product , @Description, @Quantity, @Phone, @Email, @Price, @Urgency, @Freight, @PO, @Vendor , @Notes, 0, 0, 0, @PickupCustomer)">
    <InsertParameters>
        <asp:Parameter Name="Date" />
        <asp:Parameter Name="First" />
        <asp:Parameter Name="Last" />
        <asp:Parameter Name="Product" />
        <asp:Parameter Name="Description" />
        <asp:Parameter Name="Quantity" />
        <asp:Parameter Name="Phone" />
        <asp:Parameter Name="Email" />
        <asp:Parameter Name="Price" />
        <asp:Parameter Name="Urgency" />
        <asp:Parameter Name="Freight" />
        <asp:Parameter Name="PO" />
        <asp:Parameter Name="Vendor" />
        <asp:Parameter Name="Notes" />
        <asp:Parameter Name="PickupCustomer" />
    </InsertParameters>

1 个答案:

答案 0 :(得分:0)

我找到了错误。在我的代码背后,我错误地输入了PickupCustomer参数。因为我的查询文本是在asp.net端,虽然它没有抛出任何错误。一旦我以编程方式在其后面的代码中添加了查询,就会产生错误。