Results 1 to 28 of 28

Thread: [RESOLVED] [2008] check to see if select statement finds a result

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Resolved [RESOLVED] [2008] check to see if select statement finds a result

    i am kinda new to sql programing. just saying that to get it out of the way. i know how to connect to a database but i don't know how to check to see if a result is received from a select statement. this is my select statement

    Code:
    da.SelectCommand = New OleDbCommand("SELECT * FROM userinfo WHERE username =" & UsernameTextBox.Text)
    i need to know if there is a user with that username and if there is then i will need to read the password for that person. then if both username and password are correct then i will show form 2. any help is welcome.

  2. #2
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [2008] check to see if select statement finds a result

    you can use

    Code:
     Dim dtUser As New DataTable("User")
          
            Dim oldAdap As New OleDataAdapter("SELECT * FROM userinfo WHERE username =" & UsernameTextBox.Text), objConn) 'objConn is connection object
            oleAdap.Fill(dtUser )
      
          If dtUser.rows.count = 1 then
            'here is you code that will check password and other thing.
      else
             messagebox.show("Invalid user name")
        End if
    PS: Better use parameterised query.
    __________________
    Rate the posts that helped you

  3. #3
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [2008] check to see if select statement finds a result

    Fill the DataTable, and then take a count of rows.

    Dim dt As New DataTable
    da.Fill(dt)
    dt.Rows.Count
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  4. #4

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] check to see if select statement finds a result

    trying code will get back to you soon

  5. #5
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: [2008] check to see if select statement finds a result

    Using a dataadapter is overkill, using a command's executescalar method should work faster.

  6. #6
    Fanatic Member Clanguage's Avatar
    Join Date
    Jan 2008
    Location
    North Carolina
    Posts
    659

    Re: [2008] check to see if select statement finds a result

    Quote Originally Posted by wild_bill
    Using a dataadapter is overkill, using a command's executescalar method should work faster.
    I would have to agree with wild__bill
    CLanguage;
    IF Post = HelpFull Then
    RateMe
    Else
    Say("Shut UP")
    End If
    DotNet rocks
    VB 6, VB.Net 2003, 2005, 2008, 2010, SQL 2005, WM 5.0,ahem ?OpenRoad?

  7. #7

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] check to see if select statement finds a result

    how would i do that? i would not know where to start.

  8. #8

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] check to see if select statement finds a result

    Quote Originally Posted by riteshjain1982
    you can use

    Code:
     Dim dtUser As New DataTable("User")
          
            Dim oldAdap As New OleDataAdapter("SELECT * FROM userinfo WHERE username =" & UsernameTextBox.Text), objConn) 'objConn is connection object
            oleAdap.Fill(dtUser )
      
          If dtUser.rows.count = 1 then
            'here is you code that will check password and other thing.
      else
             messagebox.show("Invalid user name")
        End if
    PS: Better use parameterised query.
    if i used your code it would only work if there is one entry ( i could change that to a for loop but not atm) i would also need an way to check for the a password and the way you have it set up i would have to have a copy of a lookup table on the client side. which would have to be updated every time there was a new member. so your way is not very useful to me bu thank you for reponding so quickly

  9. #9
    Fanatic Member Clanguage's Avatar
    Join Date
    Jan 2008
    Location
    North Carolina
    Posts
    659

    Re: [2008] check to see if select statement finds a result

    Quote Originally Posted by bagstoper
    Code:
    da.SelectCommand = New OleDbCommand("SELECT * FROM userinfo WHERE username =" & UsernameTextBox.Text)
    i need to know if there is a user with that username and if there is then i will need to read the password for that person. then if both username and password are correct then i will show form 2. any help is welcome.
    If you do the following you should only return one record.
    Code:
    SELECT Password FROM userinfo WHERE username =" & UsernameTextBox.Text
    Create a command object and use the executescalar method
    CLanguage;
    IF Post = HelpFull Then
    RateMe
    Else
    Say("Shut UP")
    End If
    DotNet rocks
    VB 6, VB.Net 2003, 2005, 2008, 2010, SQL 2005, WM 5.0,ahem ?OpenRoad?

  10. #10
    Fanatic Member Clanguage's Avatar
    Join Date
    Jan 2008
    Location
    North Carolina
    Posts
    659

    Re: [2008] check to see if select statement finds a result

    Note that if the user does not exist you not return a record but if the user exists then you will return the password
    CLanguage;
    IF Post = HelpFull Then
    RateMe
    Else
    Say("Shut UP")
    End If
    DotNet rocks
    VB 6, VB.Net 2003, 2005, 2008, 2010, SQL 2005, WM 5.0,ahem ?OpenRoad?

  11. #11
    Fanatic Member Clanguage's Avatar
    Join Date
    Jan 2008
    Location
    North Carolina
    Posts
    659

    Re: [2008] check to see if select statement finds a result

    If you have trouble with the command object there is a sample from today on our forum. Post #9 will be of interest to you.
    http://www.vbforums.com/showthread.p...command+object
    CLanguage;
    IF Post = HelpFull Then
    RateMe
    Else
    Say("Shut UP")
    End If
    DotNet rocks
    VB 6, VB.Net 2003, 2005, 2008, 2010, SQL 2005, WM 5.0,ahem ?OpenRoad?

  12. #12
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [2008] check to see if select statement finds a result

    Quote Originally Posted by bagstoper
    if i used your code it would only work if there is one entry ( i could change that to a for loop but not atm)
    Is your table stores Multiple user for same username

    Quote Originally Posted by bagstoper
    i would also need an way to check for the a password
    you can use dtUser.rows(0).Item("Password")

    Quote Originally Posted by bagstoper
    the way you have it set up i would have to have a copy of a lookup table on the client side. which would have to be updated every time there was a new member. so your way is not very useful to me bu thank you for reponding so quickly
    can you explain Why/what is the need to store/Refresh table at client side?

    and as suggested by other member,in case you are only intersted in getting Password for a user you can simply for for ExecuteReader and check if Return value is nothing (that mean user does not exist) or have Valid value.
    __________________
    Rate the posts that helped you

  13. #13

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] check to see if select statement finds a result

    i think i have firgured it out but now my connection string isnt working. this is my connection string
    Code:
    	
    Provider=MySQLProv;Data Source=mydb;User Id=myUsername;Password=myPassword;
    i can fill in the data source and user id and password but i dont know how to find my provider. any help?

  14. #14
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [2008] check to see if select statement finds a result

    if it's SQL database simply use SQLClient objects instead of OLEDB,also look at Thisit has plenty of connection string sample for various database.
    __________________
    Rate the posts that helped you

  15. #15

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] check to see if select statement finds a result

    that is where i got the connection string from and this is a mySQL db will that matter

  16. #16
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [2008] check to see if select statement finds a result

    can you post you code that fills/check the username (along with connection string and objects).

    Also check if are you able to call open() method of oleConnection object?if not post the error message.
    __________________
    Rate the posts that helped you

  17. #17

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] check to see if select statement finds a result

    ok i have gotten everything working except with my connection string.
    Code:
    Driver={MySQL ODBC 3.51 Driver};Server=http://dansoft.selfip.com;Option=131072;Stmt=;Database=elemental_battle; User=****;Password=****;
    my error
    Code:
    ERROR [HY000] [MySQL][ODBC 3.51 Driver]Unknown MySQL server host 'http://dansoft.selfip.com' (11004) ERROR [HY000] [MySQL][ODBC 3.51 Driver]Unknown MySQL server host 'http://dansoft.selfip.com' (11004)
    when i change the connection string to where server=localhost then there is no error and i can get my data. i don't see the problem.

  18. #18
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [2008] check to see if select statement finds a result

    never worked with MySQL,but you can cross this these point..

    1>Are you able to connect to server 'http://dansoft.selfip.com' using some external tools? (e.g SQL Analyser in case of SQL 2000)
    2>Make sure MySQL is running on same default port else explicitly specify it in your connection string.
    __________________
    Rate the posts that helped you

  19. #19

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] check to see if select statement finds a result

    can you recommend a good one?

  20. #20
    Frenzied Member
    Join Date
    Jan 2006
    Posts
    1,875

    Re: [2008] check to see if select statement finds a result

    Quote Originally Posted by bagstoper
    can you recommend a good one?
    What? if it's tool may be you would like to Check this
    __________________
    Rate the posts that helped you

  21. #21

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] check to see if select statement finds a result

    that only has an external download link to this

  22. #22

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] check to see if select statement finds a result

    well i found one but it also wont connect to the url but it will connect to the localhost. i get this error when it tries to connect to the url version (via visual studio)
    Code:
    ERROR [HY000] [MySQL][ODBC 3.51 Driver]Host '192.168.1.1' is not allowed to connect to this MySQL server ERROR [HY000] [MySQL][ODBC 3.51 Driver]Host '192.168.1.1' is not allowed to connect to this MySQL server

  23. #23

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [2008] check to see if select statement finds a result

    solved it i had to create a new user in mySQL with the host being anyone and give them privileges to view the database.

  24. #24
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [RESOLVED] [2008] check to see if select statement finds a result

    As a side note, if you're using MySQL, I'd recommend downloading the MySQL .NET provider off their site. It's a LOT faster and nicer to use than ODBC.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  25. #25

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [RESOLVED] [2008] check to see if select statement finds a result

    ok i will look into that i was wondering if there was any way i could make it go faster. it would take about 10 secs to connect and read the data for 2 rows. would hate to see how long it took to read 100+ rows. thanks i will into it.

  26. #26

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [RESOLVED] [2008] check to see if select statement finds a result

    wait do you mean the MySQL :: MySQL Connector/Net or something else?

  27. #27
    PowerPoster Jenner's Avatar
    Join Date
    Jan 2008
    Location
    Mentor, OH
    Posts
    3,712

    Re: [RESOLVED] [2008] check to see if select statement finds a result

    No, that's what I mean. The .NET connector (here). I believe the latest version is 5.2.2 and it works with VS2008. It appears like you're using the old ODBC driver v3.51 and talking to your database via the ADO.NET ODBC or OLEDB objects.

    The .NET connector gives you a MySQL namespace with MySqlConnection, MySqlCommand, MySqlDataAdapter, etc objects to use. It's also a fair amount faster.
    Last edited by Jenner; Jul 16th, 2008 at 04:24 PM.
    My CodeBank Submissions: TETRIS using VB.NET2010 and XNA4.0, Strong Encryption Class, Hardware ID Information Class, Generic .NET Data Provider Class, Lambda Function Example, Lat/Long to UTM Conversion Class, Audio Class using BASS.DLL

    Remember to RATE the people who helped you and mark your forum RESOLVED when you're done!

    "Two things are infinite: the universe and human stupidity; and I'm not sure about the universe. "
    - Albert Einstein

  28. #28

    Thread Starter
    Hyperactive Member
    Join Date
    Feb 2007
    Location
    indiana
    Posts
    341

    Re: [RESOLVED] [2008] check to see if select statement finds a result

    ok i just downloaded it how do i implement it into my project?

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width