2014-02-16 65 views
0

所以我有2個MySQL表,一個名爲「Service_Details」和一個叫「Payment_details」填充與MySQL數據的文本框的組合框選擇後

我在形式的組合框從顯示「service_id爲」場服務表。 我想編碼一個文本框,所以當我從組合框中選擇服務ID時,它會寫入「服務」,這是服務詳細信息表中的另一個字段。服務ID鏈接到服務。

我得到的錯誤「標識應爲」在[0]和system.data.datatablecollection在dss.tables‘不能轉換爲‘字符串’「的類型值’

我似乎無法得到它瀏覽互聯網一小時

這裏工作後,是我的代碼:

Private Sub cbxserviceid_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles cbxserviceid.SelectedIndexChanged 
    Dim dss As New DataSet 
    Dim daa As New MySqlDataAdapter("SELECT * from service_details WHERE ServiceID='" + cbxserviceid.Text + "'", objconnection) 
    Dim cmddd = New MySqlCommandBuilder(daa) 
    daa.Fill(dss) 
    txtService1.Text = dss.Tables[0].Rows[0]["Service"].ToString(); 
End Sub 

回答

0

在VB.NET的索引使用圓括號表示,方括號在C#中使用

txtService1.Text = dss.Tables(0).Rows(0)("Service").ToString() 

此外,還要避免使用的SQL命令文本字符串連接,始終使用參數化查詢

Private Sub cbxserviceid_SelectedIndexChanged(......) 
    Dim dss As New DataSet 
    if cbxserviceid.SelectedIndex <> -1 Then 
     Dim daa As New MySqlDataAdapter("SELECT * from service_details WHERE [email protected]", _ 
             objconnection) 
     daa.SelectCommand.Parameters.AddWithValue("@id", Convert.ToInt32(cbxserviceid.Text)) 
     daa.Fill(dss) 
     txtService1.Text = dss.Tables(0).Rows(0)("Service").ToString() 
    End If 
End Sub 

我也刪除的MySqlCommandBuilder的創作,是適配器它沒有任何意義或影響(局部變量如果這當然是你所有的代碼)。

看下面的評論和海報聊天還有另一個錯誤來解決。 時指定數據源,組合框的DisplayMember和ValueMember屬性是強制性遵循一個精確的順序,以避免不必要的副作用

cbxserviceid.DisplayMember = "ServiceID" 
cbxserviceid.ValueMember = "ServiceID" 
cbxserviceid.DataSource = datatable 
+0

我得到命令執行過程中遇到錯誤致命錯誤。 'daa.fill(dss)'? ? – Livaren

+0

Steve有什麼想法? – Livaren

+0

對不起,我沒有注意到你的第一個評論。字段ServiceID是MySql中的字符串(文本)還是整數? – Steve

相關問題