Login form error

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Ajay Bhalala
    New Member
    • Nov 2014
    • 119

    Login form error

    I have created 1 login form.

    I have used the MS ACCESS database as backend tool.
    Table Name : UserAccount
    Fields : • username • password

    I written the code as below ...

    Button1=Login button

    Code:
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    
            Dim con As New OleDbConnection("Provider=microsoft.jet.oledb.4.0 ; Data Source=E:\Brinda & Jay\Brinda\02 S.Y\VB.NET\My Project\Project.mdb")
    
    con.Open()
    
    qry = "select count(id) from UserAccount where username= ' " & txtUserName.Text & " ' and password= ' " & txtPassword.Text & " ' ;"
    
    Dim cmd As New OleDbCommand(qry, con)
    Dim da As New OleDbDataAdapter(cmd)
    ans = cmd.ExecuteScalar()
    If ans > 0 Then
        MsgBox("Accessed")
    Else
        MsgBox("Not Accessed")
    End If
    con.Close()
    End Sub
    There is no error in this code, but when I run this form, and enter the right username and password, then also it displays the magbox "Not Accessed". What can I do about this?
  • Frinavale
    Recognized Expert Expert
    • Oct 2006
    • 9749

    #2
    The OleDbCommand.Ex ecuteScalar Method returns the first column of the first row in the result set returned by the query.

    Are you sure this is what you want?

    I think what you probably want to use the OleDbCommand.Ex ecuteReader Method and then check if the reader HasRows to determine if a row exists in the database with the user name and password provided.

    You should always use the OleDbCommand.Pa rameters Property instead of dynamically building your SQL query string by directly inserting data provided by the user. This will prevent SQL insertion attacks and give you better caching ability for your SQL commands.

    -Frinny

    Comment

    • michael0120
      New Member
      • Nov 2014
      • 6

      #3
      try this..

      This tutorial will teach you how to create Log-in with database using VB6 and MS Access. Keep in mind that your project and database ...

      Comment

      • Frinavale
        Recognized Expert Expert
        • Oct 2006
        • 9749

        #4
        Michael,

        This question was asked in the VB.NET forum, not the vb6 forum.
        The answer to the question should be using the VB.NET controls and tools.

        Comment

        • Ajay Bhalala
          New Member
          • Nov 2014
          • 119

          #5
          I have tried to create the login form in another way as follows

          Code:
          Imports System.Data.OleDb
          
          Public Class Login
          
          Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
          
                  Dim con As New OleDbConnection("Provider=microsoft.jet.oledb.4.0 ; Data Source=E:\Brinda & Jay\Brinda\02 S.Y\VB.NET\My Project\Project.mdb")
          
          con.Open()
          
          Dim password As String
          password = Me.txtPassword.Text.ToString
          Dim da As New OleDbDataAdapter("select * from UserAccount where username=" & txtUserName.Text.ToString, con)
          Dim dt As New DataTable
          da.Fill(dt)
          Dim epassword As String = dt.Rows(0).Item("Password")
          
          If password.CompareTo("epassword") Then
          MsgBox("Successful")
          Else
          MsgBox("Not Success")
          End If
          
          con.Close()
          End Sub
          End Class
          but there is runtime error occured. "No value given for one or more required parameters."

          And whrer I have to use OleDbCommand.Pa rameters Property in above 1st or 2nd code? and How can I use this property?

          It is my college's exercise for connectivity in vb.net

          I have tried it since a month, but there is nothing could be done in this. I have also tried many different methods, but nothing could be done.

          Can you provide me a new successful code for this?
          Please help me Frinny.

          Comment

          • Frinavale
            Recognized Expert Expert
            • Oct 2006
            • 9749

            #6
            Ajay Bhalala,

            The code you posted looks exactly the same as the code you posted previously.

            I suggested that you probably didn't want to use the ExecuteScalar method, that you are probably interested in using the ExecuteReader command instead and then you could check the HasRows property to determine if any rows are returned, and I suggested that you should be using parameters.

            By posting the links that I previously provided, I was recommending that you re-write your code to be something like this (The following is guideline and I have no idea if this is going to work as written):
            Code:
            Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
            
              Dim conString As String = "Provider=microsoft.jet.oledb.4.0 ; Data Source=E:\Brinda & Jay\Brinda\02 S.Y\VB.NET\My Project\Project.mdb"
              Dim msg As String = "Access not checked" 
            
              Using connection As New OleDbConnection(conString)
                Dim queryString = "select * from UserAccount where username= ? and password= ?;"
                Dim command As New OleDbCommand(queryString, connection)
                command.Parameters.Add(txtUserName.Text)
                command.Parameters.Add(txtPassword.Text)
            
                connection.Open()
                Dim reader As OleDbDataReader = command.ExecuteReader()
                  
                If reader.HasRows
                  msg = "Access Permitted"
                Else
                  msg = "NO Access Permitted"
                End If
            
                reader.Close()
              End Using
              MsgBox(msg)
            End Sub

            -Frinny
            Last edited by Frinavale; Dec 2 '14, 05:30 PM.

            Comment

            • Ajay Bhalala
              New Member
              • Nov 2014
              • 119

              #7
              If I take "conString" in place of "connectionStri ng" following runtime error occured.

              "The OleDbParameterC ollection only accepts non-null OleDbParameter type objects, not String objects."

              in line 8 in above code.

              Comment

              • Ajay Bhalala
                New Member
                • Nov 2014
                • 119

                #8
                Thank you for your quick reply. Can you provide me your full code?

                I mean to say can you give me a new full code for the login form?

                Comment

                • Frinavale
                  Recognized Expert Expert
                  • Oct 2006
                  • 9749

                  #9
                  That error message suggests that the Parameters property expects to have OleDbParameter types added to it and not String types.

                  In my code I added just strings...

                  You need to modify your code to provide proper parameters.

                  Maybe something like this:
                  Code:
                  command.Parameters.Add("@UserName", OleDbType.VarChar, 80).Value = txtUserName.Text
                  command.Parameters.Add("@Password", OleDbType.VarChar, 80).Value = txtPassword.Text
                  See the documentation links for more information.

                  -Frinny

                  Comment

                  • Ajay Bhalala
                    New Member
                    • Nov 2014
                    • 119

                    #10
                    If I use this, and run my application, then msgbox is displays..
                    MsgBox Title : Microsoft Visual Basic 2008 Express Edition.
                    MsgBox Text : There is no source code available for the current loaction.

                    and if I click OK in msgbox, runtime error occured, "The OleDbParameterC ollection only accepts non-null OleDbParameter type objects, not String objects." in 'command.Parame ters.Add("@User Name", OleD' with dull green color.

                    What can I do?

                    Comment

                    • Frinavale
                      Recognized Expert Expert
                      • Oct 2006
                      • 9749

                      #11
                      Then supply it with non-null parameters.
                      I just took the code that I posted from the msdn example which means you probably need to modify it for your purpose.

                      Do you understand what non-null means?
                      It means that you have declared a variable but not instantiated it yet.

                      So, to fix Null problems ensure that the variables are instantiated before using them.

                      To instantiate something you use the keyword new.

                      Like this:
                      Code:
                      Dim uNameParam As New OleDbParameter("@UserName", OleDbType.VarChar, 80)
                      Dim passParam As New OleDbParameter("@Password", OleDbType.VarChar, 80)
                      
                      ' Now uNameParam and passParam are instantiated (not null)
                      ' So you can use them
                      uNameParam.Value = txtUserName.Text
                      passParam.Value = txtPassword.Text
                      
                      'And then add them to the parameters property of the command
                      command.Parameters.Add(uNameParam)
                      command.Parameters.Add(passParam)

                      Comment

                      • Ajay Bhalala
                        New Member
                        • Nov 2014
                        • 119

                        #12
                        Oh wow frinny...... thank you very very much for your help. I am so happy.
                        After all its done. I am so happy.... oh frinavale thank you very very very very much for the help.

                        Thank you so much. Thank you, thank you, thank you....

                        Frinavale you helps me lot. I am so happy. Its a big day for me in my life, thank you again frinny.

                        thanks again for all your helps and links provided by you.

                        All the information provided by you is very useful for me. VB.NET was my favorite subject. but while this login form is not done I have lost my interest from this subject, but now I have get my interest again in this subject due to you.

                        You solve my query which can't be solved by anything including my teacher, so I like my this subject VB.NET again.
                        Before I ask this question on this website, I have aked my whole teachers, my friends, etc, but no one can solve this and you solve it. So thank you very much.

                        Thanks frinny. and sorry for take your huge time.

                        Comment

                        • Ajay Bhalala
                          New Member
                          • Nov 2014
                          • 119

                          #13
                          I am sorry, but I still have a question.

                          In database, I have enter the "Brinda" in Username field and "MyProject" in Password field.

                          When I run my application, and enter the above username and password in form then it display "accessed", but when I enter the "brinda" in username's textbox and "myproject" in password's textbox in form then it also display "Accessed". I want to check the username and password case sensitively. Can I do this? If yes then How?

                          Thank you.

                          Comment

                          • Frinavale
                            Recognized Expert Expert
                            • Oct 2006
                            • 9749

                            #14
                            I am really happy you were able to solve your problem. The .NET framework is very powerful and never gets boring!

                            I haven't actually tried connecting to an Access Database like you are doing.

                            When I query my MS SQL database using:
                            Code:
                            Select * From userTable Where username='brinda' and password='myproject'
                            It will not work because it is case sensitive.

                            This may be something special with how Access processes select queries.

                            Aside form the odd behviour of select command, I think you may be interested in checking out the topic of .NET Principals and Identities.

                            An Identity contains information about the user's authentication details.... by this I mean it holds the user name and password and has a method that does the process of authenticating a provided user name and password (just like you are doing).

                            A Principal contains an Identity and once the user has been authenticate (logged in) the Principal class retrieves the "roles" or "permission s" that the user has privileges to.

                            This is really useful if your application has sections that are visible to some users (like the owner of a company or an IT administrator) that are not visible to other users (like a receptionist or line worker).

                            After authenticating (checking if the user name and password work), the rest of the application can be locked down or unlocked according to the permissions/roles assigned to the user.

                            The general topic to research would be "Authentica tion and Authorization in .NET". It is really cool and once you know how to use principals and identities you will be able to professionally implement multi-user applications!

                            Anyways, it's a really cool topic and if you want to try making your own custom Identity and Principal class feel free to ask questions about it.

                            In the meantime, I think it would be best to ask about why your select query is returning true for something that it is not true in the Access forum.....

                            Unless there is something else wrong.... Did you try authenticating something that you know isn't in your database? Like authenticating username:'Frinn y' and password:'bytes '?

                            Does that return true or false?

                            -Frinny
                            Last edited by Frinavale; Dec 4 '14, 04:16 PM.

                            Comment

                            • Ajay Bhalala
                              New Member
                              • Nov 2014
                              • 119

                              #15
                              Yes I have try authenticating something that isn't in my database. It returns msgbox "Not Accessed". There is no problem with wrong name or password, there is problem only with the case sensitivity.

                              and thanks for your information about .NET Principals and Identities. I will surely study this topics.

                              Really, VB.NET is very interesting subject.

                              Project making is not in our course for this year, but I am interested in Project Making. I want to make one project in this year, but I think I have not enough knowledge of VB.NET for making the Project in VB.NET. But now I think I have to start to make the project, because if I have any query, then I am sure you will solve my query and help me on my project. and I am sure you will give me ideas for how to make the software.

                              Thank You Frinny for help me lot and also for solve my queries. Thank you very much. Now I will start the making the software and which topic I select, I will say you tommorow. I am suer you will help me to make my first software.
                              Thanks again.

                              Comment

                              Working...