How to get rid of Conversion from string "admin" to type 'Double' is not valid

时间:2016-02-28 10:14:41

标签: sql-server vb.net

Hello guys am having issues with my login form for multiple users of my software, below is the complete login button code for the admin

Private Sub btnlogin_Click(sender As System.Object, e As System.EventArgs) Handles btnlogin.Click
    If cmbusertype.Text = "" Then
        MsgBox("Please select the appropriate Account Type!", vbExclamation, "Account Type")
    End If
    If cmbusertype.Text = "Admin" Then
        Dim Strconn As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\phermacy.mdf;Integrated Security=True;User Instance=True"
        Dim Selectcmd As String = "select * from tbl_admin where admin_username = '" + txtusername.Text + "' and password = '" + txtpassword.Text + "'"
        Dim da As New SqlDataAdapter
        Dim ds As New DataSet
        Dim sqlcmd As SqlCommand
        sqlconn = New SqlConnection(Strconn)
        Try
            sqlconn.Open()
        Catch ex As Exception
            MsgBox("Could not connect to DataBase. Application will close now!", vbCritical, "Database Error")
            End
        End Try
        sqlcmd = New SqlCommand(Selectcmd, sqlconn)
        da.SelectCommand = sqlcmd
        sqlcmd.Dispose()
        sqlconn.Close()
        da.Fill(ds)

Matching Admin User Name & Password

 If ds.Tables(0).Rows.Count > 0 Then

                    If txtusername.Text = ds.Tables(0).Rows(0).Item(0) And txtpassword.Text = ds.Tables(0).Rows(0).Item(1) Then
                        MsgBox("Administrator Log-in Successful.", vbInformation, "Admin Log-in")
                        Me.Hide()
                        adminform.Show()

                    Else
                        ErrorProvider1.SetError(txtusername, "Invalid User Name.")
                        ErrorProvider1.SetError(txtpassword, "Invalid Password.")
                        MsgBox("Invalid Administrator Username or Password.", vbCritical, "Admin Log-in")
                    End If

                Else
                    ErrorProvider1.SetError(txtusername, "Invalid User name or Password.")
                    ErrorProvider1.SetError(txtpassword, "Invalid User name or Password.")
                    ErrorProvider1.SetError(cmbusertype, "Please select the appropriate Account Type")
                    MsgBox("Invalid Administrator Username or Password.", vbCritical, "Admin Log-in")
                End If
            End If
        End Sub

the code line highlighted is the following

        If txtusername.Text = ds.Tables(0).Rows(0).Item(0) And txtpassword.Text = ds.Tables(0).Rows(0).Item(1) Then

and the error is "Conversion from string "admin" to type 'Double' is not valid." am actually new to vb trying to create a login for multiple users, the above part as you can see is just for the admin. Please any help here will be appreciated thanks.

1 个答案:

答案 0 :(得分:-1)

试试这个......

   Private Sub btnlogin_Click(sender As System.Object, e As System.EventArgs) Handles btnlogin.Click
    If cmbusertype.Text = "" Then
        MsgBox("Please select the appropriate Account Type!", vbExclamation, "Account Type")
    End If
    If cmbusertype.Text = "Admin" Then
        Dim Strconn As String = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\phermacy.mdf;Integrated Security=True;User Instance=True"
        Dim Selectcmd As String = "select * from tbl_admin where admin_username = @usernme and password = @paswd"
        Dim da As New SqlDataAdapter
        Dim ds As New DataSet
        Dim sqlcmd As SqlCommand
        sqlconn = New SqlConnection(Strconn)
        Try
            sqlconn.Open()
        Catch ex As Exception
            MsgBox("Could not connect to DataBase. Application will close now!", vbCritical, "Database Error")
            End
        End Try
        sqlcmd = New SqlCommand(Selectcmd, sqlconn)
        sqlcmd.parameters.addwithValue("@usernme",txtusername.Text)
        sqlcmd.parameters.addwithValue("@paswd",txtpassword.Text)
        da.SelectCommand = sqlcmd
        sqlcmd.Dispose()
        sqlconn.Close()
        da.Fill(ds)