Click here to Skip to main content
15,889,867 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Try
            Dim myConnection As OleDb.OleDbConnection
            myConnection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "D:\myvbsbs\NES\NES\Database1.accdb")
            Dim myCommand As OleDbCommand
            'myCommand = New OleDb
            myCommand = New OleDbCommand("SELECT Username, Password FROM Users WHERE Username=txtUserid and Password=txtPassword")
            Dim uName As New OleDbParameter("@Username", SqlDbType.VarChar)
            Dim uPassword As New OleDbParameter("@Password", SqlDbType.VarChar)
            uName.Value = txtUserid.Text
            uPassword.Value = txtPassword.Text
            myCommand.Parameters.Add(uName)
            myCommand.Parameters.Add(uPassword)
            myCommand.Connection.Open()
            Dim myReader As OleDbDataReader = myCommand.ExecuteReader(CommandBehavior.CloseConnection)
            Dim Login As Object = 0
            If myReader.HasRows Then
                myReader.Read()
                Login = myReader(Login)
            End If
            If Login = Nothing Then
                MsgBox("Login Failes !!!!", MsgBoxStyle.Critical, "Error.")
                txtUserid.Clear()
                txtPassword.Clear()
                txtUserid.Focus()
            Else
                ProgressBar1.Visible = True
                ProgressBar1.Maximum = 5000
                ProgressBar1.Minimum = 0
                ProgressBar1.Value = 4
                ProgressBar1.Step = 1
                For i = 0 To 5000
                    ProgressBar1.PerformStep()
                Next
                FrmMain.ToolStripStatusLabel2.Text = txtUserid.Text
                Me.Hide()
                FrmMain.Show()
            End If
            myCommand.Dispose()
            myConnection.Close()
        Catch ex As Exception
            MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)

        End Try
    End Sub


What I have tried:

i hv searched the solution for my problem on internet & run devenv.exe, but still the problem is not solved.
Posted
Updated 10-Jul-20 0:43am

It is Visual Basic, not Studio.
First thing, remove the Try/Catch so the program will give you the error message with line of error. This information is really helpful.
Possible problem:
VB
myCommand = New OleDbCommand("SELECT Username, Password FROM Users WHERE Username=txtUserid and Password=txtPassword") ' Here you set a query without parameter
Dim uName As New OleDbParameter("@Username", SqlDbType.VarChar) ' Here you set a parameter that is not used by query
Dim uPassword As New OleDbParameter("@Password", SqlDbType.VarChar) ' Here you set a parameter that is not used by query
uName.Value = txtUserid.Text
uPassword.Value = txtPassword.Text
myCommand.Parameters.Add(uName)
myCommand.Parameters.Add(uPassword)
 
Share this answer
 
This is one of the most common problems we get asked, and it's also the one we are least equipped to answer, but you are most equipped to answer yourself.

Let me just explain what the error means: You have tried to use a variable, property, or a method return value but it contains null - which means that there is no instance of a class in the variable.
It's a bit like a pocket: you have a pocket in your shirt, which you use to hold a pen. If you reach into the pocket and find there isn't a pen there, you can't sign your name on a piece of paper - and you will get very funny looks if you try! The empty pocket is giving you a null value (no pen here!) so you can't do anything that you would normally do once you retrieved your pen. Why is it empty? That's the question - it may be that you forgot to pick up your pen when you left the house this morning, or possibly you left the pen in the pocket of yesterdays shirt when you took it off last night.

We can't tell, because we weren't there, and even more importantly, we can't even see your shirt, much less what is in the pocket!

Back to computers, and you have done the same thing, somehow - and we can't see your code, much less run it and find out what contains null when it shouldn't.
But you can - and Visual Studio will help you here. Run your program in the debugger and when it fails, VS will show you the line it found the problem on. You can then start looking at the various parts of it to see what value is null and start looking back through your code to find out why. So put a breakpoint at the beginning of the method containing the error line, and run your program from the start again. This time, VS will stop before the error, and let you examine what is going on by stepping through the code looking at your values.

But we can't do that - we don't have your code, we don't know how to use it if we did have it, we don't have your data. So try it - and see how much information you can find out!

And ... don't do it like that! 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 C#, but it's pretty obvious.

And remember: if you have any European Union users then GDPR applies and that means you need to handle passwords as sensitive data and store them in a safe and secure manner. Text is neither of those and the fines can be .... um ... outstanding. In December 2018 a German company received a relatively low fine of €20,000 for just that.
 
Share this answer
 
There are a few problems with your code
SQL
SELECT Username, Password
FROM   Users
WHERE  Username = txtUserid
AND    Password = txtPassword
1. Why are you selecting the username and password when you already know them?
2. This query compares the un/pw to other columns, and not to user input.
3. It would be recommended to hash or encrypt the password, this looks like plain-text

I would change the query up to just get the Count, and use the parameters.
SQL
SELECT Count(*)
FROM   Users
WHERE  Username = @Username
AND    Password = @Password
The next few lines look OK, you add in the parameters (which don't exist in the query but we're adding it in) and we get to the execution.
1. Set Login = 0
2. If there are rows, set Login to be the reader
3. Compare the Login value to nothing to see if the login failed

Umm... The login will be 0 if there were no rows found, not nothing.
Regardless, we already need to change this to reflect the altered SQL (Count) statement anyways.

So at this point I am rewriting your code to adapt it to new logic, and to simplify the parameters.. and I think I found your problem... you never set the Connection property of the SqlCommand
VB
Try
   Dim myQuery as String = "SELECT Count(*) FROM Users WHERE Username=@Username and Password=@Password"

   Dim myConnection As OleDb.OleDbConnection
   myConnection = New OleDb.OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + "D:\myvbsbs\NES\NES\Database1.accdb")

   Dim myCommand As OleDbCommand = New OleDbCommand(myQuery, myConnection)
   myCommand.Parameters.AddWithValue("@Username", txtUserid.Text)
   myCommand.Parameters.Add("@Password", txtPassword.Text)

   myCommand.Connection.Open()

   Dim Login As Int32 = myCommand.ExecuteScalar()

   If Login = 0 Then
      MsgBox("Login Failes !!!!", MsgBoxStyle.Critical, "Error.")
      txtUserid.Clear()
      txtPassword.Clear()
      txtUserid.Focus()
   Else
      ProgressBar1.Visible = True
      ProgressBar1.Maximum = 5000
      ProgressBar1.Minimum = 0
      ProgressBar1.Value = 4
      ProgressBar1.Step = 1
      For i = 0 To 5000
         ProgressBar1.PerformStep()
      Next
      FrmMain.ToolStripStatusLabel2.Text = txtUserid.Text
      Me.Hide()
      FrmMain.Show()
   End If

   myCommand.Dispose()
   myConnection.Close()

Catch ex As Exception
   MessageBox.Show(ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
Notes and recommendations:
1. VB is not my native language, and this was done in Notepad. There may be typos.
2. Many of the Sql objects have overloaded methods which were used, fewer lines.
3. As noted the passwords should not be plain-text; see the link from @OriginalGriff
4. As @Patrice T noted, you may want to comment out the try...catch for testing
 
Share this answer
 

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