Error?? DataTable

K

Kelvin Nguyen

Would anybody please help me out?



I think my code is correct but it gave me an error I don't know what it is
please correct me.





using System;

using System.Collections;

using System.ComponentModel;

using System.Data;

using System.Drawing;

using System.Web;

using System.Web.SessionState;

using System.Web.UI;

using System.Web.UI.WebControls;

using System.Web.UI.HtmlControls;

using System.Data.OleDb;

namespace Energy

{

/// <summary>

/// Summary description for tblProjectDetails.

/// </summary>

public class tblProjectDetails : System.Web.UI.Page

{

private void Page_Load(object sender, System.EventArgs e)

{

DataFromSourceToMemory("myQuestion");

}

private void DataFromSourceToMemory(string strDataSessionName)

{

// Gets rows from the data source

DataSet oDS = PhysicalDataRead();


// Stores it in the session cache

Session[strDataSessionName] = oDS;

getQuestions(oDS,strDataSessionName);


}

private DataSet PhysicalDataRead()

{

// Creates connection and command string

string mystrConn, mystrCmd;

mystrConn = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" +
Server.MapPath("dcpols.mdb");

mystrCmd = "SELECT * FROM tblQuestionLU WHERE Q_PT_PKF= 9";

OleDbDataAdapter oCMD = new OleDbDataAdapter(mystrCmd, mystrConn);

DataSet oDS = new DataSet();

oCMD.Fill(oDS, "MyProjectQuestions");

return oDS;

}

private void getQuestions(DataSet ds,string str)

{

//ArrayList myQuestionList = new ArrayList();

DataTable myQuestionTable = ds.Tables[str];


foreach(DataRow myRow in myQuestionTable.Rows)

{

foreach(DataColumn myCol in myQuestionTable.Columns)

{

Response.Write(myRow[myCol].ToString());

Response.Write("<br>");


}

}

**************************************************************

And this is the error................





Server Error in '/Energy' 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 52: DataTable myQuestionTable = ds.Tables[str];
Line 53:
Line 54: foreach(DataRow myRow in myQuestionTable.Rows)
Line 55: {
Line 56: foreach(DataColumn myCol in myQuestionTable.Columns)
 

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