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>");
}
}
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>");
}
}