嗨im新的Visual Basic。我有一個按鈕,當它被點擊時,它會通過用戶輸入的ID找到學生,並將數據輸出到文本框。我不確定我是否正確地做這件事。因爲我得到這個錯誤[圖像] >>http://img812.imageshack.us/img812/7650/gq0z.png如何從數據庫獲取數據到VB.net上的文本框
btw這是我的代碼到目前爲止。有人能幫助我嗎?謝謝!
cmd.CommandText = "Select * from Student where Student_id = '" & id.Text & "'"
cmd.Connection = db
dr = cmd.ExecuteReader
Try
dr.Read()
id.Text = dr.GetValue(0)
Lname.Text = dr.GetValue(1)
Fname.Text = dr.GetValue(2)
Mname.Text = dr.GetValue(3)
datet.Text = dr.GetValue(4)
age.Text = dr.GetValue(5)
male.Text = dr.GetValue(6)
female.Text = dr.GetValue(7)
status.Text = dr.GetValue(8)
staddress.Text = dr.GetValue(9)
cityAdd.Text = dr.GetValue(10)
dr.Close()
Catch ex As Exception
MsgBox("" + ex.Message)
dr.Close()
End Try
'是空的,你會得到一個錯誤;在構建SQL之前,您還需要將其包含的**文本**轉換爲數字。在未來發布實際的錯誤消息(他們是重要的信息),而不是一個圖片的鏈接 – Plutonix
我會推薦一個:'如果不是isdbnull(dr.getvalue(0))'...每一個明顯增加getvalue每個人的號碼。 – logixologist
未經請求的建議:繼續並將'dr.getvalue(n)'改爲'dr.getvalue(「nameOfColumn」)'。它是一個更好的習慣。 – logixologist