简体   繁体   中英

Vb.net Login using OdbcConnection

I am using Postgresql for my database, i want to create an App where the user login their account, i have this logic, and i am getting this error System.NullReferenceException: 'Object reference not set to an instance of an object.' why i am getting this error?

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Try
            Dim Username = Username_TextBox1.Text
            Dim Password = Password_TextBox2.Text
            Dim dt As New DataTable
            Dim ds = New DataSet
            Dim i As Integer
            Try
                Using MyCon As New Odbc.OdbcConnection("Driver={PostgreSQL ANSI};database=YouthRecord;server=localhost;port=5432;uid=postgres;sslmode=disable;readonly=0;protocol=7.4;User ID=*****;password=*****;"),
                    cmd As New Odbc.OdbcCommand("SELECT * FROM YouthApp_tableuser WHERE `username` " & "='" & Username & "' and " & "password='" & Password & "'", MyCon)

                    If ds.Tables("YouthApp_tableuser").Rows.Count > 0 Then
                        For i = 0 To ds.Tables("YouthApp_tableuser").Rows.Count - 1
                            Username = ds.Tables("YouthApp_tableuser").Rows(i).Item(1).ToString
                            Password = ds.Tables("YouthApp_tableuser").Rows(i).Item(2).ToString
                        Next
                        If (Username = Username_TextBox1.Text And Password = Password_TextBox2.Text) Then
                            Username = ""
                            Password = ""

                            MainForm.Show()
                            Me.Hide()
                        Else
                            MsgBox("Invalid User! Please Enter Correct User Name and Password.", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Invalid!")
                            Username = ""
                            Password = ""
                        End If
                    End If

                    MyCon.Open()
                    dt.Load(cmd.ExecuteReader)
                End Using
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try
        Catch ex As Exception

        End Try

this is the error

在此处输入图像描述

UPDATE

Try
   Using MyCon As New Odbc.OdbcConnection("Driver={PostgreSQL ANSI};database=YouthRecord;server=localhost;port=5432;uid=******;sslmode=disable;readonly=0;protocol=7.4;User ID=*****;password=*****;"),
   cmd As New Odbc.OdbcCommand("SELECT id, username, password FROM ""YouthApp_tableuser"" where username='" & Username & "' and password='" & Password & "' ", MyCon)
   MyCon.Open()
   dt.Load(cmd.ExecuteReader)
       If ds.Tables("YouthApp_tableuser").Rows.Count > 0 Then
             If (Username = Username_TextBox1.Text And Password = Password_TextBox2.Text) Then
                 Username = ""
                 Password = ""

                 MainForm.Show()
                 Me.Hide()
               Else
                 MsgBox("Invalid User! Please Enter Correct User Name and Password.", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Invalid!")
                  Username = ""
                  Password = ""
                End If
       End If

   End Using
Catch ex As Exception
    MessageBox.Show(ex.Message)
End Try

i get the same error

在此处输入图像描述

 Try
                Using MyCon As New Odbc.OdbcConnection("Driver={PostgreSQL ANSI};database=YouthRecord;server=localhost;port=5432;uid=*****;sslmode=disable;readonly=0;protocol=7.4;User ID=*****;password=*****;"),
                    cmd As New Odbc.OdbcCommand("SELECT id, username, password FROM ""YouthApp_tableuser"" where username='" & Username & "' and password='" & Password & "' ", MyCon)
                    MyCon.Open()
                    dt.Load(cmd.ExecuteReader)

                    If dt.Rows.Count > 0 Then
                        If (Username = Username_TextBox1.Text And Password = Password_TextBox2.Text) Then
                            Username = ""
                            Password = ""

                            MainForm.Show()
                            Me.Hide()

                        End If
                    Else
                        MsgBox("Invalid User! Please Enter Correct User Name and Password.", MsgBoxStyle.Critical + MsgBoxStyle.OkOnly, "Invalid!")
                        Username = ""
                        Password = ""
                    End If


                End Using
            Catch ex As Exception
                MessageBox.Show(ex.Message)
            End Try

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM