Question index out of range exception was unhandled

flodi

Member
Joined
Jan 23, 2008
Messages
15
Location
Makkah Al Mukarramah, Makkah, Saudi Arabia, Saudi
Programming Experience
Beginner
hi to all and happy new year
am using vb.net 2005 and an access data base
i use system.data.olede as top of my code
my database save in my bin folder with .mdb
am using a storedproceder techniqe for add and delete and insert

my program start with a login form
and here is my code:
VB.NET:
Imports System
Imports System.Data
Imports System.Data.OleDb
Public Class LoginForm1
    Private Sub OK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles OK.Click
        If Trim(UsernameTextBox.Text) = "" Then
            ErrorProvider1.SetError(UsernameTextBox, "الرجاء ادخال اسم للمستخدم")
        Else
            ErrorProvider1.SetError(UsernameTextBox, "")
        End If
        If Trim(PasswordTextBox.Text) = "" Then
            ErrorProvider1.SetError(PasswordTextBox, "الرجاء ادخال كلمة المرور")
        Else
            ErrorProvider1.SetError(PasswordTextBox, "")
        End If
        Dim str As String
        Dim oledbCommand2 As New OleDbCommand
        str = "login_sp"
        With oledbCommand2
            .CommandType = CommandType.StoredProcedure
            .CommandText = str
            .Parameters.AddWithValue("@em_user_name", UsernameTextBox.Text)
            .Parameters.AddWithValue("@em_password", PasswordTextBox.Text)
            .Connection = OleDbConnection1
        End With
        OleDbConnection1.Open()
        Dim valid As Integer = oledbCommand2.ExecuteScalar
        If valid < 1 Then
            'invalid login
            MsgBox("كلمة المرور خطاء", MsgBoxStyle.OkOnly)
            UsernameTextBox.Text = ""
            PasswordTextBox.Text = ""
            UsernameTextBox.Focus()
            OleDbConnection1.Close()
            oledbCommand2.Parameters.Clear()
        Else
            'valid login
            Label1.ForeColor = Color.Red
            OK.Enabled = False
            Cancel.Enabled = False
            UsernameLabel.Enabled = False
            PasswordLabel.Enabled = False
            UsernameTextBox.Enabled = False
            PasswordTextBox.Enabled = False
            MenuStrip1.Enabled = True
            username = UsernameTextBox.Text
            Label3.Text = username
            oledbCommand2.Parameters.Clear()
            OleDbCommand1.Parameters("@em_user_name").Value = UsernameTextBox.Text
            Dim read As OleDb.OleDbDataReader
            read = OleDbCommand1.ExecuteReader
            If read.Read Then
                Label2.Text = read.Item("em_user_type")
                read.Close()
                OleDbConnection1.Close()
                OleDbCommand1.Parameters.Clear()
            End If
        End If
    End Sub
my error shows in line:OleDbCommand1.Parameters("@em_user_name").Value = UsernameTextBox.Text
and it say's:
index out of range exception was unhandled
An OleDbParameter with ParameterName '@em_user_name' is not contained by this OleDbParameterCollection.
my oledbcommand1 text is:
VB.NET:
SELECT     em_user_type
FROM         ghost_user
WHERE     (em_user_name = '@em_user_name')
i don't know what's wrong in my code coz every thing okay
so why this error happend?plz help me in that coz i get tierd to know what's the problem
thanks
 
Last edited by a moderator:
'@em_user_name' is a plain string, @em_user_name is a parameter marker. Remove the quotes.
 
Back
Top