Click here to Skip to main content
14,389,512 members
Rate this:
Please Sign up or sign in to vote.
See more:
Hello guys,
I had one error in my code but I don't know exactly how to correct it
Please help me out
my program it display the fellowong message as worning message

Warning 1 Variable 'UserRoles' is used before it has been assigned a value. A null reference exception could result at runtime. C:\Users\DAVID\Pictures\Library Project\Library Project\CuuLIB\CuuLIB\LoginForm2.vb 36 16 CuuLIB


here doing are the following code I m using, thank you

Imports System.Data.OleDb
Imports System.Media
Public Class LoginForm2
   
    Dim path = System.Windows.Forms.Application.StartupPath
    Dim LogOnsound As String
    Dim MyPlayer As New SoundPlayer()

    Private Sub LoginForm2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        LogOnsound = "\LogOn.wav"
    End Sub
    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        Dim connection As New OleDbConnection("PROVIDER=Microsoft.ACE.OleDb.12.0;DATA SOURCE = CUULibDB.mdb.accdb;")
        ''Dim command As New OleDbCommand("SELECT [ID] FROM [Staff] WHERE [usernameField] = username AND [passwordField] = password", connection)
        Dim command As New OleDbCommand("SELECT a.ID,a.RoleID FROM Staff a ,Role b WHERE a.usernameField = username AND a.passwordField = password ", connection)
        Dim UserRoles As String
        Dim usernameParam As New OleDbParameter("username", Me.UsernameTextBox.Text)
        Dim passwordParam As New OleDbParameter("password", Me.PasswordTextBox.Text)

        command.Parameters.Add(usernameParam)
        command.Parameters.Add(passwordParam)

        command.Connection.Open()
        Dim reader As OleDbDataReader = command.ExecuteReader()
        If reader.HasRows Then

            Do While reader.Read()
                UserRoles = reader.Item(1)
            Loop

            MessageBox.Show("you are authenticated")
            MyPlayer.SoundLocation = path & LogOnsound
            MyPlayer.Play()
            PasswordTextBox.Text = ""
            Me.Hide()
            If UserRoles = "1" Then
                LibrarySoftForm.Show()
            Else
                cuulibForm.Show()
            End If

        Else
            MessageBox.Show("username and or password are not found")
            PasswordTextBox.Text = ""
        End If

        command.Connection.Close()

    End Sub


    Private Sub Cancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Cancel.Click
        Me.Close()
    End Sub

End Class
Posted
Comments
[no name] 11-Aug-13 11:34am
   
So simply assign a value and your error will go away, Dim UserRoles As String = String.Empty
DAVID DUSHIMIMANA 11-Aug-13 11:45am
   
Thank you so much Sir, your help has been work properly

1 solution

Rate this:
Please Sign up or sign in to vote.

Solution 1

The reason it complains is pretty simple. If I cut down your code a bit it's a clearer:
Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
    Dim UserRoles As String
    Dim reader As OleDbDataReader = command.ExecuteReader()
        Do While reader.Read()
            UserRoles = reader.Item(1)
        Loop
        If UserRoles = "1" Then
            LibrarySoftForm.Show()
        End If
End Sub
The compiler can see a route through your code where UserRoles is never assigned a value before it reaches the If condition - basically, if the reader returns no rows. If your code takes that path at run time, then a null reference exception would be thrown, so it is letting you know before you get a problem in six months time.

Either check for Nothing as well as a specific value, or assign a default value when you define the variable which will "force" it to take the path you want if there are no rows.
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100