Results 1 to 9 of 9

Thread: Retriewing data from class

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049

    Question Retriewing data from class

    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

  2. #2

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    Sorry, forgot...
    Attached Files Attached Files

  3. #3
    Frenzied Member Memnoch1207's Avatar
    Join Date
    Feb 2002
    Location
    DUH, Guess...Hint: It's really hot!
    Posts
    1,861
    can you write out the returned data from within the class itself?
    Being educated does not make you intelligent.

    Need a weekend getaway??? Come Visit

  4. #4
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    does it throw an error or do nothing?

    also you should do an
    Code:
    If Not(PostBack) Then 
    'GetData 
    End If
    in your Page_Load keeps you from getting data you've got
    Magiaus

    If I helped give me some points.

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    The error is:
    Code:
    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

  6. #6
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    try this

    Code:
    Dim objCommand As System.Data.OleDb.OleDbCommand = New System.Data.OleDb.OleDbCommand(param, param)
    If I recall correctly from before I quit VB and moved C# doing that for your objects will fix it.
    Magiaus

    If I helped give me some points.

  7. #7

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    I've tried that now, but I still get the same error

  8. #8
    Frenzied Member Magiaus's Avatar
    Join Date
    Mar 2002
    Location
    swamp land
    Posts
    1,267
    got me then
    Magiaus

    If I helped give me some points.

  9. #9

    Thread Starter
    Frenzied Member
    Join Date
    Jan 2001
    Location
    Denmark
    Posts
    1,049
    Now I've managed to solve the problem, finally!

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width