473,395 Members | 2,253 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

Error when trying to get fields of a table

When I try to display the fields associated with a table using the following
code:

private void fieldsButton_Click(object sender, System.EventArgs e)
{

string tableName = tablesList.SelectedItem.ToString();
string con = connectLabel.Text;
cn = new System.Data.OleDb.OleDbConnection(con);
cn.Open();

System.Data.DataTable schemaTable;

schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,
new Object[] {null, null, tableName, null});

for (int i = 0; i < schemaTable.Rows.Count; i++)
{
fieldsList.DataSource = schemaTable;
fieldsList.DataTextFormatString = "COLUMN_NAME";
fieldsList.DataBind();
}

cn.Close();

}

I get the following error:

Server Error in '/DataViewer' 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 91: {
Line 92:
Line 93: string tableName = tablesList.SelectedItem.ToString();
Line 94: string con = connectLabel.Text;
Line 95: cn = new System.Data.OleDb.OleDbConnection(con);
Source File: c:\inetpub\wwwroot\dataviewer\defaultform.aspx.cs Line: 93

Stack Trace:
[NullReferenceException: Object reference not set to an instance of an
object.]
DataViewer.defaultForm.fieldsButton_Click(Object sender, EventArgs e) in
c:\inetpub\wwwroot\dataviewer\defaultform.aspx.cs: 93
System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData)
System.Web.UI.Page.ProcessRequestMain()


--------------------------------------------------------------------------------
Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET
Version:1.1.4322.573

How do I need to alter the code to make this error go away?

Thanks,

Dave


Nov 16 '05 #1
1 1250
Hi, Dave,

Most probably there is no item selected in tablesList. Try checking
if(tablesList.SelectedIndex > -1).

Also it might be that tablesList is NullReference - check the declaration
and the syntax in the html part of the page.

Hope this helps
Martin
"kscdavefl" <ks*******@discussions.microsoft.com> wrote in message
news:D9**********************************@microsof t.com...
When I try to display the fields associated with a table using the following code:

private void fieldsButton_Click(object sender, System.EventArgs e)
{

string tableName = tablesList.SelectedItem.ToString();
string con = connectLabel.Text;
cn = new System.Data.OleDb.OleDbConnection(con);
cn.Open();

System.Data.DataTable schemaTable;

schemaTable = cn.GetOleDbSchemaTable(OleDbSchemaGuid.Columns,
new Object[] {null, null, tableName, null});

for (int i = 0; i < schemaTable.Rows.Count; i++)
{
fieldsList.DataSource = schemaTable;
fieldsList.DataTextFormatString = "COLUMN_NAME";
fieldsList.DataBind();
}

cn.Close();

}

I get the following error:

Server Error in '/DataViewer' 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 91: {
Line 92:
Line 93: string tableName = tablesList.SelectedItem.ToString();
Line 94: string con = connectLabel.Text;
Line 95: cn = new System.Data.OleDb.OleDbConnection(con);
Source File: c:\inetpub\wwwroot\dataviewer\defaultform.aspx.cs Line: 93

Stack Trace:
[NullReferenceException: Object reference not set to an instance of an
object.]
DataViewer.defaultForm.fieldsButton_Click(Object sender, EventArgs e) in c:\inetpub\wwwroot\dataviewer\defaultform.aspx.cs: 93
System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPo stBackEventHandler.RaisePo
stBackEvent(String eventArgument) System.Web.UI.Page.RaisePostBackEvent(IPostBackEve ntHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCol lection postData)
System.Web.UI.Page.ProcessRequestMain()


-------------------------------------------------------------------------- ------ Version Information: Microsoft .NET Framework Version:1.1.4322.573; ASP.NET Version:1.1.4322.573

How do I need to alter the code to make this error go away?

Thanks,

Dave

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Frances | last post by:
Hi All, I'm having a problem trying to add a record to a simple Access 2000 db (db is very similar to an address book but with more info than the usual address, phone, etc.). The database is...
8
by: Raven | last post by:
Hello, I get a error message when I try to Add data in a relational Access Database. This is the error message: "You cannot add or change a record because a related record is required in...
0
by: Megan | last post by:
Hi Everybody- I know that this is a really, really long post, but I wanted to try to give you as much background as possible. So here's a quick overview of the issues I'm asking for help with:...
14
by: Abhi | last post by:
FYI: This message is for the benefit of MS Access Community. I found that this prblem has been encounterd by many but there is hardly any place where a complete solution is posted. So I thought...
5
by: Richard Stanton | last post by:
Hello all My database has a main form linked to table1. It has several subforms on the main form, all linked to table2. Table1 and Table2 are linked by primary/foreign key, no duplicates...
0
by: Roman | last post by:
I'm trying to create the form which would allow data entry to the Client table, as well as modification and deletion of existing data rows. For some reason the DataGrid part of functionality stops...
0
by: Leon | last post by:
I keep geting this error in the IE when trying to run the scrept below from an ASP page. Can any one help please. Error Type: Provider (0x80040E37) Table does not exist. /gal.asp, line 43
9
by: Ecohouse | last post by:
I have a main form with two subforms. The first subform has the child link to the main form identity key. subform1 - Master Field: SK Child Field: TrainingMasterSK The second subform has a...
0
by: hussainiyad | last post by:
Dear All i,m totlally new for vb 6.0 and i,m doing a invoice project and i,m facing a issue , can you plz tell me solution i,m trying to retrive some datas from sql (Product table) it...
3
by: graphicssl | last post by:
Okay, so first of all, I'm a designer first and a light coder second (I'm only really trained with HTML and CSS). So I apologize for having to post about something that's probably super-trivial! ...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.