Problem with an Access query with parameters

M

Maury

Hy,
I have a problem showing results from a query with parameters (in Access) into a datagrid, if I use this code
I receive a System.NullReferenceException

DataTable myDt = new DataTable();

OleDbConnection myConnection = new OleDbConnection(ConfigurationSettings.AppSettings["strDB"]);
OleDbCommand myCommand = new OleDbCommand();
myCommand.Parameters.Add("Data", DayToShow); //DayToShow is a variable of type DateTime
myCommand.Connection = myConnection;
myCommand.CommandText = "myQuery";
myCommand.CommandType = CommandType.StoredProcedure;

OleDbDataAdapter myAdapt = new OleDbDataAdapter(myCommand);
myAdapt.Fill(myDt);
myDt.PrimaryKey = new DataColumn[] {myDt.Columns[0]};

if (myDt.Rows.Count>0)
{
dgDD.DataSource = myDt; //THIS ROW THROWS System.NullReferenceException
dgDD.DataBind();
}

But instead if I write the following code I see my data without errors....WHY?
Can someone help me? Thanks!

if (myDt.Rows.Count>0)
{
for (int j=0; j<myDt.Rows.Count; j++)
{
for (int i=0; i<myDt.Rows[j].ItemArray.Length; i++)
{
Response.Write(myDt.Rows[j].ItemArray.GetValue(i).ToString()+"-");
}
Response.Write("<br>");
}
}
 

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

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,583
Members
45,073
Latest member
DarinCeden

Latest Threads

Top