Getting object reference to instance error in row data bound

Oct 5 2018 9:14 AM
Hello Everyone,
I am getting object reference to instance error in row data bound event in gridview while performing sorting for huge count of records i am storing datatable in a session
and using it in gridview sorting method after databinding it is entering in row data bound event here i am getting this error. tried for 788 count of records it is working
correctly. when i tried for 30,000 records count, i got this error. i tried to increse session timeout as well but still getting this error. is this issue is because storing
datatable in a session? or do i need to call the database again for this operation?
is there any way of doing sorting efficiently for bulk count of records in gridview.
Any help is appreciated. Thanks in advance.
 
Anyone please help me on this issue.
Below i am attaching sample version of my code please look at it.
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
refreshdata();
}
}
public void refreshdata()
{
SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
SqlCommand cmd = new SqlCommand("select * from tbl_data", con);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
ViewState["dirState"] = dt;
ViewState["sortdr"] = "Asc";
}
protected void GridView1_Sorting(object sender, GridViewSortEventArgs e)
{
DataTable dtrslt= (DataTable)ViewState["dirState"];
if (dtrslt.Rows.Count > 0)
{
if (Convert.ToString(ViewState["sortdr"]) == "Asc")
{
dtrslt.DefaultView.Sort = e.SortExpression + " Desc";
ViewState["sortdr"] = "Desc";
}
else
{
dtrslt.DefaultView.Sort = e.SortExpression + " Asc";
ViewState["sortdr"] = "Asc";
}
GridView1.DataSource = dtrslt;
GridView1.DataBind();
}
}
protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
SqlCommand cmd = new SqlCommand("SELECT DISTINCT(Country) FROM Customers");
DropDownList ddlCountries = (e.Row.FindControl("ddlCountries") as DropDownList);
ddlCountries.DataSource = this.ExecuteQuery(cmd, "SELECT");
ddlCountries.DataTextField = "Country";
ddlCountries.DataValueField = "Country";
ddlCountries.DataBind();
string country = (e.Row.FindControl("lblCountry") as Label).Text;
ddlCountries.Items.FindByValue(country).Selected = true;
}
}