简体   繁体   中英

DataTable works in WinForms but not in ASP.NET

I have created a class that returns a datatable, when I use the class in ac# winform the dataGridView is populated correctly using the following code

DataTable dt = dbLib.GetData();
if (dt != null)
{
  dataGridView1.DataSource = dbLib.GetData();
}

However when I try the same thing with ASP.NET I get a

Object reference not set to an instance of an object.

using the following code

DataTable dt = dbLib.GetData();
if (dt != null)
{
  GridView1.DataSource = dt;
  GridView1.DataBind();
}

the dbLib class GetData2 is there to prove that it is nothing caused by SQlite or the data

public static DataTable GetData()
{
  SQLiteConnection cnn = new SQLiteConnection("Data Source=c:\\test.db");
  SQLiteCommand cmd = new SQLiteCommand("SELECT count(Message) AS Occurrences, Message FROM evtlog GROUP BY Message ORDER BY Occurrences DESC LIMIT 25", cnn);
  cnn.Open();
  SQLiteDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
  DataTable dt = new DataTable();
  dt.Load(dr);
  return dt;
}

public static DataTable GetData2()
{
  DataTable dt = new DataTable();
  dt.Columns.Add(new DataColumn("Occurrences", typeof(string)));
  dt.Columns.Add(new DataColumn("Message", typeof(string)));

  DataRow dataRow = dt.NewRow();
  dataRow["Occurrences"] = "1";
  dataRow["Message"] = "a";
  dt.Rows.Add(dataRow);
  return dt;
}

the asp code

<asp:GridView ID="GridView1" runat="server">
  <Columns>
    <asp:BoundField DataField="Occurrences" HeaderText="Occurrences"></asp:BoundField>
    <asp:BoundField DataField="Message" HeaderText="Message"></asp:BoundField>
  </Columns>
</asp:GridView>

The NullReferenceException is thrown because you have a null reference to GridView1 . This could be cause by a couple of reasons.

  1. You are attempting to access GridView1 at the wrong point in the Asp.net Page Life cycle . Possibly in the constructor of the page? If this is the case moving the logic to PageLoad will fix your problem.
  2. The markup page (.aspx) is out of sync with the code behind page (.aspx.cs/.aspx.designer.cs) causing the GridView1 to never be instantiated. If this is the case the best option is to remove the gridview from the markup and re-add it.

It probably depends on a connection string that is not present in your asp.net web.config. It's hard to say, because we cannot actually see what your dbLib does.

change the code to below, put a break point on the line throw new Exception("Error in GetData()", ex); and let us know what the actual exception is. I suspsect the ASPNET worker process account doesnt have rights to open your DB.

public static DataTable GetData() 
{ 
  try
  {
    SQLiteConnection cnn = new SQLiteConnection("Data Source=c:\\test.db"); 
    SQLiteCommand cmd = new SQLiteCommand("SELECT count(Message) AS Occurances, Message FROM evtlog GROUP BY Message ORDER BY Occurances DESC LIMIT 25", cnn); 
    cnn.Open(); 
    SQLiteDataReader dr = cmd.ExecuteReader(CommandBehavior.CloseConnection); 
    DataTable dt = new DataTable(); 
    dt.Load(dr); 
    return dt; 
  }
  catch (Exception ex)
  {
    throw new Exception("Error in GetData()", ex);
  }
} 

Change your code to this:

DataTable dt = dbLib.GetData();
GridView1.DataSource = dt;
GridView1.DataBind();

In the debugger when you reach GridView1.DataSource = dt; see if dt is null. This will allow you to determine whether the problem lies with the GridView or the method. If it is the gridview, then try removing EnableModelValidation and seeing if the exception goes away. Post the results of the above tests and we will help investigate further.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM