2011-07-04 45 views
0

當我嘗試將參數用於我的sql語句時出現此錯誤,但在不使用它時正常工作。我的代碼如下所示:vb.net - 對象引用未設置爲對象的實例

Dim i As String 
Dim sql as String 

sql = "SELECT * FROM tblStaff WHERE Username = @User AND Password = @Pass" 
myCommand = New SqlCommand(sql, myConnection) 
myCommand.Parameters.AddWithValue("@User", txtUser.Text) 
myCommand.Parameters.AddWithValue("@Pass", txtPassword.Text) 

i = myCommand.ExecuteScalar 
txtUserType.Text = i.ToString 

當我在txtUserType.Text = i.ToString評論,它工作正常。任何想法?

回答

0

ExecuteScalar應該只給你一個值,像一個整數。因此,如果您在SQL語句中只指定了一列(例如,從tblStaff中選擇usertype ...),executecalar應該返回一個整數(如果該列是一個數字)。

然後它應該工作。

順便說一句..你不必使用ToString作爲字符串的變量。只需使用變量名稱 txtUserType.Text = i

相關問題