-2

我对这个 asp.net 代码有问题。变量 qty 和 itname 没有得到有效值……谁能找出问题所在?

Imports System.Data
Imports System.Data.SqlClient
Partial Class consolidate
    Inherits System.Web.UI.Page

    Public lastreq_no As Int32

    Protected Sub btnconsolidate_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnconsolidate.Click
        Dim qtypen As Integer
        Dim qtypencil As Integer
        Dim qtygbag As Integer
        Dim qtysugar As Integer
        Dim i As Integer
        Dim req As Integer
        Dim qty As Integer
        Dim itname As String = ""
        Dim sqlcon As New SqlConnection("Data Source=user-hp\sqlexpress;initial catalog=campco;integrated security=true;")
        If sqlcon.State = ConnectionState.Open Then
            sqlcon.Close()
        End If

        sqlcon.Open()
        Dim str As String
        str = "Select Req_no from Requirements "
        Dim cmd As New SqlCommand(str, sqlcon)
        Dim sdr As SqlDataReader
        sdr = cmd.ExecuteReader()
        sdr.Read()
        lastreq_no = sdr.GetInt32(sdr.VisibleFieldCount - 1)
        For i = 0 To sdr.VisibleFieldCount - 1
            req = sdr.GetInt32(i)
            While req > lastreq_no
                Dim selcomnd1 As String
                Dim selcomnd2 As String
                selcomnd1 = "Select @itname=It_name from Requirements where Req_no= @req"
                selcomnd2 = "Select @qty= Quantity from Requirements where Req_no= @req"
                Dim sqlcomnd1 As New SqlCommand(selcomnd1, sqlcon)
                Dim sqlcomnd2 As New SqlCommand(selcomnd2, sqlcon)
                sqlcomnd1.Parameters.AddWithValue("@itname", itname)
                sqlcomnd2.Parameters.AddWithValue("@qty", qty)
                sqlcomnd1.ExecuteScalar()
                sqlcomnd2.ExecuteScalar()
                TextBox1.Text = itname
                TextBox2.Text = qty
                sqlcon.Close()
                sqlcon.Open()
                Select Case (itname)
                    Case "Pen"
                        qtypen += qty
                        lastreq_no = req
                    Case "Pencil"
                        qtypencil += qty
                        lastreq_no = req
                    Case "Gunny bag"
                        qtygbag += qty
                        lastreq_no = req
                    Case "Sugar"
                        qtysugar += qty
                        lastreq_no = req
                End Select
            End While
        Next

        sqlcon.Close()
        If sqlcon.State = ConnectionState.Open Then
            sqlcon.Close()
        End If
        sqlcon.Open()
        Dim comm As String
        comm = "Insert into Consolidate (lastr_no,qtypen,qtypencil,qtygunnybag,qtysugar)values('" + lastreq_no.ToString + "','" + qtypen.ToString + "','" + qtypencil.ToString + "','" + qtygbag.ToString + "','" + qtysugar.ToString + "')"
        Dim sqlcomm As New SqlCommand(comm, sqlcon)
        Dim s As String
        s = sqlcomm.ExecuteNonQuery()
        sqlcon.Close()
    End Sub
End Class
4

2 回答 2

1

首先,标量语句均无效。您是否尝试在 SQL Management Studio 或类似程序中运行这些语句来测试这些语句本身?它们应该是这样的:

selcomnd1 = "Select It_name from Requirements where Req_no=@req"
selcomnd2 = "Select Quantity from Requirements where Req_no=@req"

然后你会以这种方式分配它们:

itname = CType(sqlcmnd1.ExecuteScalar(), String)  ' .ToString() would probably work here as well
qty = Convert.Int32(sqlcmnd2.ExecuteScalar())

或者您可以使用.TryParse数量:

Integer.TryParse(sqlcmnd2.ExecuteScalar(), qty)
于 2013-02-25T13:14:27.287 回答
0

线

sqlcomnd1.Parameters.AddWithValue("@itname", itname)

提供一个值为 的输入参数itname。尚未为该变量分配任何值。

您需要添加一个输出参数:请参阅此处了解如何执行此操作。

在 ADO.NET 中获取输出参数值

于 2013-02-25T13:14:04.650 回答