0

my Gridview always get null datatable ,error message:Object reference not set to an instance of an object., but i donot know which code is missing.
Thanks for help!!!

.CS

protected void btn_Click(object sender, EventArgs e)
{
        DataSet ds = new DataSet();
        DataTable dt = new DataTable("abc");
        dt = getDT(formatDate); //string[] formatDate ={2013-01-01,2013-05-01..}
        ds.Tables.Add(dt);
        GridView2.DataSource = ds.Tables["abc"].DefaultView; 
         //if change to dt , it works ==> GridView2.DataSource =dt;
        GridView2.DataBind();
    }

}

private DataTable getDT(string[] date)
{
    DataTable dt = new DataTable();

    dt.Columns.Add("RowID", typeof(Int16));
    dt.Columns.Add("Date", typeof(DateTime));

    for (int i = 0; i < date.Length; i++)
    {
        dt.Rows.Add(i + 1, date[i]);
    }
    return dt;
}
bbqwings
  • 85
  • 1
  • 4
  • 15

2 Answers2

3

Because of the code -

Protected void btn_Click(object sender, EventArgs e)
{
        DataSet ds = new DataSet();
        DataTable dt = new DataTable("abc");
        dt = getDT(formatDate);
        ds.Tables.Add(dt);
        GridView2.DataSource = ds.Tables["abc"].DefaultView;
        GridView2.DataBind();
    }

}

private DataTable getDT(string[] date)
{
    DataTable dt = new DataTable();

    dt.Columns.Add("RowID", typeof(Int16));
    dt.Columns.Add("Date", typeof(DateTime));

    for (int i = 0; i < date.Length; i++)
    {
        dt.Rows.Add(i + 1, date[i]);
    }
    return dt;
}

Inside the getDT function you are again creating a new DataTable which does not have a name. You should either send the table as reference or create it inside the function. Either of the following solution will work -

Protected void btn_Click(object sender, EventArgs e)
{
        DataSet ds = new DataSet();
        DataTable dt = new DataTable("abc");
        dt = getDT(dt, formatDate);
        ds.Tables.Add(dt);
        GridView2.DataSource = ds.Tables["abc"].DefaultView;
        GridView2.DataBind();
    }

}

private DataTable getDT(DataTable dt, string[] date)
{
    //DataTable dt = new DataTable();

    dt.Columns.Add("RowID", typeof(Int16));
    dt.Columns.Add("Date", typeof(DateTime));

    for (int i = 0; i < date.Length; i++)
    {
        dt.Rows.Add(i + 1, date[i]);
    }
    return dt;
}

OR,

Protected void btn_Click(object sender, EventArgs e)
{
        DataSet ds = new DataSet();
        DataTable dt = getDT(formatDate);
        ds.Tables.Add(dt);
        GridView2.DataSource = ds.Tables["abc"].DefaultView;
        GridView2.DataBind();
    }

}

private DataTable getDT(string[] date)
{
    DataTable dt = new DataTable("abc");

    dt.Columns.Add("RowID", typeof(Int16));
    dt.Columns.Add("Date", typeof(DateTime));

    for (int i = 0; i < date.Length; i++)
    {
        dt.Rows.Add(i + 1, date[i]);
    }
    return dt;
}

EDIT: Only for HassanNisar @HassanNisar .. this is the debugger output -

enter image description here

brainless coder
  • 6,310
  • 1
  • 20
  • 36
2

You should pass name of DataTable you are creating. By default DataTable does not have name, that's why you can't get it by name abc.

private DataTable getDT(string name, string[] date)
{
    DataTable dt = new DataTable(name);

    dt.Columns.Add("RowID", typeof(Int16));
    dt.Columns.Add("Date", typeof(DateTime));

    for (int i = 0; i < date.Length; i++)    
        dt.Rows.Add(i + 1, date[i]);        

    return dt;
}

Then creating table will look like:

ds.Tables.Add(getDT("abc", formatDate));

Also you can add null-check for date parameter.


NOTE: I suggest you to improve naming of your code. See C# Naming Guidelines. And you don't need to use DataSet here - simply bind to DataTable:

private DataTable CreateDatesTable(IEnumerable<DateTime> dates)
{
    DataTable dt = new DataTable("Dates");

    dt.Columns.Add("RowID", typeof(Int16));
    dt.Columns.Add("Date", typeof(DateTime));

    foreach(var date in dates)        
       dt.Rows.Add(dt.Rows.Count + 1, date); 

    return dt;
}

Usage:

protected void btn_Click(object sender, EventArgs e)
{
    var dates = formattedDates.Select(DateTime.Parse);
    GridView2.DataSource = CreateDatesTable(dates);
    GridView2.DataBind();
}
Sergey Berezovskiy
  • 232,247
  • 41
  • 429
  • 459