2
Answers

[C# net4] GridView empty

Hello, hope in your help.

My problem is when the GridView is empty in the aspx page I've this error:

Object reference not set to an instance of an object on:


protected void OnDataBound(object sender, EventArgs e)
{
sql = "SELECT ";
sql = sql + " denom
sql = sql + "FROM ";
sql = sql + " tbl_xxx; ";

DropDownList Newcombo = DecoderView.FooterRow.FindControl("Newcombo") as DropDownList;
Newcombo.DataSource = GetData(sql);
Newcombo.DataTextField = "denom";
Newcombo.DataValueField = "denom";
Newcombo.DataBind();
Newcombo.Items.Insert(0, new ListItem("------", ""));
}


in this line:

DropDownList Newcombo = DecoderView.FooterRow.FindControl("Newcombo") as DropDownList;


If I've gridview records to show the aspx page is working ...

Can you help me?
Thank you in advance

Attachment: cs.zip

Answers (2)

0
Photo of Chevy Mark Sunderland
NA 188 170.3k 11y
Thank you but the error is persistent.

Object reference not set to an instance of an object on:


protected void OnDataBound(object sender, EventArgs e)
{
sql = "SELECT ";
sql = sql + " denom
sql = sql + "FROM ";
sql = sql + " tbl_xxx; ";

DropDownList Newcombo = DecoderView.FooterRow.FindControl("Newcombo") as DropDownList;
Newcombo.DataSource = GetData(sql);
Newcombo.DataTextField = "denom";
Newcombo.DataValueField = "denom";
Newcombo.DataBind();
Newcombo.Items.Insert(0, new ListItem("------", ""));
}


in this line:

DropDownList Newcombo = DecoderView.FooterRow.FindControl("Newcombo") as DropDownList;

0
Photo of Iftikar Hussain
NA 12.6k 452.2k 11y
Hi,
        Remove the DecoderView.DataBind(); from all place and use the below code

protected override void OnPreRenderComplete(EventArgs e)
{
            DataBind();
            base.OnPreRenderComplete(e);
}

Regards,
Iftikar