ASP.NET - bind data to Gridview Footerrow control dropdownlist

Asked By Gunasundari Loganathan on 18-Mar-13 02:23 AM
Hi i have a dropdownlist inside a footer row of Gridview. i need to bind data to the dropdownlist. I used the following code,

 public void Add()
    {
        con.Open();
        DropDownList dsproduct = (DropDownList)GV_QuoteDom.FooterRow.FindControl("Dd_Productdom");
        SqlDataAdapter da_add = new SqlDataAdapter("Select ProductId,ProductName from ProductRequisition_Domestic where ProductId ='" + Session["AddPID"].ToString() + "'", con);
        DataSet ds_add = new DataSet();
        da_add.Fill(ds_add, "ProductRequisition_Domestic");
        if (ds_add.Tables[0].Rows.Count != 0)
        {
            dsproduct.DataSource = ds_add;
            dsproduct.DataTextField = "ProductName";
            dsproduct.DataValueField = "ProductId";
            dsproduct.DataBind();
            dsproduct.Items.Insert(0, "Select");
        }
        con.Close();
  }  when iused this it shows the following error Thanks in advance


Object reference not set to an instance of an object.

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

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

Source Error:

Line 204:    {
Line 205:        con.Open();
Line 206:        DropDownList dsproduct = (DropDownList)GV_QuoteDom.FooterRow.FindControl("Dd_Productdom");
Line 207:        SqlDataAdapter da_add = new SqlDataAdapter("Select ProductId,ProductName from ProductRequisition_Domestic where ProductId ='" + Session["AddPID"].ToString() + "'", con);
Line 208:        DataSet ds_add = new DataSet();

Source File: e:\Inventory1\QuotationEntry_Domestic.aspx.cs    Line: 206

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an object.]
   QuotationEntry_Domestic.Add() in e:\Inventory1\QuotationEntry_Domestic.aspx.cs:206
   QuotationEntry_Domestic.GV_productsdom_RowCommand(Object sender, GridViewCommandEventArgs e) in e:\Inventory1\QuotationEntry_Domestic.aspx.cs:230
   System.Web.UI.WebControls.GridView.OnRowCommand(GridViewCommandEventArgs e) +111
   System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +73
   System.Web.UI.WebControls.GridView.OnBubbleEvent(Object source, EventArgs e) +89
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +88
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +37
   System.Web.UI.WebControls.Button.OnCommand(CommandEventArgs e) +114
   System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +159
   System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
   System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
Rohan Dave replied to Gunasundari Loganathan on 19-Mar-13 03:12 AM
You need to write code or call a method inside "Row_DataBound" event of grid view. So your code looks like below..


if (e.Row.RowType == DataControlRowType.Footer)
{
            DropDownList objddlCity= (DropDownList)GridView1.FooterRow.FindControl("ddlCity");
            objDt = new DataTable ();
            objAdpt = new SqlDataAdapter("Select CityID, CityName from tblCity", objCon);
    objAdpt.Fill (objDt);
            objddlCity.DataSource = objDt;
            objddlCity.DisplayMember = "CityName";
    objddlCity.ValueMember = "CityID";
            objddlCity.DataBind();
}

that's it.