Error when trying to get fields of a table

G

Guest

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.IPostBackEventHandler.RaisePostBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection 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
 
M

Martin Dechev

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 said:
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.IPostBackEventHandler.RaisePo
stBackEvent(String eventArgument)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top