views:

89

answers:

4

I have created a class that returns a datatable, when I use the class in a c# 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>
A: 

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.

PieterG
for testing purposes, the dblib does the connection and query and returns a datatable type. I did this to prove that the library was working in c# but not asp.net
Charles Gargent
A: 

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);
  }
} 
Mauro
GetData doesnt throw an exception, the exception is thrown in the calling code. also the library works for c# it returns a datatable and it works in ASP.NET, when debugging, I can see the dt object is populated. But just to make sure I did do what you suggested but as I thought it wasnt the library that failed.
Charles Gargent
A: 

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.

Ben Robinson
I added an "if (dt != null)" clause, and dt is not null. I removed the EnableModelValidation tags and still the same error.
Charles Gargent
Binding data tables to gridviews in asp.net WORKS, there must be something more going on here such as the SQLiteDataReader is creating a dodgy data table some how. Try binding the grid view to a table you have created manually, with one row and one column with a simple piece of text in it.
Ben Robinson
@Ben I have updated to my question in response to your comment
Charles Gargent
A: 

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.
Aaron Carlson
I give you the marks for this, it was because my gridview was in the wrong aspx code
Charles Gargent