2014-03-12 53 views
0

美好的一天每個人。使用組合框搜索數據庫,如果某個項目在數據庫中,則會出現提示

我正在製作POS和庫存管理系統,到目前爲止我對這個特定模塊有問題。

將項目添加到採購訂單列表中時,如果項目已在採購訂單數據庫中,系統將提示已有掛單。我已經做了提示,但添加到數據庫是有點亂了。它根本沒有做任何事情。當我刪除ds.hasrows和dr.read條件時,代碼在工作。這是我的代碼:

Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click 

    Ceiling = CInt(txtCeiling.Text) 
    TotalQuantity = CurrentItemQuantity + CInt(txtPurchaseQty.Text) 

    If TotalQuantity > Ceiling Then 
     MsgBox("Exceeds Ceiling Point.") 
    Else 

     sqlString = "SELECT PRODUCT_ID FROM posinventory.purchaseorder WHERE purchaseorder.PRODUCT_ID = '" & cboProductID.Text & "'" 

     cmd = New MySqlCommand(sqlString, con) 
     dr = cmd.ExecuteReader 

     If dr.HasRows Then 
      While dr.Read 

       If CurrentItem = dr.Item("PRODUCT_ID") Then 

        MsgBox("Product has pending order.") 
        cboProductID.Focus() 

       Else 

        sqlString = "INSERT INTO posinventory.purchaseorder (PRODUCT_ID, PURCHASE_QUANTITY, DATE_PURCHASED, TIME_PURCHASED) VALUES (" & cboProductID.Text & ", '" & txtPurchaseQty.Text & "', '" & txtDate.Text & "', '" & txtTime.Text & "')" 

        Try 
         cmd = New MySqlCommand(sqlString, con) 
         dr = cmd.ExecuteReader 
         dr.Close() 
        Catch ex As Exception 
         MsgBox("Error saving to database. Error is: " & ex.Message) 
         Exit Sub 
        End Try 

        MsgBox("Transaction Complete.") 

        lvOrderList.Items.Clear() 

        sqlString = "SELECT posinventory.purchaseorder.TRANSACTION_ID, posinventory.products.PRODUCT_ID, posinventory.products.PRODUCT_NAME, posinventory.products.SUPPLIER_NAME, posinventory.purchaseorder.PURCHASE_QUANTITY, posinventory.purchaseorder.DATE_PURCHASED, posinventory.purchaseorder.TIME_PURCHASED FROM posinventory.purchaseorder, posinventory.products WHERE posinventory.purchaseorder.PRODUCT_ID = posinventory.products.PRODUCT_ID" 
        cmd = New MySqlCommand(sqlString, con) 
        da = New MySqlDataAdapter(cmd) 
        ds = New DataSet 
        da.Fill(ds, "Table") 

        Dim i As Integer = 0 
        Dim j As Integer = 0 

        For i = 0 To ds.Tables(0).Rows.Count - 1 
         For j = 0 To ds.Tables(0).Columns.Count - 1 
          itemcol(j) = ds.Tables(0).Rows(i)(j).ToString() 
         Next 

         Dim lvi As New ListViewItem(itemcol) 
         Me.lvOrderList.Items.Add(lvi) 
        Next 

        grpCreateOrder.Enabled = False 
        grpOrderList.Enabled = True 
        cboProductID.SelectedIndex = -1 
        txtPurchaseQty.Text = "" 
        txtDate.Text = "" 
        txtTime.Text = "" 
        txtProductName.Text = "" 
        txtSupplier.Text = "" 
        txtQty.Text = "" 
        txtCeiling.Text = "" 
        btnBack.Enabled = True 

       End If 

      End While 

     End If 

     dr.Close() 

    End If 

End Sub 

回答

0

我認爲它是因爲你在使用cmd和dr的循環中。當你在那裏時,你正在定義一個新的cmd和dr。

嘗試使用不同的名稱,例如cmd2,dr2。

相關問題