Forum Moderators: open
Firstly, my global.asa file has an application variable which contains the database connection string.
Application("ConnectionStr") = "Provider=Microsoft.Jet.OLEDB.4.0;Data
Source=C:\Inetpub\ww wroot\sites\test\testdb.mdb"
My aspx file contains the following HTML:
<%@ Page Language="vb" AutoEventWireup="false"
Codebehind="connect_nonDSN.aspx.vb" Inherits="Test.connect_nonDSN"%>
<HTML>
<HEAD>
<title>MS Access Database Example</title>
</HEAD>
<body>
<asp:label id="connection_open" runat="server"
/><br>
<asp:label id="connection_command" runat="server"
/><br>
<asp:label id="connection_close" runat="server" />
<br><br><br>
<asp:DataGrid id="DGrid" Runat="server" />
</body>
</HTML>
I have two options. The first causes no results to be displayed. It has the
code including in the page 'DGrid = New DataGrid()'. Here is the code:
Imports System.Data
Imports System.Data.OleDbPublic Class connect_nonDSN
Inherits System.Web.UI.Page
Protected WithEvents connection_open As System.Web.UI.WebControls.Label
Protected WithEvents connection_close As System.Web.UI.WebControls.Label
Protected WithEvents connection_command As
System.Web.UI.WebControls.Label#Region " Web Form Designer Generated Code "
'This call is required by the Web Form Designer.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()End Sub
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
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
'Put user code to initialize the page hereDim conn As OleDbConnection
Dim comm As OleDbCommand
Dim DGrid As DataGrid
DGrid = New DataGrid()conn = New OleDbConnection(Application("ConnectionStr"))
comm = New OleDbCommand("SELECT table001.field001 FROM table001;",
conn)
conn.Open()
connection_open.Text = "Connection open!"
DGrid.DataSource = comm.ExecuteReader
DGrid.DataBind()
connection_command.Text = "Command Run!"
conn.Close()
connection_close.Text = "Connection closed!"End Sub
End Class
If I comment out the line 'DGrid = New DataGrid()' I get the following
error:
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 35: conn.Open()
Line 36: connection_open.Text = "Connection open!"
Line 37: DGrid.DataSource = comm.ExecuteReader
Line 38: DGrid.DataBind()
Line 39: connection_command.Text = "Command Run!"Source File: C:\Inetpub\ww wroot\sites\Test\connect_nonDSN.aspx.vb Line:
37Stack Trace:
[NullReferenceException: Object reference not set to an instance of an
object.]
Test.connect_nonDSN.Page_Load(Object sender, EventArgs e) in
C:\Inetpub\ww wroot\sites\Test\connect_nonDSN.aspx.vb:37
System.Web.UI.Control.OnLoad(EventArgs e) +67
System.Web.UI.Control.LoadRecursive() +29
System.Web.UI.Page.ProcessRequestMain() +724
Can someone please tell me where I am going wrong. All my books refer to SQL
server as that is what I usually use.
Thanks,
Chris.
I put the space in the 'ww wroot' to stop the forum code from hyperlinking it.
// This is a generic function that accepts a SQL statement or a stored procedure name and
// performs database activities to return a DataSet
private DataSet ADOGetDataSet(string sSQL, DataSet ds) {
System.Data.SqlClient.SqlDataAdapter sqlDAHistoricalCount = new System.Data.SqlClient.SqlDataAdapter(sSQL,sqlConHistoricalCount);
sqlDAHistoricalCount.Fill(ds);
return ds;
}
Bind the data Set:
DGRID.DataSource = dsDDLData.Tables("Cities").DefaultView
DGRID.DataBind()
or:
sqlDataAdapter1.Fill(dataSet11.Tables[0]);
DataGrid1.DataSource = dataSet11.Tables[0]; DataGrid1.DataBind();