none
LINQtoSQL ASP.NET RowUpdating object reference error RRS feed

  • Question


  • protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e) { Guid key = (Guid)(GridView1.DataKeys[e.RowIndex].Value); TextBox CompanyName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("CompanyName")); TextBox CompanyAddress = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("CompanyAddress")); TextBox CompanyCity = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("CompanyCity"));

    ermContextDataContext dc = new ermContextDataContext(); CompanyProfile profile = (from p in dc.CompanyProfiles where p.CompanyProfileID == key select p).First<CompanyProfile>(); profile.CompanyName = CompanyName.Text.ToString(); profile.CompanyAddress = CompanyAddress.Text.ToString(); profile.CompanyCity = CompanyCity.Text.ToString(); dc.SubmitChanges(); GridView1.EditIndex = -1; // fetch and rebind the data. GridView1.DataBind(); }

    Hi everyone,

     Sorry if this is all ugly.  I'm a noob.


    I am getting a "Object Reference not set to object" error.  In the debugger up to the point where the key, which is a Guid, is passed everything works.  The Guid is there and I can hover to see the corresponding object in the database, but then...nothing.  If I wasn't so bald, I'd pull all my hair out.  Any help would really be appreciated.




    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.
    Line 159:                   select p).Single<CompanyProfile>();
    Line 160:     
    Line 161:        prof.CompanyName = CompanyName.Text.ToString();
    Line 162:        prof.CompanyAddress = CompanyAddress.Text.ToString();
    Line 163:        prof.CompanyCity = CompanyCity.Text.ToString();




    The markup:

    @ Page Title="" Language="C#" MasterPageFile="~/master.Master" AutoEventWireup="true" CodeBehind="b2bnet.aspx.cs" Inherits="EnterpriseRelationshipManagement.b2b.b2bnet" %>
    <%@ Register assembly="AjaxControlToolkit" namespace="AjaxControlToolkit" tagprefix="cc1" %>
    <%@ Register assembly="System.Web.Entity, Version=3.5.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" namespace="System.Web.UI.WebControls" tagprefix="asp" %>
    <asp:Content ID="Content1" ContentPlaceHolderID="head" runat="server">
        <style type="text/css">
            .style1
            {
                width: 100%;
                height: 405px;
            }
            .style2
            {
                height: 153px;
            }
            .style3
            {
                width: 633px;
                height: 65px;
            }
            .style4
            {
                height: 153px;
                width: 633px;
            }
            .style5
            {
                height: 65px;
            }
        </style>
    </asp:Content>
    <asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" runat="server">
        <table class="style1">
            <tr>
                <td class="style3">
                    You might want to bid on these:</td>
                <td class="style5">
                    You might want to buy these:</td>
            </tr>
            <tr>
                <td class="style4">
                    <asp:ScriptManager ID="ScriptManager1" runat="server">
                    </asp:ScriptManager>
                    <cc1:TabContainer ID="TabContainer1" runat="server" ActiveTabIndex="0" 
                        Height="500px" Width="900px">
                        <cc1:TabPanel runat="server" HeaderText="My Profile" ID="TabPanelMyProfile">
                        <ContentTemplate>
    Page One 
                        
                       
                         
                            
                            
                            
                            <br></br>
                            <asp:GridView ID="GridView1" runat="server" DataKeyNames="CoProfguid" 
                                OnRowCancelingEdit="GridView1_RowCancelingEdit" 
                                OnRowEditing="GridView1_RowEditing" OnRowUpdating="GridView1_RowUpdating">
                                <Columns>
                                    <asp:CommandField ShowEditButton="True" />
                                </Columns>
                            </asp:GridView>
                            </br>
                            </br>
                            
                            
                                                    
                        
                        
    </ContentTemplate>
                        
                        
    </cc1:TabPanel>
                        
                        <cc1:TabPanel runat="server" HeaderText="My Products" ID="TabPanelMyProds"><ContentTemplate>
    Page Two 
                            
                            
                         
                            
                          
                          
                            
                            <br></br>
                            <br>
                            </br>
                            
                            
                          
                        
    </ContentTemplate>
                        
                        
    </cc1:TabPanel>
                        
                        
                        <cc1:TabPanel runat="server" HeaderText="My Bids" ID="TabPanelMyBids">
                        <ContentTemplate>
    Page Three 
                            
                            <br> 
                            
                            </br>
                            
     
                            
                        
    
                            
                        </ContentTemplate>
                        
                        
    </cc1:TabPanel>
                        
                        
                      
                        
                        
                        <cc1:TabPanel runat="server" HeaderText="I Want To Buy" ID="TabPanelIW2Buy">
                        <ContentTemplate>
    Page Four 
                            
                           
                           
                            <br> 
                            
                           
                           
                            </br>
                        
                        
    </ContentTemplate>
                        
                        
    </cc1:TabPanel>
                        
                        
                
                        
                        
                        <cc1:TabPanel runat="server" HeaderText="I Want To Hire" ID="TabPanelIW2Hire">
                        <ContentTemplate>
    Page Five <br></br>
                            <br></br>
                        
                        
    </ContentTemplate>
                        
                        
    </cc1:TabPanel>
                    
                        
                        
                        <cc1:TabPanel runat="server" HeaderText="Do Business" ID="TabPanelDoBiz">
                        <ContentTemplate>
    Page Six <br></br>
                            <br></br>
                        
                        
    </ContentTemplate>
                        
                        
    </cc1:TabPanel>
                    </cc1:TabContainer>
                    <br />
                </td>
                <td class="style2">
                    &nbsp;</td></tr></table></asp:Content>
    THe Enitire page_load

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.Security;
    
    
    namespace EnterpriseRelationshipManagement.b2b
    {
        public partial class b2bnet : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
                ermContextDataContext dc = new ermContextDataContext();
    
                MembershipUser myObject = Membership.GetUser();
                Guid UserID = (Guid)myObject.ProviderUserKey;
    
               
    
    
    
               var coprofile = from cp in dc.CompanyProfiles
                                where cp.UserIDFK == UserID
                                select new
                                {
                                     CompanyName = cp.CompanyName,
                                     CompanyAddress = cp.CompanyAddress,
                                     CompanyCity = cp.CompanyCity,
                                     CompanyState = cp.CompanyState,
                                     CompanyZip = cp.CompanyZip,
                                     CompanyPhone = cp.CompanyPhone,
                                     CompanyTagLine = cp.CompanyTagLine,
                                     CoProfguid = cp.CompanyProfileID
                                };
    
    
               
    
               GridView1.DataSource = coprofile;
               GridView1.DataBind();
               
    
    
                
    
               
                
                
             
        
    
    
            }
    
            protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
            {
    
    
                GridView1.EditIndex = e.NewEditIndex;     // fetch and rebind the data.        
                GridView1.DataBind();     
    
            }
    
            protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
            {
                GridView1.EditIndex = -1;
    
                // fetch and rebind the data.
                GridView1.DataBind();
    
            }
    
            protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
    
        {        
            Guid key = (Guid)(GridView1.DataKeys[e.RowIndex].Value);
    
           
            
            TextBox CompanyName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("CompanyName"));
            TextBox CompanyAddress = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("CompanyAddress"));
            TextBox CompanyCity = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("CompanyCity"));
    
            //TextBox City = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[1]);
    
            ermContextDataContext dc = new ermContextDataContext();
    
            CompanyProfile profile = (from p in dc.CompanyProfiles
                       where p.CompanyProfileID == key
                       select p).First<CompanyProfile>();
          
            profile.CompanyName = CompanyName.Text.ToString();
            profile.CompanyAddress = CompanyAddress.Text.ToString();
            profile.CompanyCity = CompanyCity.Text.ToString();
    
           
                
                
            
            dc.SubmitChanges();
    
    
    
    
            GridView1.EditIndex = -1;
    
    
    // fetch and rebind the data.
            GridView1.DataBind();
        }
    
    
            
            
    
            
    
            
        }
    }
    
    Friday, May 15, 2009 6:17 PM

