locked
login form RRS feed

  • Question

  • hi. i have a windows form application with a login form. here is the code for that form:

    Imports System.Data.SqlClient
     
    Public Class LoginForm1
     
        ' TODO: Insert code to perform custom authentication using the provided username and password 
        ' (See http://go.microsoft.com/fwlink/?LinkId=35339).  
        ' The custom principal can then be attached to the current thread's principal as follows: 
        '     My.User.CurrentPrincipal = CustomPrincipal
        ' where CustomPrincipal is the IPrincipal implementation used to perform authentication. 
        ' Subsequently, My.User will return identity information encapsulated in the CustomPrincipal object
        ' such as the username, display name, etc.
     
        'Dim con As New Data.SqlClient.SqlConnection
        'Dim cmd As New Data.SqlClient.SqlCommand
        
        'Dim sdr As SqlDataReader
     
        Private Sub OK_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles OK.Click
            Stop
            Dim con As New Data.SqlClient.SqlConnection("Data Source=69.175.122.58,8888;Initial Catalog=CustomerTicks4;Persist Security Info=True;User ID=danwo;Password=dan58wo")
            Dim cmd As New Data.SqlClient.SqlCommand("Select * From tblUsers Where txtUserName= UserNameTextBox.Text AND txtPassword= PasswordTextBox.Text,con")
            
            con.Open()
            
            Dim sdr As SqlDataReader = cmd.ExecuteReader 'this error occurs here: ExecuteReader: Connection property has not been initialized.
     
     
     
     
     
            ' If the record can be queried, it means passing verification, then open another form.   
            'If (sdr.Read() = True) Then
            'MessageBox.Show("The user is valid!")
            'Dim mainForm As New MainForm
            'mainForm.Show()
            'Me.Hide()
     
            'If sdr.Read = True Then
            'MessageBox.Show("The user is valid!")
     
     
     
     
            'End If
     
     
        End Sub
     
        Private Sub Cancel_Click(ByVal sender As System.ObjectByVal e As System.EventArgsHandles Cancel.Click
            Me.Close()
        End Sub
     
        Private Sub Button1_Click(sender As System.Object, e As System.EventArgsHandles Button1.Click
            My.Forms.AddUserForm.Show()
     
     
        End Sub
    End Class

    Dan Wojtasiewicz

    Monday, September 17, 2012 6:19 PM

All replies

  • Hi Dan,

    Welcome to the MSDN forum.

    What's the problem you're facing? Please clarify your question, we're glad to help you.

    Have a nice day!


    Shanks Zen
    MSDN Community Support | Feedback to us

    Wednesday, September 19, 2012 3:04 AM
    Moderator
  • Private Sub btn_Ok_Click(sender As Object, e As EventArgs) Handles btn_Ok.Click
    
            Dim myConn As OleDbConnection
            Dim myCmd As OleDbCommand
            Dim myDr As OleDbDataReader
            Dim myDT As New DataTable
    
            myConn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=E:\Database File\PulsaHP.mdb")
            myConn.Open()
            myCmd = New OleDbCommand("SELECT Nama,Password,UserLevel FROM Pengguna WHERE Nama='" & tb_User.Text & "' AND Password='" & tb_Pass.Text & "'", myConn)
            myDr = myCmd.ExecuteReader
            myDT.Clear()
            myDT.Load(myDr)
            Try
                If myDT.Rows(0)(0).ToString = tb_User.Text And myDT.Rows(0)(1).ToString = tb_Pass.Text Then
                    If myDT.Rows(0)(2).ToString.ToLower = "1" Then
                        'code if the user login as administrator
                    Else
                        'code if user login as standard user
                    End If
    
                    MessageBox.Show("Login Success")
                End If
            Catch ex As Exception
                MessageBox.Show("LogIn Failed")
            End Try
    that my example, on your code you are wrong type the command 
    Dim cmd As New Data.SqlClient.SqlCommand("Select * From tblUsers Where txtUserName= UserNameTextBox.Text AND txtPassword= PasswordTextBox.Text,con")
    the correct one is :
    Dim cmd As New Data.SqlClient.SqlCommand("Select * From tblUsers Where txtUserName= UserNameTextBox.Text AND txtPassword= PasswordTextBox.Text",con)




    • Proposed as answer by Rian Widianto Monday, November 19, 2012 1:52 PM
    Wednesday, October 10, 2012 2:24 AM