Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 203
  • Last Modified:

Trying to populate a dropdown list nested in a gridview

I have a GridView, and in there I have a combo box:
<asp:GridView ID ="gvResults" runat ="server" 
    AutoGenerateColumns ="False" 
    BackColor ="White" 
    BorderColor ="#CCCCCC" 
    BorderStyle ="None" 
    BorderWidth ="1px" CellPadding ="3"
    OnRowEditing="gvResults_RowEditing"
    OnRowDataBound="gvResults_RowDataBound"
    ShowFooter="True"> 

~~~~code~~~~

<asp:TemplateField HeaderText="IP2" HeaderStyle-HorizontalAlign="Left"> 
            <EditItemTemplate> 
                <asp:DropDownList ID="ddlIP2" runat="server" DataValueField="Server_IP" /> 
            </EditItemTemplate> 
            <ItemTemplate> 
                <asp:Label ID="lblIP2" runat="server" Text='<%# Eval("SErver_IP") %>'></asp:Label> 
            </ItemTemplate> 
            <FooterTemplate> 
                <asp:DropDownList ID="ddlIP2" runat="server" DataValueField="Server_IP" />
            </FooterTemplate> 
        </asp:TemplateField> 

~~~~more code~~~~

Open in new window



Now for the Codebehind:
    protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
    {
        DropDownList cmbNewType = (DropDownList)e.Row.FindControl("ddlIP2");
        string sqlQuery = "SELECT Server_IP from fulllist";
        SqlConnection conn = new SqlConnection("Data Source=MyServer;Initial Catalog=MyDatabase;Integrated Security=True");
        SqlDataAdapter sda = new SqlDataAdapter(sqlQuery, conn);
        DataTable dtData = new DataTable();

        DropDownList cmbType = (DropDownList)e.Row.FindControl("ddlIP2");
        cmbType.DataSource = dtData.DefaultView;
        cmbType.DataTextField = "Server_IP";
        cmbType.DataValueField = "Server_IP";
        cmbType.DataBind();
        gvResults.DataKeys[e.Row.RowIndex].Values[1].ToString();
    }

Open in new window


I get the following error when trying to render the page:
Server Error in '/' Application.
--------------------------------------------------------------------------------

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 52: 
Line 53:         DropDownList cmbType = (DropDownList)e.Row.FindControl("ddlIP2");
Line 54:         cmbType.DataSource = dtData.DefaultView;
Line 55:         cmbType.DataTextField = "Server_IP";
Line 56:         cmbType.DataValueField = "Server_IP";
 

Source File: c:\webroot\myDV.aspx.cs    Line: 54 

Stack Trace: 


[NullReferenceException: Object reference not set to an instance of an object.]
   myDV.gvResults_RowDataBound(Object sender, GridViewRowEventArgs e) in c:\webroot\myDV.aspx.cs:54
   System.Web.UI.WebControls.GridView.CreateRow(Int32 rowIndex, Int32 dataSourceIndex, DataControlRowType rowType, DataControlRowState rowState, Boolean dataBind, Object dataItem, DataControlField[] fields, TableRowCollection rows, PagedDataSource pagedDataSource) +281
   System.Web.UI.WebControls.GridView.CreateChildControls(IEnumerable dataSource, Boolean dataBinding) +2167
   System.Web.UI.WebControls.CompositeDataBoundControl.PerformDataBinding(IEnumerable data) +71
   System.Web.UI.WebControls.GridView.PerformDataBinding(IEnumerable data) +17
   System.Web.UI.WebControls.DataBoundControl.OnDataSourceViewSelectCallback(IEnumerable data) +146
   System.Web.UI.WebControls.DataBoundControl.PerformSelect() +260
   myDV.FillGrid() in c:\webroot\myDV.aspx.cs:42
   myDV.gvResults_RowEditing(Object sender, GridViewEditEventArgs e) in c:\webroot\myDV.aspx.cs:64
   System.Web.UI.WebControls.GridView.HandleEdit(Int32 rowIndex) +51
   System.Web.UI.WebControls.GridView.HandleEvent(EventArgs e, Boolean causesValidation, String validationGroup) +693
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +69
   System.Web.UI.WebControls.GridViewRow.OnBubbleEvent(Object source, EventArgs e) +109
   System.Web.UI.Control.RaiseBubbleEvent(Object source, EventArgs args) +69
   System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +28
   System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +2980

Open in new window


I know I'm missing some fundamental concept, its been years since I last used ASP.NET
 
0
Mike-Po
Asked:
Mike-Po
  • 3
  • 3
  • 3
1 Solution
 
Ramkisan JagtapLead DeveloperCommented:
i think ur cmbtype object is null. It is unable to find ur dropdown list. Try to debug the code and find why it is coming null.
0
 
jagssiduralaCommented:
Change your code like below

protected void gvResults_RowDataBound(object sender, GridViewRowEventArgs e)
    {
      if(e.Row.RowType == DataControlRowType.DataRow)
    {

        DropDownList cmbNewType = (DropDownList)e.Row.FindControl("ddlIP2");
        string sqlQuery = "SELECT Server_IP from fulllist";
        SqlConnection conn = new SqlConnection("Data Source=MyServer;Initial Catalog=MyDatabase;Integrated Security=True");
        SqlDataAdapter sda = new SqlDataAdapter(sqlQuery, conn);
        DataTable dtData = new DataTable();

        DropDownList cmbType = (DropDownList)e.Row.FindControl("ddlIP2");
        cmbType.DataSource = dtData.DefaultView;
        cmbType.DataTextField = "Server_IP";
        cmbType.DataValueField = "Server_IP";
        cmbType.DataBind();
        gvResults.DataKeys[e.Row.RowIndex].Values[1].ToString();
}
    }


Refer these links

http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowdatabound.aspx

http://forums.asp.net/t/1346686.aspx/1
0
 
Mike-PoAuthor Commented:
I did noice that I forgot to actually fill the dtData with sda.Fill(dtData).

I have implimpented the if statement (didnt even think about headers and footers). Still no joy, I am currently configuring the webserver to allow for debugging with VS. I will persue that avenue.

Thanks for the input so far!
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
Ramkisan JagtapLead DeveloperCommented:
before binding chk cmbType & dtData for null.
0
 
Mike-PoAuthor Commented:
THe dtData is getting filled, however cmbType is staying null. So somewhere I'm not refrencing the dropdown list ddlIP2 correctly.
0
 
jagssiduralaCommented:
try with below condition

 if (e.Row.RowType == DataControlRowType.DataRow && 
        (e.Row.RowState == DataControlRowState.Edit)
    {
        // Find control
       
    }
}
0
 
jagssiduralaCommented:
Refer this link

http://www.codeproject.com/KB/webforms/editable_gridview_control.aspx

try this condition

if (e.Row.RowType == DataControlRowType.DataRow)
   {
   if ((e.Row.RowState & DataControlRowState.Edit) > 0)
   {

//find your control
}
}
0
 
Ramkisan JagtapLead DeveloperCommented:
u have used edittemplate so chk the rowstate and then use find control.
0
 
Mike-PoAuthor Commented:
Having added the validation that the row is being edited: && e.Row.RowState == DataControlRowState.Edit which is the state the dropdown list displays in. Resolved my problem.

I knew it was some fundamental logic flaw on my part. Thank you!
0

Featured Post

Important Lessons on Recovering from Petya

In their most recent webinar, Skyport Systems explores ways to isolate and protect critical databases to keep the core of your company safe from harm.

  • 3
  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now