Re: Data access and vb class file from .net novise

From: yonggangwang (wyglx1224_at_msn.com)
Date: 11/03/04


Date: Wed, 3 Nov 2004 08:44:09 -0500

I think you need to ceate an instance of the calss1
after Imports myapp.net.Class1
      publick Test as new Class1
try again

"Terje Flaten" <terflate@online.no> 写入消息新闻
:eYU36xZwEHA.1984@TK2MSFTNGP14.phx.gbl...
> 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: DataGrid-Inhalt in ein Picture wandeln?
    ... Imports System.Data ... Public Class Handler: Implements IHttpHandler ... Public Sub ProcessRequestImplements IHttpHandler.ProcessRequest ... Dim fnt As New Font(System.Drawing.FontFamily.GenericSansSerif, 12, ...
    (microsoft.public.de.german.entwickler.dotnet.asp)
  • Re: Interaction between the classes
    ... Imports System.Windows.Forms ... Public Class myKeyClass Implements owf.IMessageFilter ... Overridable Function PreFilterMessageAs Boolean Implements owf.IMessageFilter.PreFilterMessage ... Public Sub New ...
    (microsoft.public.dotnet.framework.compactframework)
  • Re: Intercepting the WM_HELP message
    ... Imports System.Drawing ... End Sub ... Public Class MessageFilter ... eMVP" wrote in message ...
    (microsoft.public.dotnet.framework.compactframework)
  • vb.net error while registering evevents
    ... Evidence assemblySecurity, String[] args) ... Imports System.Runtime.Remoting.Channels.Http ... Public Class Client ... Public Sub New ...
    (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 ... > Public Class CBaseEventSourceContainer ... > End Sub ... >>> As you say you still have access to the base class event source ...
    (microsoft.public.dotnet.languages.vb)