Click here to Skip to main content
15,890,609 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Private Sub OKbt1_Click(sender As Object, e As EventArgs) Handles OKbt1.Click
       Call Connect() ' [ connection to module ]'

       Dim Reader As SqlDataReader
       Try
           'selecting DATA from DATABASE
           Dim query As String
           query = "select * from uinfo where  password = '" & PASStb2.Text & "' "
           command = New SqlCommand(query, sqlConn)
           'Dim command As New SqlCommand("select * from uinfo where password = '" & PASStb2.Text & "'", sqlConn)
           'Dim dt As New DataTable
           'Dim adapter As New SqlDataAdapter(command)
           'adapter.Fill(dt)
           Reader = command.ExecuteReader


           Dim count As Integer
           count = 0
           While Reader.Read
               count = count + 1

           End While

           If count = 1 Then

               Dim usertype = Reader.GetString("Type")

               If usertype = "admin" Then
                   'MsgBox("username and password are correct")
                   MAIN_MENU.Show()


                   For a = 0 To 500

                   Next
                   Me.Hide()
                   sqlConn.Close()
                   sqlConn.Dispose()

               ElseIf usertype = "user" Then

                   For a = 0 To 500

                   Next
                   Me.Hide()
                   'MsgBox("username and password are correct")
                   USERMENU.Show()




               End If

           ElseIf count > 1 Then



               MsgBox("username and password are duplicate")



           Else
               MsgBox("username and password are not correct")


           End If

           sqlConn.Close()
       Catch ex As SqlException
           MsgBox(ex.Message)
       Finally
           sqlConn.Dispose()


       End Try
   End Sub


What I have tried:

i tried to change it to double but same error d Sub
the error goes like this "
System.InvalidCastException: 'Conversion from string "Type" to type 'Integer' is not valid.'"
Posted
Updated 19-Apr-18 19:55pm
Comments
RedDk 20-Apr-18 13:28pm    
There's NO REASON to use secure code that is not vulnerable to sql injection, typically through creation of a cte, on a box which has no security requirements ... but, offhand, this error 'Conversion from string to integer' seems to me to be the first thing shown in what is probably a cascading error referring to a method that is trying to stuff many things into a container which will hold only one thing. So fix the TSQL by provding a column name in the SELECT statement.

1 solution

So many bad ideas in such a short piece of code...

Don't do it like that! 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. Use Parametrized 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 when you have fixed that throughout your app, you can look at the next big change you need to make: 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 obvious, and online converters can help if you are stuck.
Just to give you an idea in how little regard text based passwords are held: Code Crime 1[^]
 
Share this answer
 

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

  Print Answers RSS


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