2011-04-26 38 views
0

我有一個組合框和文本框的表單。
現在我有一個名爲balance的表的SQL數據庫。它有兩列,一列是customername,另一列是obbalance
現在我已將所有客戶名稱綁定到組合框。
我需要做的是:
當用戶從組合框中選擇一個客戶名稱時,文本框應顯示所選customernameobbalance。這裏的客戶名稱不會重複,只有一個客戶的名字。
我該怎麼辦?請幫幫我。如何使用datareader從combobox selecteditem中獲取數據到文本框

Dim conectionstring As String 
    conectionstring = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\SHOPPROJECT\SHOPPROJECT\shop.mdf;Integrated Security=True;User Instance=True" 


    Dim ST As String = ComboBox1.SelectedText 

    Dim sqlcon As New SqlConnection(conectionstring) 

    Dim sqlcmd As New SqlCommand("SELECT OBBALANCE FROM BALANCE WHERE CUSTOMERNAME = " & " '" & ST & "'" & "", sqlcon) 
    MessageBox.Show(TextBox1.Text) 


    Dim result As Object 

    Try 
     sqlcon.Open() 
     ' Dim sdr As SqlDataReader = sqlcmd.ExecuteReader() 
     result = sqlcmd.ExecuteScalar() 

     If result IsNot Nothing Then 
      TextBox1.Text = result.ToString() 
      MessageBox.Show(TextBox1.Text) 

     End If 

    Catch ex As SqlException 
     MessageBox.Show(ex.Message) 

    End Try 
End Sub 

我已經做到了這一點,但仍然我不能在文本框中看到的價值和obbalance是從數據庫SQL一個flost值

回答

0
Private Sub doSomething() 
Dim conectionstring As String 
     conectionstring = "Data Source=.\SQLEXPRESS;AttachDbFilename=C:\Documents and Settings\Administrator\My Documents\Visual Studio 2005\Projects\SHOPPROJECT\SHOPPROJECT\shop.mdf;Integrated Security=True;User Instance=True" 


    Dim ST As String = ComboBox1.SelectedText 

    Dim sqlcon As New SqlConnection(conectionstring) 

    Dim sqlcmd As New SqlCommand("SELECT OBBALANCE, CUSTOMERNAME FROM BALANCE" ,sqlcon) 
    sqlcmd.CommandType = CommandType.Text 

    Try 
     sqlcon.Open() 
     Dim sdr As SqlDataReader = sqlcmd.ExecuteReader() 

     If sdr IsNot Nothing Then 
      Dim dt As DataTable 
      dt.Load(sdr) 

      With yourCombobox 
       .DataSource = dt 
       .DisplayMember = "CUSTOMERNAME" 
       .ValueMember = "OBBALANCE" 

      End With 

     End If 

    Catch ex As SqlException 
     MessageBox.Show(ex.Message) 
    End Try 
End Sub 

Private Sub comboBoxChange(ByVal sender As Object, ByVal e As System.EventArgs) Handles yourCombobox.SelectedIndexChanged 

textbox.text = yourCombobox.SelectedValue.ToString 

End Sub 
+0

我不能明白笏ü沒有 – MUKESH 2011-04-26 19:25:08

+0

我把你的'SqlDataReader'到一個'Datatable'中。然後將'Datatable'綁定到你的組合框。然後在你的'combobox'的'SelectedIndexChanged'事件中,我添加了代碼來抓取'combobox'中的文本並將其放入'textbox'中。 – Jack 2011-04-26 19:33:53

+0

我有一個數據庫表中的客戶名稱和obbalance現在我需要的是如果我選擇組合框的文本框應顯示obbalce從tat表中選擇的客戶名稱的值現在可以瞭解 – MUKESH 2011-04-26 19:48:46

相關問題