dcsimg
Results 1 to 11 of 11

Thread: [RESOLVED] always say wrong password why?

  1. #1

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,560

    Resolved [RESOLVED] always say wrong password why?

    hey,
    trying to check passwords between sql field and text box always say wrong password
    even when its correct password why?

    Code:
             Try
                CN.Open()
                Cmd.CommandText = "select * from Db_Users where username = '" & cmbusers.Text & "' and userpass = '" & TxtPass.Text & "'"
                Dr = Cmd.ExecuteReader
                If Dr.Read Then
                    MsgBox("Welcome", vbInformation)
                Else
                    MsgBox("Wrong Password", vbExclamation)
                    TxtPass.Clear()
                    TxtPass.Select()
                End If
    
            Catch ex As Exception
                MsgBox(ex.Message)
            End Try
    tnx for any help
    salsa

  2. #2
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    32,504

    Re: always say wrong password why?

    Because it's the wrong password. There are problems with the code, but none that get around the fact that Dr.Read is returning False, so why is it returning False? The most likely answer is that the credentials are simply wrong.

    Here are some other issues:

    1) Concatenating strings into a query opens you up to SQL Injection attacks. You need to be using parameters. This will also help with a really unlikely possible problem with your code.

    2) Returning ALL the fields when you don't even want one field is a waste. Don't use SELECT * casually. It's a bit slow. Use it only when you want ALL the fields. In this case, even one is more than you need.

    3) Getting a reader is fairly fast, but in this case it is also pointless. All you really need is the COUNT of a single field. Then you could use ExecuteScalar, which is even faster. If it returns 0, then no match was found. If it returns 1, then a match was found.

    Still, the code, as you have it, is pretty simple, so there aren't a lot of places problems could arise. That means that the problem is most likely that you simply aren't getting a match, despite thinking that you are entering the right information.
    My usual boring signature: Nothing

  3. #3
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,039

    Re: always say wrong password why?

    You really ought to have done some basic testing before posting here. How about you try retrieving all the records and then loop through them and compare the values? If you do that then you'll be able to see every set of values and pick out the one that you expect to match and then, when it doesn't, examine it to see why. Debugging does not consist solely of reading your code and throwing your hands in the air if you can't immediately see an issue. It involves stepping through code and carefully examining data.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  4. #4

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,560

    Re: always say wrong password why?

    You need to be using parameters
    can u give me an example?
    2) Returning ALL the fields when you don't even want one field is a waste. Don't use SELECT * casually. It's a bit slow. Use it only when you want ALL the fields. In this case, even one is more than you need.
    i change the select *
    3) Getting a reader is fairly fast, but in this case it is also pointless. All you really need is the COUNT of a single field. Then you could use ExecuteScalar, which is even faster. If it returns 0, then no match was found. If it returns 1, then a match was found.
    how do i use ExecuteScalar?
    im new in vb net(old vb 6 man)

  5. #5
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,039

    Re: always say wrong password why?

    Quote Originally Posted by salsa31 View Post
    can u give me an example?
    Can you look for an example? There are plenty around. Your first instinct should be to use a search engine to look for yourself, then ask only if you can't find anything helpful. For a comprehensive explanation and examples, follow the Blog link in my signature below and check out my post on Parameters In ADO.NET.
    Quote Originally Posted by salsa31 View Post
    how do i use ExecuteScalar?
    Again, why can't you look for yourself? Being new to VB.NET doesn't mean that you're new to programming or the internet. You should know how to use a search engine quite well by now. You might also follow the CodeBank link in my signature below and check out my thread on Retrieving & Saving Data.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,560

    Re: always say wrong password why?

    Can you look for an example? There are plenty around. Your first instinct should be to use a search engine to look for yourself, then ask only if you can't find anything helpful. For a comprehensive explanation and examples, follow the Blog link in my signature below and check out my post on Parameters In ADO.NET.
    i already looked
    Again, why can't you look for yourself? Being new to VB.NET doesn't mean that you're new to programming or the internet. You should know how to use a search engine quite well by now. You might also follow the CodeBank link in my signature below and check out my thread on Retrieving & Saving Data.
    why cant you just help?why are you sending me to look?
    dont you think i did that already?i seen your code banks very impressing.
    this form supposed to help people not criticize them.
    if i knew how to loop or doing some things you think i will ask here?
    what did i asked here?a nuclear code?

  7. #7
    Frenzied Member ChrisE's Avatar
    Join Date
    Jun 2017
    Location
    Frankfurt
    Posts
    1,290

    Re: [RESOLVED] always say wrong password why?

    Hi salsa,

    here a sample with Dr, this will return all Users(to a Listview) if you don't add some criteria.

    Like jmc said, start an see if all records a returned first, if the don't .. then why

    Code:
      Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            Dim sDB As String = "E:\Nordwind.mdb"
            Dim sCon As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & _
                                    "Data Source=" & sDB & ";"
    
            Dim Cn As OleDb.OleDbConnection = New OleDb.OleDbConnection(sCon)
            Cn.Open()
    
           
            Dim sSql As String = "SELECT Username, UserPass From tbl_User "
    
    
            Dim sWhere As String = Nothing
    
            If ComboBox1.Text <> Nothing Then
                sWhere &= " And tbl_User.UserName=" & ComboBox1.Text
            End If
            If TextBox2.Text <> Nothing Then
                sWhere &= " And tbl_User.UserPass=" & TextBox2.Text
            End If
            If sWhere <> Nothing Then
                sWhere = "Where " & sWhere.Substring(4)
            End If
            sSql &= sWhere & " Order by UserName"
    
            MsgBox(sSql) 'take a look at your Sql Statement, allways a good idea
    
            Dim Cmd As New OleDb.OleDbCommand(sSql, Cn)
            Dim Dr As OleDb.OleDbDataReader
            Dr = Cmd.ExecuteReader
    
            With ListView1
                If .Columns.Count = 0 Then
                    .View = View.Details
                    .Columns.Add("UserName", 250)
                    .Columns.Add("UserPass", 150)
                End If
                .Items.Clear()
                .BeginUpdate()
                Do While Dr.Read
                    Dim Li As New ListViewItem
                    Li.Text = Dr("UserName").ToString
                    Li.SubItems.Add(Dr("UserPass").ToString)
                    .Items.Add(Li)
                Loop
                .EndUpdate()
            End With
            Dr.Close()
        End Sub
    
        Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
            ComboBox1.Text = "myUser"
            TextBox2.Text = "myPass"
    
            ComboBox1.Items.Add("Salsa")
            ComboBox1.Items.Add("Chris")
        End Sub
    this isn't finished code for a User Entry/Pass, it is just to show you how to use the DR and return Data with you criteria entered

    regards
    Chris
    Last edited by ChrisE; Oct 14th, 2018 at 01:43 AM.
    to hunt a species to extinction is not logical !
    since 2010 the number of Tigers are rising again in 2016 - 3900 were counted. with Baby Callas it's 3901, my wife and I had 2-3 months the privilege of raising a Baby Tiger.

  8. #8

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,560

    Re: [RESOLVED] always say wrong password why?

    thank you chris that was my next question

  9. #9
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,039

    Re: always say wrong password why?

    Quote Originally Posted by salsa31 View Post
    i already looked
    And what did you search for? I just searched for "using parameters in database query vb.net" and these were the first three results:

    https://www.codeguru.com/columns/vb/...plications.htm
    https://stackoverflow.com/questions/...y-why-should-i
    https://jwcooney.com/2014/07/31/vb-n...uery-examples/

    All three of them provide an example of using parameters and there were plenty more besides.
    Quote Originally Posted by salsa31 View Post
    why cant you just help?why are you sending me to look?
    Why can't you just look instead of expecting us to spoon-feed you? I just demonstrated how easy it is to find this information. It seems like you need to spend a bit of time learning how to search the web effectively.
    Quote Originally Posted by salsa31 View Post
    dont you think i did that already?
    I don't know you so I have no idea what you would or wouldn't do. Two things I do know are that plenty of people don't make the slightest effort to find information for themselves and that this information is easy to find. Putting those two facts together led me to believe that you didn't look. I still think that you didn't look very hard at all.
    Quote Originally Posted by salsa31 View Post
    this form supposed to help people not criticize them.
    The two are not mutually exclusive. If someone does the wrong thing and it is never pointed out to them, they will continue to do the wrong thing. Hopefully, now that you can see how easy it is to find information like this, you'll try a bit harder to find it in future. If you do that then it will be a benefit to you, so I will have helped. It will also help us because there will be fewer questions that people should be able to answer for themselves and we can concentrate on the hard ones.
    Quote Originally Posted by salsa31 View Post
    if i knew how to loop or doing some things you think i will ask here?
    I'm not suggesting that you asked about something that you already knew. I'm suggesting that you asked about something that you could have easily found out for yourself with very little effort.
    Quote Originally Posted by salsa31 View Post
    what did i asked here?a nuclear code?
    No, you asked us to do something that you could have easily done for yourself.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Enjoy the moment
    Join Date
    Feb 2011
    Location
    Barrio Del pilar madrid spain
    Posts
    4,560

    Re: [RESOLVED] always say wrong password why?

    the problem was not it the code it was in the sql collation.
    the way that i learn is when you point to something that i do wrong and say if you do this then it will work.
    then i can move forward to the next error.

  11. #11
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    100,039

    Re: [RESOLVED] always say wrong password why?

    Quote Originally Posted by salsa31 View Post
    the way that i learn is when you point to something that i do wrong and say if you do this then it will work.
    That's what people say when they find doing their own research to be too much trouble. It's easier to justify a lack of effort to yourself if you tell yourself that you can't learn effectively by making that effort but that's really just a story you tell yourself. It's easier for anyone to move on if someone else simply tells them what to do but the best way to learn is always to do for yourself. Finding this issue would have been fairly easy with proper testing and finding information on suggested improvements would also have been easy. It just takes a bit of effort. Testing and researching is not the fun part of programming so a lot of people try to avoid it. You've really got to ask yourself whether you want to be a good developer or just have fun writing code. If it's the letter then that's your prerogative but I would rather spend my time helping those interested in the former.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

Posting Permissions

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



Featured


Click Here to Expand Forum to Full Width