I have a problem. I can't fetch my data located in a class to my webform. I have no problems using SQL-server, but it doesn't work when I use an Access database.
This is my class:
Code:
Imports System.Data
Imports System.Data.OleDb
Public Class clsLanguage
Inherits System.Web.UI.UserControl
Dim strLang, langArr(100) As String
Public x As Integer
Public Function getLanguage(ByVal language As String) As String()
Dim strLangCn As String = "Provider=Microsoft.jet.oledb.4.0;Data Source=" & Server.MapPath("language.MDB") & ";"
Dim strSql As String = "Select * FROM LanguageTable ORDER BY id ASC"
Dim objConnection As New OleDbConnection(strLangCn)
Dim objCommand As New OleDbCommand(strSql, objConnection)
Dim objDataReader As OleDbDataReader
objConnection.Open()
objDataReader = objCommand.ExecuteReader()
Do While objDataReader.Read() = True
langArr(x) = CStr(objDataReader(language))
x += 1
Loop
objDataReader.Close()
objConnection.Close()
Return langArr
End Function
End Class
And this is my webform:
Code:
Public Class WebForm1
Inherits System.Web.UI.Page
#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
End Sub
Protected WithEvents Label1 As System.Web.UI.WebControls.Label
Protected WithEvents Label2 As System.Web.UI.WebControls.Label
Protected WithEvents ListBox1 As System.Web.UI.WebControls.ListBox
'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceholderDeclaration As System.Object
Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeComponent()
End Sub
#End Region
Dim voresKunder As String()
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Dim test As New clsLanguage
voresKunder = test.getLanguage("english")
Dim kunde As String
For Each kunde In voresKunder
Try
ListBox1.Items.Add(kunde)
Catch ex As Exception
Exit For
End Try
Next
End Sub
End Class
I have attatched a zip file including the project with database
Server Error in '/SprogAppHpl' Application.
--------------------------------------------------------------------------------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 7:
Line 8: Public Function getLanguage(ByVal language As String) As String()
Line 9: Dim strLangCn As String = "Provider=Microsoft.jet.oledb.4.0;Data Source=" & Server.MapPath("language.MDB") & ";"
Line 10: Dim strSql As String = "Select * FROM LanguageTable ORDER BY id ASC"
Line 11: Dim objConnection As New OleDbConnection(strLangCn)
Source File: c:\inetpub\wwwroot\SprogAppHpl\clsLanguage.vb Line: 9
Stack Trace:
[NullReferenceException: Object reference not set to an instance of an object.]
System.Web.UI.UserControl.get_Server()
SprogAppHpl.clsLanguage.getLanguage(String language) in c:\inetpub\wwwroot\SprogAppHpl\clsLanguage.vb:9
SprogAppHpl.WebForm1.Page_Load(Object sender, EventArgs e) in c:\inetpub\wwwroot\SprogAppHpl\WebForm1.aspx.vb:30
System.Web.UI.Control.OnLoad(EventArgs e)
System.Web.UI.Control.LoadRecursive()
System.Web.UI.Page.ProcessRequestMain()
--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573