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