Forum Moderators: open

Message Too Old, No Replies

Choice of no results or an error

Please Help. I'm stuck.

         

chris_f

1:26 pm on Jun 15, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can someone please help me. This has been bugging me for ages. I am using asp.net and vb.net to connect to a Microsoft Access 2000 database. This is for a web application using code behind. Can some check over my code please.

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.OleDb

Public 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 here

Dim 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:
37

Stack 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.

chris_f

9:55 am on Jun 16, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Little help please.

txbakers

12:53 pm on Jun 16, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Sorry, I don't know .NET well enough to debug this for you. All I know is that it works for me.

brotherhood of LAN

1:43 pm on Jun 16, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This is something I am unfamiliar with, sorry I can't help but...

>>>>>>C:\Inetpub\ww wroot

is the space between the W's just something that you changed to post here or maybe this is causing a problem

andrey_sea

5:02 pm on Jun 17, 2002 (gmt 0)

10+ Year Member



Did you try using OleDbDataAdapter?
System.Data.OleDb.OleDbDataAdapter
You can change this code for OleDb and VB.NET

// 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();

tomasz

4:10 pm on Jul 4, 2002 (gmt 0)

10+ Year Member



Ad to code behind

Protected WithEvents DGrid As System.Web.UI.WebControls.DataGrid

and then you can use your datagrid without 'New'

chris_f

4:39 pm on Jul 4, 2002 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It was a .Net/WinXP security issue. It's solved now but thanks for the help guys.

Chris.