Data access and vb class file from .net novise

Tech-Archive recommends: Fix windows errors by optimizing your registry

From: Terje Flaten (terflate_at_online.no)
Date: 11/03/04


Date: Wed, 3 Nov 2004 12:57:49 +0100

Hi!

Im trying to make a common data access file (class1.vb) for use in my .net
projects. To test this I make a simple webform based on some examples I
found at MSDN and asp.net.

My modell is like this:
webform3.aspx imports a class1.vb and calls subs and functions in this
class.
The Class1.vb imports the System.Data.OleDB. Class1 is supose to -open
connection, -execute a command, -close the connection and send back the data
as a resultset to the webform3.aspx

When I build and browse it runs fine the first time, but If I try to refresh
the browser I getting the error:

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.
The error is in this line of the class1 file: objConn.ConnectionString =
connection_string
Under here is the code for the 2 files:

I will thank you all for any help
Regards Terje

-------------------------

webform3.aspx
--------------------
Imports myapp.net.Class1
Public Class WebForm3
    Inherits System.Web.UI.Page
    Protected WithEvents MyDataGrid As System.Web.UI.WebControls.DataGrid

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
    'Put user code to initialize the page here
    Dim dbread
    'Get data from class1
    Connect_database("Provider=Microsoft.Jet.OLEDB.4.0;data
source=somebase.mdb")
    dbread = getSimpleData("Select * from Table1 where id=3")
    MyDataGrid.DataSource = dbread
    MyDataGrid.DataBind()
    Disconnect_database()
End Sub

End Class

----------------
My class1 file :

-----------------
Imports System.Data.OleDb

Public Class Class1

Public Shared objConn As New System.Data.OleDb.OleDbConnection()
Public Shared objCmd As New System.Data.OleDb.OleDbCommand()

    Shared Sub Connect_database(ByVal connection_string As String)
        'sett hvilken connectionstring som skal brukes av objConn objektet.
        objConn.ConnectionString = connection_string
        Try
            objConn.Open()
        ' hvilken connection skal kommandoen sendes/ settes til
            objCmd.Connection = objConn
        Catch
        End Try
    End Sub

    Shared Sub Disconnect_database()
    objConn.Close()
    objConn = Nothing
    objCmd = Nothing
    End Sub

    Shared Function getSimpleData(ByVal strSQL)
        Dim objRst
        If Trim(strSQL) <> "" Then
            ' Call remove_parameters()
            objCmd.CommandType = CommandType.Text
            objCmd.CommandText = strSQL
            objRst = objCmd.ExecuteReader()
            getSimpleData = objRst
        End If
    End Function
End Class



Relevant Pages

  • Re: An absence of IntelliSense in this situation
    ... > event source and only need to know if and when it fires an event then ... > Public Class CBaseEventSourceContainer ... > End Sub ... >>> As you say you still have access to the base class event source ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Event doesnt work in late binding?
    ... by the way from a VB.Net project you could use the pointer of a public class, so if you convert the project to a activex exe a public class should also work ... I can pass form to com and then client don't have to be a com, but when I want pass object to com, object class must be public - so must me a com, yea? ... Public Sub Connected() ... Private Sub Class_Initialize ...
    (microsoft.public.vb.enterprise)
  • Re: Gärtnerin
    ... Public Class Form1 ... Private VirtuellerGarten As New Garten ... Private Sub Handle_CreditsChanged(ByVal sender As Object, ... Public Overrides Function ToString() As String ...
    (microsoft.public.de.german.entwickler.dotnet.vb)
  • How to reset a complex property in PropertyGrid
    ... Public Sub New ... 'Required by the Windows Form Designer ... Public Overridable Overloads Function CanResetSelectedGridItem(ByVal resetIfPossible As Boolean) As Boolean ... Public Class Class1 ...
    (microsoft.public.dotnet.languages.vb)
  • Re: An absence of IntelliSense in this situation
    ... event source and only need to know if and when it fires an event then you ... Public Class CBaseEventSourceContainer ... Private Sub MyEventHandler ... >> As you say you still have access to the base class event source so ...
    (microsoft.public.dotnet.languages.vb)