Database value in MsgBox

Help to print database value in a msgbix

Page 1 of 1

9 Replies - 5393 Views - Last Post: 31 October 2010 - 01:28 AM Rate Topic: -----

#1 Nickless  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-October 10

Database value in MsgBox

Posted 30 October 2010 - 03:29 AM

Hi.

Have been struggling to write a database into a msgbox.
This is the code:

Dim login = Me.UsersTableAdapter1.Login(User.Text, Pass.Text)


        If login Is Nothing Then
            MsgBox("Username or Password is incorrect!", MsgBoxStyle.Critical, "Login Error")
        Else
            Dim Userinformation = Me.UsersTableAdapter1.GetUserInfo(User.Text, Pass.Text)

            MsgBox("Welcome " & Userinformation, MsgBoxStyle.Information, "Access Granted")



The database is a MS Access-file. The first part where I check that the user exists and that the password/username is correct works.
But I want to write Welcome "firstname of the user".

All the information should be in the Userinformation variable. But how can I get the firstname value?

This post has been edited by Nickless: 30 October 2010 - 03:31 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Database value in MsgBox

#2 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Database value in MsgBox

Posted 30 October 2010 - 06:31 AM

Try:
MsgBox("Welcome " & User.Text, MsgBoxStyle.Information, "Access Granted")


Not sure what Userinformation is at this point but if it's successful, then User.Text should be the correct user anyway.
Was This Post Helpful? 0
  • +
  • -

#3 Nickless  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-October 10

Re: Database value in MsgBox

Posted 30 October 2010 - 08:18 AM

View PostCharlieMay, on 30 October 2010 - 05:31 AM, said:

Try:
MsgBox("Welcome " & User.Text, MsgBoxStyle.Information, "Access Granted")


Not sure what Userinformation is at this point but if it's successful, then User.Text should be the correct user anyway.


Thanks for your reply.

But User.text is the username the user types in when he/she logs in. User.Pass is the password.
What I want is the FirstName column of the Users-table where the username is User.text
Is there anyway to do it?
Was This Post Helpful? 0
  • +
  • -

#4 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Database value in MsgBox

Posted 30 October 2010 - 09:33 AM

I assume UserInformation is a class that stores all this information? Is that correct. If so, is there a property that holds the FirstName (something like: FirstName). If so, then you would use:

Quote

MsgBox("Welcome " & UserInformation.FirstName, MsgBoxStyle.Information, "Access Granted")


Note to Moderators, I left the code outside a code window to allow for the bold text

This post has been edited by CharlieMay: 30 October 2010 - 09:34 AM

Was This Post Helpful? 0
  • +
  • -

#5 Nickless  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-October 10

Re: Database value in MsgBox

Posted 30 October 2010 - 10:26 AM

Can't UserInformation.FirstName :S Can use UserInformation.FirstNameColumn, but that only returns the name of the column, not the value. If I write UserInformation.FirstName I get the error:

'FirstName' is not a member of 'WindowsApplication1.UsersDataSet.UsersDataTable'.

The UserDataSet is some preset functions that shall return the value. In side the UserDataSet I Have a function called GetUserInfo(Username, Password).

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim login = LoginChecker.LoginCheck(Username.Text, Password.Text)

        If login Is Nothing Then
            MsgBox("Invalid Username or Password", MsgBoxStyle.Critical, "Access Denied")
        Else
            Dim DataTable As DataTable
            Dim Userinformation = LoginChecker.GetUserInfo(Username.Text, Password.Text)
            DataTable = LoginChecker.GetUserInfo(Username.Text, Password.Text)

            MsgBox(Userinformation.FirstName, MsgBoxStyle.Information, "Access Granted")



The first part:

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim login = LoginChecker.LoginCheck(Username.Text, Password.Text)




returns 1 if the login is valid, this part works. But as soon as I try the other part(GetUserInfo)which will return FirstName,LastName,Address,Phone and so on I get problems. Don't know how to get does attributes out :S

Hope you understand the problem :)
Was This Post Helpful? 0
  • +
  • -

#6 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Database value in MsgBox

Posted 30 October 2010 - 12:16 PM

show the code for LoginCheck and GetUserInfo
Was This Post Helpful? 0
  • +
  • -

#7 Nickless  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-October 10

Re: Database value in MsgBox

Posted 30 October 2010 - 12:56 PM

LoginCheck and GetUserInfo are functions created in UserDataSet and when I doble click on them they are empty functions.

