Error reading database

liunx

Guest
This is the code I have for connecting to an Access database.

Dim conn As System.Data.IDbConnection
Dim cmd As System.Data.IDbCommand
Dim reader As System.Data.IDataReader
Dim adapter As System.Data.IDbDataAdapter

conn.ConnectionString = dbConn()
Dim strSQL As String
strSQL = "select systemusername, password, userid, securitylevel "
strSQL &= "from systemuser "
strSQL &= "where systemusername='" & txtUserName.Text & "' "
strSQL &= "and password='" & txtPassword.Text & "' "

' Try to open database and read information.
Try
conn.Open()
reader = cmd.ExecuteReader
reader.Read()

If reader("securitylevel") >= 3 Then
Session.Contents("username") = reader("username")
Session.Contents("userid") = reader("userid")
Session.Contents("securitylevel") = reader("securitylevel")
Else
Response.Redirect("Welcome.aspx?ID=" + txtUserName.Text)
End If

reader.Close()
lblError.Text = ""
Catch err As Exception
lblError.Text = "Invalid Login"
Finally
If Not conn Is Nothing Then conn.Close()
End Try

I am getting the error "Error reading database: Object reference nto set to an instance of an object."

Anyone have any thoughts?

Thanks for any and all help!!I must declare first that I'm a C# programmer, I don't know much vb.net.
But I think that maybe you have to do this:
Dim conn As New System.Data.IDbConnection
Dim cmd As New System.Data.IDbCommand
Dim reader As New System.Data.IDataReader
Dim adapter As New System.Data.IDbDataAdapter:)

Heloo.,

Whenever you want to access the object you have to create the instance of the object with keyword NEW.

VIZ :
Dim sqlcon1 As SqlClient.SqlConnection = New SqlClient.SqlConnection(SQL)
or
Dim sqlcon1 As NEW SqlClient.SqlConnectionDim conn As New System.Data.odbc.odbcConnection(dbconn)
Dim cmd As new System.Data.odbc.OdbcCommand
Dim reader As System.Data.odbc.OdbcDataReader
Dim adapter As System.Data.odbc.OdbcDataAdapter

Dim strSQL As String
strSQL = "select systemusername, password, userid, securitylevel "
strSQL &= "from systemuser "
strSQL &= "where systemusername='" & txtUserName.Text & "' "
strSQL &= "and password='" & txtPassword.Text & "' "

' Try to open database and read information.
Try
conn.Open()
cmd.connection = conn
cmd.commandText = strSQL
reader = cmd.ExecuteReader
reader.Read()

If reader("securitylevel") >= 3 Then
Session.Contents("username") = reader("username")
Session.Contents("userid") = reader("userid")
Session.Contents("securitylevel") = reader("securitylevel")
Else
Response.Redirect("Welcome.aspx?ID=" + txtUserName.Text)
End If

reader.Close()
lblError.Text = ""
Catch err As Exception
lblError.Text = "Invalid Login"
Finally
If Not conn Is Nothing Then conn.Close()
End Try

Should Work if you use an odbc connectionCast from type 'OleDbConnection' to type 'String' is not valid.

Line 41: Dim conn As New System.Data.odbc.OdbcConnection(dbConn)
Line 42: Dim cmd As New System.Data.odbc.OdbcCommand
Line 43: Dim reader As System.Data.odbc.OdbcDataReader
Line 44: Dim adapter As System.Data.odbc.OdbcDataAdapter


Line 43 is where the error occurs. This is what I had before for the code and this is the error I recieved. That's why I was trying the IdbConnection.

Here is my dbConn function that I call. It may be something wrong with this.

Public Function dbConn()

Dim strConnectionString As String = "Provider=Microsoft.Jet.OLEDB.4.0; Ole DB Services=-4; Data Source=C:\Documents an" & _
"d Settings\EZ\Desktop\Alumni.mdb"

dbConn = strConnectionString

End Function

Thanks for the help!!!
 
Top