I got this error when trying to use parameters to my sql statement, but it works fine when not use it. My codes are below:
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
And when I comment on txtUserType.Text = i.ToString, it works fine. Any idea?
ExecuteScalar should only give you one value back, like an integer. So if you specify only one column in your SQL statement for example (Select usertype from tblStaff…) the executescalar should return an integer (if that column is a number).
Then it should work.
and by the way.. you don’t have to use ToString on a variable that is a string. Just use the variables name
txtUserType.Text = i