Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hi I'm new to vb.net.I'm having error input string is not in the correct format. I already used different codes for adding a parameter in sql select query.

I would like to ask what is the best way of adding a parameter in a VB.net select query?

Thanks in advance.

VB
Dim strsql As String = "Select * from [users] where [userid]=@uid and [password]=@pword"


SQLCOn = New SqlConnection(Constring)

        Try
            SQLCOn.Open()
            Dim cmd As New SqlCommand(strsql, SQLCOn)

            1 cmd.Parameters.AddWithValue("@uid", UsernameTextBox.Text)
              cmd.Parameters.AddWithValue("@pword", PasswordTextBox.Text)


            2 cmd.Parameters.Add(New SqlParameter("@uid", UsernameTextBox.Text))
              cmd.Parameters.Add(New SqlParameter("@pword", PasswordTextBox.Text))

            3 cmd.Parameters.Add("@uid", SqlDbType.VarChar).Value = UsernameTextBox.Text
              cmd.Parameters.Add("@pword", SqlDbType.VarChar).Value = PasswordTextBox.Text


            4 cmd.Parameters("@uid").Value = UsernameTextBox.Text
              cmd.Parameters("@pword").Value = PasswordTextBox.Text

            'count = Convert.ToInt32(cmd.ExecuteScalar())

            If Convert.ToInt32(cmd.ExecuteScalar()) >= 1 Then
                MsgBox("Login success", vbInformation)
                Me.Hide()
            Else
                MsgBox("Invalid login", vbExclamation)
            End If

        Catch ex As Exception
            MsgBox(ex.ToString, vbExclamation)
        End Try
Posted
Comments
[no name] 12-May-13 7:59am    
Both 1 and 3 should work.

1 solution

My mistake. I should have used.
"Select count(*) from [users] where [userid]=@uid and [password]=@pword"

since I used
Convert.ToInt32(cmd.ExecuteScalar())>= 1
which requires numeric result.

1, 2 and 3 worked after I fixed my error in my select query.
 
Share this answer
 
v2
Comments
Maciej Los 12-May-13 15:24pm    
Self solved ;)
+5

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900