2013-02-25 56 views
-2

我有這個asp.net代碼的問題。 變量qty和itname沒有得到有效值......任何人都可以找出問題所在?ASP.NET變量沒有得到分配的值

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 
+0

你在哪裏asigning值到數量變量?我沒有看到任何 – Cris 2013-02-25 13:12:50

+0

而不是發佈這一大堆的代碼,並尋求幫助,我認爲你應該討論你嘗試運行代碼時遇到的問題。還有,像你在selcommand1和selcommand2中寫的那樣寫查詢是有效的。我不這麼認爲。 – 2013-02-25 13:16:38

回答

0

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

提供輸入參數與值itname。沒有值被分配給這個變量。

您需要添加一個輸出參數:請參閱此處瞭解如何執行此操作。

Get output parameter value in ADO.NET

1

要下手,既不標聲明是有效的。您是否試圖在SQL Management Studio或類似程序中運行這些語句來自行測試語句?他們應該是這樣的:

selcomnd1 = "Select It_name from Requirements where [email protected]" 
selcomnd2 = "Select Quantity from Requirements where [email protected]" 

然後您可以將它們分配以這種方式:

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

或者你可以使用.TryParse的數量:

Integer.TryParse(sqlcmnd2.ExecuteScalar(), qty) 
+0

Yap,這是我熟悉的那種sql查詢。查詢應返回數據庫內的縮放器值。 – 2013-02-25 13:20:04