AL3MS AL3MS - 2 months ago 17
ASP.NET (C#) Question

For dropdownlist footer in grid view (NullReferenceException)

I have a drop down list in the footer in a grid view. I want the it to read from the database since this is for insert.

I got a code for this purpose from here and it works but when I write the same for footer it gives me the error: System.NullReferenceException.

my code is bellow:

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" OnRowDataBound="OnRowDataBound" ShowFooter="True">
<Columns>
<asp:BoundField HeaderText="EId" DataField="EId" />
<asp:TemplateField HeaderText = "CName">
<ItemTemplate>
<asp:Label ID="lblCountry" runat="server" Text='<%# Eval("CName") %>' Visible = "false" />
<asp:DropDownList ID="ddlCountries" runat="server">
</asp:DropDownList>

</ItemTemplate>
<FooterTemplate>

<asp:DropDownList ID="ddlCName" CssClass="auto-style16" Width="80px" runat="server" ></asp:DropDownList>


</FooterTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


code behind:

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GridView1.DataSource = GetData("select * from TbExam inner join TBCourse on TBCourse.CId = TbExam.CId inner join TbCourseMajor on TbCourseMajor.CId=TBCourse.CId");
GridView1.DataBind();
}
}

private DataSet GetData(string query)
{
string conString = ConfigurationManager.ConnectionStrings["RigesterConnectionString1"].ConnectionString;

SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
sda.SelectCommand = cmd;
using (DataSet ds = new DataSet())
{
sda.Fill(ds);
return ds;
}
}
}
}

protected void OnRowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
//Find the DropDownList in the Row
DropDownList ddlCountries = (e.Row.FindControl("ddlCountries") as DropDownList);
ddlCountries.DataSource = GetData("SELECT * FROM TBCourse INNER JOIN TbCourseMajor ON TBCourse.CId = TbCourseMajor.CId ");
ddlCountries.DataTextField = "CName";
ddlCountries.DataValueField = "CId";
ddlCountries.DataBind();

//Add Default Item in the DropDownList
ddlCountries.Items.Insert(0, new ListItem("Please select"));

DropDownList ddlCName = GridView1.FooterRow.FindControl("ddlCName") as DropDownList;
ddlCName.DataSource = GetData("SELECT * FROM TBCourse INNER JOIN TbCourseMajor ON TBCourse.CId = TbCourseMajor.CId ");
ddlCName.DataTextField = "CName";
ddlCName.DataValueField = "CId";
ddlCName.DataBind();

//Add Default Item in the DropDownList
ddlCName.Items.Insert(0, new ListItem("Please select"));

}
}

Answer

Since OnRowDataBound on asp.net GridView fires after every databinding row in Grid and footer row might not be binded...So System.NullReferenceException. is natural ...So instead you can do the following..

After rowDataBound find the Footer in the GridView

and Update database

So complete code

.aspx

<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" 
            ShowFooter="True" ondatabound="GridView1_DataBound">
    <Columns>
      <asp:BoundField HeaderText="EId" DataField="UserId" />
        <asp:TemplateField HeaderText = "CName">
            <ItemTemplate>
                <asp:Label ID="lblCountry" runat="server" Text='<%# Eval("UserId") %>' Visible = "True" />

            </ItemTemplate>
             <FooterTemplate>

              <asp:DropDownList ID="ddlCName"  CssClass="auto-style16" Width="80px" runat="server" >
                  <asp:ListItem Selected="True">1</asp:ListItem>
                  <asp:ListItem>2</asp:ListItem>
                 </asp:DropDownList>


        </FooterTemplate>
        </asp:TemplateField>
    </Columns>
</asp:GridView>

and .cs

protected void Page_Load(object sender, EventArgs e)
    {
        GridView1.DataSource = DbUtilities.GetDataTableFromSqlQuery("select top 10 * from Users");
        GridView1.DataBind();
    }

    protected void GridView1_DataBound(object sender, EventArgs e)
    {
        GridViewRow row = GridView1.FooterRow;

        var firstName = ((DropDownList)row.Cells[1].FindControl("ddlCName")).SelectedValue;
        var s = firstName;
    }