5 Replies - 1642 Views - Last Post: 01 March 2011 - 04:26 AM Rate Topic: -----

#1 sephsta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 01-March 11

Conversion from string "Admin" to type 'Double' is not

Posted 01 March 2011 - 02:31 AM

Hey all, I'm having an issue with 'access levels' The database is connected and my user levels are set up, Admin and User, I'm trying to set up so when you log in it checks if the username falls under an admin in the database. but I seem to be getting this Conversion from string "Admin" to type 'Double' is not valid error, Please help



 Public Class frmlogin

    Private Sub btn_login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_login.Click
        Dim AccessLevel = Me.UsersTableAdapter1.ValidateUsernamePassword(Me.txt_Username.Text, Me.txt_Password.Text)
        If AccessLevel Is Nothing Then
            'username / password wrong
            MessageBox.Show("Please enter a valid username or password")
        Else
            If AccessLevel = "Admin" Then

                frm_main.Show()
                Me.Hide()
            Else
                If AccessLevel = "User" Then

                    frm_usermain.show()
                    Me.Hide()
                End If
            End If
        End If



    End Sub 

This post has been edited by sephsta: 01 March 2011 - 02:32 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Conversion from string "Admin" to type 'Double' is not

#2 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Conversion from string "Admin" to type 'Double' is not

Posted 01 March 2011 - 02:55 AM

Seems your ValidateUsernamePassword method is returning a double, instead of the string you are expecting. What kind of query is it?

Been a long time since I used VB.Net, but you could try to typecast it to a string.
Dim AccessLevel As String
AccessLevel = CType(Me.UsersTableAdapter1.ValidateUsernamePassword(Me.txt_Username.Text, Me.txt_Password.Text), String);


Was This Post Helpful? 1
  • +
  • -

#3 sephsta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 01-March 11

Re: Conversion from string "Admin" to type 'Double' is not

Posted 01 March 2011 - 03:12 AM

 Public Class frmlogin

    Private Sub btn_login_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_login.Click

        Dim AccessLevel As String
        AccessLevel = CType(Me.UsersTableAdapter1.ValidateUsernamePassword(Me.txt_Username.Text, Me.txt_Password.Text), String)

        If AccessLevel Is Nothing Then
            'username / password wrong
            MessageBox.Show("Please enter a valid username or password")
        Else
            If AccessLevel = "Admin" Then

                frm_main.Show()
                Me.Hide()
            Else
                If AccessLevel = "User" Then

                    frm_usermain.Show()
                    Me.Hide()
                End If
            End If
        End If



    End Sub 


Hey thanks very much, it fixed the double issue but now I can't login, I write in the username and password and click login and it doesn't do anything however if I enter invalid details it'll provide my error message so it is connected to the database :/
Was This Post Helpful? 0
  • +
  • -

#4 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Conversion from string "Admin" to type 'Double' is not

Posted 01 March 2011 - 03:38 AM

That's probably because there is a hole in the logic that checks the AccessLevel. Imagine that your AccessLevel is neither "Admin", "User" nor Nothing and follow where the code goes. -- Nowhere :)

I'd do something more like:
If AccessLevel = "Admin" Then
    frm_main.Show()
    Me.Hide()
ElseIf AccessLevel = "User" Then
    frm_usermain.Show()
    Me.Hide()
ElseIf AccessLevel Is Nothing Then
    MessageBox.Show("Please enter a valid username or password")
Else
    MessageBox.Show("Invalid AccessLevel: " & AccessLevel)
End If


This way all paths lead to somewhere, and you default to an error. (Always better to default to errors, in case something happens you didn't expect.)

This post has been edited by Atli: 01 March 2011 - 03:40 AM

Was This Post Helpful? 0
  • +
  • -

#5 sephsta  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 01-March 11

Re: Conversion from string "Admin" to type 'Double' is not

Posted 01 March 2011 - 04:12 AM

Anything I enter in the field comes up with an error

"Invalid access level:1"

Urghh :/

        Dim AccessLevel As String
        AccessLevel = CType(Me.UsersTableAdapter1.ValidateUsernamePassword(Me.txt_Username.Text, Me.txt_Password.Text), String)

        If AccessLevel = "Admin" Then

            frm_main.Show()

            Me.Hide()

        ElseIf AccessLevel = "User" Then

            frm_usermain.Show()

            Me.Hide()

        ElseIf AccessLevel Is Nothing Then

            MessageBox.Show("Please enter a valid username or password")

        Else

            MessageBox.Show("Invalid AccessLevel: " & AccessLevel)

        End If

Was This Post Helpful? 0
  • +
  • -

#6 Atli  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 3719
  • View blog
  • Posts: 5,991
  • Joined: 08-June 10

Re: Conversion from string "Admin" to type 'Double' is not

Posted 01 March 2011 - 04:26 AM

Ok, I see. The typecast is only masking the real problem then, which is that your ValidateUsernamePassword method isn't actually returning a string you can check for your admin value. (I'm guessing it's returning a row count number, or something in that direction.)

You need to examine the query that the method is executing. From a brief Googling I see that TableAdapter queries usually return a DataTable, fill an existing DataTable or return a scalar value. (You'd want the latter for your code.)
This article describes how the different types of queries work.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1