简体   繁体   中英

GridView in asp.net c# with paging

I have problem on a GridView in asp net c# with paging

This GridView is populated with the query where is called one value passed in querystring.

In first bind the output in GridView is correct, but when I try on change page the GridView response with below error.

Exception Details: System.NullReferenceException: Object reference not 
set to an instance of an object.

In this line in public DataTable GridViewBind()

throw ex;

I think that the error is caused from the value in querystring that is lost in reloading the page.

My code below, I would greatly appreciate any help you can give me in working this problem.

public DataTable GridViewBind()
{
        sql = " SELECT * from tbl_premium WHERE Year_month = ?; ";

    try
    {
        dadapter = new OdbcDataAdapter(sql, conn);

        if (!string.IsNullOrEmpty(Request.QueryString["Year_month"].ToString()))
        {
            dadapter.SelectCommand.Parameters.Add("param1", Request.QueryString["Year_month"].ToString());
        }

        dset = new DataSet();
        dset.Clear();
        dadapter.Fill(dset);
        DataTable dt = dset.Tables[0];
        GridView1.DataSource = dt;

        GridView1.DataBind();
        return dt;
    }
    catch (Exception ex)
    {
        throw ex;
    }
    finally
    {
        dadapter.Dispose();
        dadapter = null;
        conn.Close();
    }
}


protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
    GridViewBind();
    GridView1.DataSource = dset.Tables[0];
    GridView1.PageIndex = e.NewPageIndex;
    GridView1.DataBind();

}

protected void ddlPages_SelectedIndexChanged(object sender, EventArgs e)
{
    GridViewRow gvrPager = GridView1.BottomPagerRow;
    DropDownList ddlPages = (DropDownList)gvrPager.Cells[0].FindControl("ddlPages");
    GridViewBind();
    GridView1.DataSource = dset.Tables[0];
    GridView1.PageIndex = ddlPages.SelectedIndex;
    GridView1.DataBind();

}

protected void Paginate(object sender, CommandEventArgs e)
{
    int intCurIndex = GridView1.PageIndex;

    switch (e.CommandArgument.ToString().ToLower())
    {
        case "First":
            GridView1.PageIndex = 0;
            break;
        case "Prev":
            GridView1.PageIndex = intCurIndex - 1;
            break;
        case "Next":
            GridView1.PageIndex = intCurIndex + 1;
            break;
        case "Last":
            GridView1.PageIndex = GridView1.PageCount - 1;
            break;
    }
    GridView1.DataBind();
}

Most likely, this line:

if (!string.IsNullOrEmpty(Request.QueryString["Year_month"].ToString()))

is running into an exception.

Please make sure you pass Year_month as a parameter in your paging url so that it works when re-binding the gridview.

Another thing that you may try, (as a fail-safe) is to check if Request.QueryString["Year_month"] is not NULL . Try to convert it to String value only if it is not null.

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