Partial Class UsersDataSet
    Partial Class UsersDataTable

    End Class

End Class


Partial Class UsersDataSet
    Partial Class UsersDataTable

    End Class

End Class



If I Right click on them and choose configure I get the following:


#####This is the GetUserInfo ####
SELECT        FirstName
FROM            Users
WHERE        (Username = ?) AND ([Password] = ?)

#####This is the LoginCheck ####
SELECT        COUNT(*) AS Result, Username, [Password]
FROM            Users
GROUP BY Username, [Password]
HAVING        (Username = ?) AND ([Password] = ?) AND (COUNT(*) = 1)




But I found a solution that works:


    Public Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        Dim login = LoginChecker.LoginCheck(Username.Text, Password.Text)
        If login Is Nothing Then
            MsgBox("Invalid Username or Password", MsgBoxStyle.Critical, "Access Denied")
        Else
            Dim cN As OleDbConnection
            Dim myDataReader As OleDbDataReader
            Dim myOleDbCommand As OleDbCommand
            Dim DataTable As DataTable
            Dim Userinformation = LoginChecker.GetUserInfo(Username.Text, Password.Text)
            DataTable = LoginChecker.GetUserInfo(Username.Text, Password.Text)

            Dim user As String = Username.Text
            cN = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source= Path to Access-file")
            myOleDbCommand = New OleDbCommand("SELECT FirstName FROM Users WHERE username ='" + user + "'", cN)
            cN.Open()

            myDataReader = myOleDbCommand.ExecuteReader()

            myDataReader.Read()
            MsgBox(myDataReader.GetString(0), MsgBoxStyle.Information, "Access Granted")

        End If

    End Sub



Do I have to do it like this, or is there another way? :)

Thank you for your help so far =)
Was This Post Helpful? 0
  • +
  • -

#8 CharlieMay  Icon User is offline

  • This space intentionally left blank
  • member icon

Reputation: 1605
  • View blog
  • Posts: 5,162
  • Joined: 25-September 09

Re: Database value in MsgBox

Posted 30 October 2010 - 02:11 PM

Well, that would work, but I agree, it appears from the Query that GetUserInfo would return the FirstName. What does UserInformation actually contain when you tried it in your original code?
Was This Post Helpful? 0
  • +
  • -

#9 AdamSpeight2008  Icon User is offline

  • MrCupOfT
  • member icon


Reputation: 2262
  • View blog
  • Posts: 9,462
  • Joined: 29-May 08

Re: Database value in MsgBox

Posted 30 October 2010 - 02:41 PM

Be careful.

Nothing is not the same as 0 (Zero). Nothing means default instance of the value type (Generally this 0 but not always). For reference type is Nothing means it has no reference.

Dim login = LoginChecker.LoginCheck(Username.Text, Password.Text)
 If login <> 1 Then 
 Else
 EndIf



As I notice that LoginChecker.LoginCheck(Username.Text, Password.Text) looks to attempting to return two different types Integer or DataTable. vb.net doesn't allow method overload with the same type signature to differ only on return types.
Was This Post Helpful? 0
  • +
  • -

#10 Nickless  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-October 10

Re: Database value in MsgBox

Posted 31 October 2010 - 01:28 AM

View PostCharlieMay, on 30 October 2010 - 01:11 PM, said:

Well, that would work, but I agree, it appears from the Query that GetUserInfo would return the FirstName. What does UserInformation actually contain when you tried it in your original code?


As far as I can remember it didn't contain any values. I could only write stuff like:
UserInformation.FirstNameColumn, USerInformation.Select() <--- Tryed to put FirstName in as a parameter but no luck.

And Userinformation.FirstNameColumn just returned FirstName. So it appears that it got contact since it knows the name of the column, but didn't manager to write any values.

AdamSpeight2008 said:

Be careful.

Nothing is not the same as 0 (Zero). Nothing means default instance of the value type (Generally this 0 but not always). For reference type is Nothing means it has no reference.

view source
print?
1 Dim login = LoginChecker.LoginCheck(Username.Text, Password.Text)
2 If login <> 1 Then
3 Else
4 EndIf


As I notice that LoginChecker.LoginCheck(Username.Text, Password.Text) looks to attempting to return two different types Integer or DataTable. vb.net doesn't allow method overload with the same type signature to differ only on return types.


Thanks for the heads up :) Will change the code on that part.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1