Answers

  • I don't see any TextBoxes with an ID of "CompanyName" in your .aspx page, so I'm guessing that is why FindControl is failing to return a control. You might try finding the control by index instead. Once you find it, check if it has an ID, and if so, you can go back to using FindControl. Otherwise, you can use the TextBox that you got by looking through the Controls property of the Cell. (TextBox City = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[1]);)

    If you have a further questions I'd direct them to the ASP.NET forums here, since they will probably have some other ideas.

    Sunday, May 17, 2009 5:16 PM
    Answerer

All replies

  • Ok, turns out I've screwed up the TextBoxes.  They are what is returning null.  The LINQ object is populated correctly, but I'm passing nulls.  It's null at :

    TextBox CompanyName = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("CompanyName"));


    Should I just try to declare Strings here and get the text?
    Friday, May 15, 2009 7:30 PM
  • I don't see any TextBoxes with an ID of "CompanyName" in your .aspx page, so I'm guessing that is why FindControl is failing to return a control. You might try finding the control by index instead. Once you find it, check if it has an ID, and if so, you can go back to using FindControl. Otherwise, you can use the TextBox that you got by looking through the Controls property of the Cell. (TextBox City = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[1]);)

    If you have a further questions I'd direct them to the ASP.NET forums here, since they will probably have some other ideas.

    Sunday, May 17, 2009 5:16 PM
    Answerer
  • Thank you.  You're suggestion works.  Now my struggle is to determine the index.  Controls[0] is the first, or only row correct?  If there is a set of command controls, are there some hidden controls?  I seem to be off one in my index, assuming Select, Edit, Update are 0,1,2 respectively. 

    The textboxes seem self explanatory, but is there a way to find out what the command fields are all about?

    Thanks again.
    Sunday, May 17, 2009 6:11 PM
  • I tried setting up a GridView similar to what you have above and found that I was able to find the TextBox as the first control in the relevant cell. i.e.

    var row = GridView1.Rows[e.RowIndex];
    var textBox = (TextBox)row.Cells[2].Controls[0];

    Monday, May 18, 2009 12:15 AM
    Answerer