Click here to Skip to main content
15,891,909 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
hello.
i have this code,,its work (kind of).
im getting error with this line:
Form2.Label2.Text = reader(2).ToString

error :
Invalid attempt to read when no data is present

why its says "no data"? i have all data in database?

can someone helpo me to correct this code?
thank you ..


VB
What I have tried:

<pre lang="vb">
 <pre>  
Dim connString As String = ConfigurationManager.ConnectionStrings("connectionstring").ConnectionString
        Dim conn As New SqlConnection(connString)
        conn.Open()
        Dim comm As New SqlCommand("SELECT username, Password,type   FROM users WHERE username='" & TextBox1.Text & "' AND Password='" & TextBox2.Text & "'", conn)
        Dim reader As SqlDataReader
        reader = comm.ExecuteReader

        Dim count As Integer
        count = 0
        While reader.Read
            count = count + 1
        End While
        If count = 1 Then
            MessageBox.Show("username and password are correct")


            Form2.Show()


            Form2.Label1.Text = Me.TextBox1.Text
            Form2.Label2.Text = reader(2).ToString
        ElseIf count > 1 Then
                MessageBox.Show("username and password are duplicated")
            Else
                MessageBox.Show("username and password are wrong")

        End If
Posted
Updated 20-Oct-18 19:46pm

1 solution

Well, yes - you will.
Let me rip some code out so it's easier to see:
        While reader.Read
            count = count + 1
        End While
        If count = 1 Then
...
            Form2.Label2.Text = reader(2).ToString
You loop through all rows to count them. So after the loop, reader is guaranteed to not be looking at a row - because if it was then reader.Read would not have returned false!
So reader isn't on a row, and if you try access row data, there isn't any there to look at! And you get an error as a result.

But that is extremely dangerous code, particularly so on a login form! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

And that means you need to go through your whole app as a matter of priority and fix that throughout: leave just one and your database is at risk!

The other problem is the way you are handling passwords, which is both dangerous and (since GDPR) can mean you are liable for prosecution for negligent handling of user data if a EU citizen tries to use your system ...
Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^] - the code is in C#, but it's pretty simple, and tehre are online converters if you can't cope: Code Converter C# to VB and VB to C# – Telerik[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900