简体   繁体   English

DataTable在WinForms中起作用,但在ASP.NET中不起作用

[英]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 我创建了一个返回数据表的类,当我在ac#winform中使用该类时,使用以下代码正确填充了dataGridView

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

However when I try the same thing with ASP.NET I get a 但是,当我使用ASP.NET尝试相同的操作时,我得到了

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 dbLib类GetData2可以证明它不是由SQlite或数据引起的

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代码

<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 . 抛出NullReferenceException的原因是您对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 . 您试图在Asp.net页面生命周期的错误点访问GridView1 Possibly in the constructor of the page? 可能在页面的构造函数中? If this is the case moving the logic to PageLoad will fix your problem. 如果是这种情况,将逻辑移至PageLoad将解决您的问题。
  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. 标记页面(.aspx)与页面后面的代码(.aspx.cs / .aspx.designer.cs)不同步,导致GridView1永远不会被实例化。 If this is the case the best option is to remove the gridview from the markup and re-add it. 如果是这种情况,最好的选择是从标记中删除gridview并重新添加它。

It probably depends on a connection string that is not present in your asp.net web.config. 它可能取决于asp.net web.config中不存在的连接字符串。 It's hard to say, because we cannot actually see what your dbLib does. 很难说,因为我们实际上看不到您的dbLib做什么。

change the code to below, put a break point on the line throw new Exception("Error in GetData()", ex); 将代码更改为下面的代码,在断点处放置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. 我怀疑ASPNET辅助进程帐户没有打开数据库的权限。

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; 在调试器中到达GridView1.DataSource = dt; see if dt is null. 查看dt是否为null。 This will allow you to determine whether the problem lies with the GridView or the method. 这将使您能够确定问题是否出在GridView或方法上。 If it is the gridview, then try removing EnableModelValidation and seeing if the exception goes away. 如果是gridview,请尝试删除EnableModelValidation并查看异常是否消失。 Post the results of the above tests and we will help investigate further. 发布以上测试的结果,我们将帮助进一步调查。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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