Click here to Skip to main content
15,901,853 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi all,
i am facing issue when i update the field in Gridview.Here i am trying when user click the EDIT button in that gridview dropdown will be bind.when i am binding dropdown i am getting the error.here problem is control is found.how can i resolve the issue.
Any one can u please any solution to me.
i am using .aspx code
ASP.NET
<asp:GridView ID="Admin_Grid" runat="server" AutoGenerateColumns="False" CellPadding="4" OnRowCancelingEdit="Admin_Grid_RowCancelingEdit" OnRowEditing="Admin_Grid_RowEditing" OnRowUpdating="Admin_Grid_RowUpdating" ForeColor="#333333" GridLines="None" OnRowDataBound="Admin_Grid_RowDataBound">
           <AlternatingRowStyle BackColor="White" />
           <Columns>


               <asp:TemplateField HeaderText="FirstName">
                   <ItemTemplate>
                       <asp:Label ID="lbl_FName" runat="server" Text='<%#Eval("FirstName") %>'></asp:Label>
                   </ItemTemplate>
                   <EditItemTemplate>
                       <asp:TextBox ID="txt_FName" runat="server" Text='<%#Eval("FirstName") %>'></asp:TextBox>
                   </EditItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="LastName">
                   <ItemTemplate>
                       <asp:Label ID="lbl_LName" runat="server" Text='<%#Eval("LastName") %>'></asp:Label>
                   </ItemTemplate>
                   <EditItemTemplate>
                       <asp:TextBox ID="txt_LName" runat="server" Text='<%#Eval("LastName") %>'></asp:TextBox>
                   </EditItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="Organization">
                   <ItemTemplate>
                       <asp:Label ID="lbl_Organization" runat="server" Text='<%#Eval("Organization") %>'></asp:Label>
                   </ItemTemplate>
                   <EditItemTemplate>
                       <asp:TextBox ID="txt_Organization" runat="server" Text='<%#Eval("Organization") %>'></asp:TextBox>
                   </EditItemTemplate>
               </asp:TemplateField>

                <asp:TemplateField HeaderText="PhoneNumber">
                   <ItemTemplate>
                       <asp:Label ID="lbl_PhoneNumber" runat="server" Text='<%#Eval("PhoneNumber") %>'></asp:Label>
                   </ItemTemplate>
                   <EditItemTemplate>
                       <asp:TextBox ID="txt_PhoneNumber" runat="server" Text='<%#Eval("PhoneNumber") %>'></asp:TextBox>
                   </EditItemTemplate>
               </asp:TemplateField>
               <asp:TemplateField HeaderText="EmailAddress">
                   <ItemTemplate>
                       <asp:Label ID="lbl_EmailAddress" runat="server" Text='<%#Eval("EmailAddress") %>'></asp:Label>
                   </ItemTemplate>
                   <EditItemTemplate>
                       <asp:TextBox ID="txt_EmailAddress" runat="server" Text='<%#Eval("EmailAddress") %>'></asp:TextBox>
                   </EditItemTemplate>
               </asp:TemplateField>
                <asp:TemplateField HeaderText = "AIPServed">
                  <ItemTemplate>
                <asp:Label ID="lbl_AIPServed" runat="server" Text='<%# Eval("AIPServed")%>'></asp:Label>

               </ItemTemplate>
              <EditItemTemplate>

                  <asp:ListBox ID="ListBoxAIPServed" runat="server"></asp:ListBox>
       </EditItemTemplate>
        </asp:TemplateField>
                <asp:TemplateField HeaderText = "UserStatus">
                  <ItemTemplate>
                <asp:Label ID="lbl_Status" runat="server" Text='<%# Eval("UserStatus")%>'></asp:Label>
               </ItemTemplate>
              <EditItemTemplate>
             <asp:Label ID="lbl_Status" runat="server" Text='<%# Eval("UserStatus")%>' Visible = "false"></asp:Label>
         <asp:DropDownList ID = "ddl_Status" runat = "server">
       </asp:DropDownList>
       </EditItemTemplate>
        </asp:TemplateField>
               <asp:TemplateField HeaderText = "City">
       <ItemTemplate>
         <asp:Label ID="lblCity" runat="server" Text='<%# Eval("APCUserID")%>'></asp:Label>
       </ItemTemplate>
       <EditItemTemplate>
               <asp:Label ID="lblCity" runat="server" Text='<%# Eval("APCUserID")%>' Visible = "false"></asp:Label>
       <asp:DropDownList ID = "ddlCities" runat = "server">
       </asp:DropDownList>
       </EditItemTemplate>
       </asp:TemplateField>

                <asp:CommandField  ShowEditButton="True" />
           </Columns>
           <EditRowStyle BackColor="#2461BF" />
           <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
           <HeaderStyle BackColor="#507CD1" ForeColor="#ffffff" Font-Bold="True"/>
           <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
           <RowStyle BackColor="#EFF3FB"/>
           <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
           <SortedAscendingCellStyle BackColor="#F5F7FB" />
           <SortedAscendingHeaderStyle BackColor="#6D95E1" />
           <SortedDescendingCellStyle BackColor="#E9EBEF" />
           <SortedDescendingHeaderStyle BackColor="#4870BE" />
       </asp:GridView>



and aspx.cs code is

C#
protected void Admin_Grid_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            DropDownList ddlCities = (DropDownList)e.Row.FindControl("ddlCities");
            DataTable table = new DataTable();
            table.Columns.Add("APCUserID");
            //table.Columns.Add("FirstName");

            // APCUserID,FirstName,LastName,Organization,PhoneNumber,EmailAddress,AIPServed,UserID,Password,UserStatus


            //var lines = System.IO.File.ReadAllLines("test.txt");
            //System.IO.File.WriteAllLines("test.txt", lines.Take(lines.Length - 1).ToArray());

            using (StreamReader sr = new StreamReader(@"C:\Working\US_Crop\Sowmya\USCROP\AgencyProfitCommissionWebApplication\APSDataAccess\Resources\AIPlist.txt"))
            {
                while (!sr.EndOfStream)
                {
                    string[] parts = sr.ReadLine().Split(',');
                    table.Rows.Add(parts[0]);
                }
            }

            ddlCities.DataSource = table;
            ddlCities.DataTextField = "APCUserID";
            ddlCities.DataValueField = "APCUserID";
            ddlCities.DataBind();
            ddlCities.Items.FindByValue((e.Row.FindControl("lblCity") as Label).Text).Selected = true;
            
        }


Any can please share solution to me.

What I have tried:

i tried to DropDownList ddlCities = (DropDownList)e.Row.FindControl("ddlCities"); using this find the control but i am not able to find solution
Posted
Updated 3-Aug-16 22:07pm
Comments
F-ES Sitecore 4-Aug-16 4:52am    
This question is asked every day, please do basic research such as using the search before asking a question.

1 solution

Run your code in a debugger and see where and what your error is.

"Object reference not set to an instance of an object." means something is null when you expect it not to be.
 
Share this answer
 
Comments
Ram349 4-Aug-16 4:48am    
yes i debugging i am getting null value,it's not findout the control in grid

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900