2016-09-19 43 views
-2

我的代碼工作正常,直到當我添加參數。從那時候我得到這個錯誤:DataReader錯誤

An unhandled exception of type 'System.NullReferenceException' occurred in NewMonitoringSystem.exe

Additional information: Object reference not set to an instance of an object.

我需要幫助解決這個錯誤。 這裏是我的代碼:錯誤在此代碼

Private Sub btnLogin_Click(sender As Object, e As EventArgs) Handles btnLogin.Click 
    frmMainMenu.Height = 720 
    frmMainMenu.Width = 950 
    Dim numID As Integer = 2 
    cmd.Connection = cn 
    cn.Open() 
    cmd.CommandText = "SELECT ID, username, password FROM tblUsers WHERE username = @username and password = @password" 
    cmd.Parameters.Add(New SqlClient.SqlParameter("@username", SqlDbType.VarChar, 20)).Value = txtUsername.Text 
    cmd.Parameters.Add(New SqlClient.SqlParameter("@password", SqlDbType.VarChar, 20)).Value = txtPassword.Text 
    If dr.HasRows Then 
     dr.Read() 
     If dr("ID") = numID Then 
      Me.Hide() 
      frmMainMenu.lblAccessLevel.Text = "Admin" 
      frmMainMenu.Show() 
     Else 
      Me.Hide() 
      frmMainMenu.lblAccessLevel.Text = "User" 
      frmMainMenu.TSMOffertory.Enabled = False 
      frmMainMenu.TSMOffertory.Visible = False 
      frmMainMenu.Show() 
     End If 
    Else 
     MsgBox("Invalid Credentials", MsgBoxStyle.Exclamation, "Invalid LogIn") 
    End If 
    cn.Close() 
End Sub 
+0

在你的調試會話中哪行代碼沒有發生錯誤? – GuidoG

+1

什麼是博士,其聲明和創作在哪裏? – GuidoG

+0

@GuidoG博士是在_Public類MyForm_ ** Dim cmd作爲新的SqlCommand ** ** Dim博士作爲SqlDataReader ** –

回答

0
dr = cmd.ExecuteReader 

找到了解決辦法的第10行。只需在參數後添加該行即可。