system null reference error when loading an image in formview

Hi I have the following code in the code behind and can't load an image inside a formview.  For some users I can for certain users it gives me a system null reference exception.  Object not set to an instance as follows:

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

Source Error:


Line 22:
Line 23:                     Image img = (Image)FormView1.FindControl("ProfilePicture");
Line 24:                     img.ImageUrl = "~/UserPages/UserPhotos/default.jpg";

 
 protected void Page_Load(object sender, EventArgs e)
    {
       
        if (!Page.IsPostBack)
        {
           
            FormView1.ChangeMode(FormViewMode.ReadOnly);
            FormView1.DataSource = MusicDbBusinessTear.GetUserDetails();
            FormView1.DataBind();
            string imageUrl = MusicDbBusinessTear.GetProfilePicture();
            if (imageUrl == "")
            {

                    Image img = (Image)FormView1.FindControl("ProfilePicture");
                    img.ImageUrl = "~/UserPages/UserPhotos/default.jpg";

            }
            else
            {
             
                Image img = (Image)FormView1.FindControl("ProfilePicture");
                img.ImageUrl = "~/UserPages/UserPhotos/" + imageUrl;
            }
        }  
       
    }

I go through the step by step debugging and everything works fine until I get to the img.ImageUrl = "~/UserPages/......"  statements weather there is a picture or not.
I have two users created and for one it works fine and another it doesn't work.
It seems like it can't find the image control in formview for some users.

Any help is appreciated.
Thank you
Claudiu10Asked:
Who is Participating?

[Webinar] Streamline your web hosting managementRegister Today

x
 
Claudiu10Connect With a Mentor Author Commented:
If anyone runs into a similar problem I figured it out.  It's because the details registration pages were created after the users instead of at sign up.  Some infor was missing in the database due to it.  3 tables had to be updated completely which I guess they weren't.  That's why formview was not displaying.

Thank you
0
 
plusone3055Commented:
one user has that folder the other doesn't
0
 
plusone3055Commented:
create for a 3rd user to confrim.. if the 3rd user works fine then its the user.. Doesn't look like its your code to me
0
Never miss a deadline with monday.com

The revolutionary project management tool is here!   Plan visually with a single glance and make sure your projects get done.

 
käµfm³d 👽Commented:
The only reason you would get that error message for "img.ImageUrl = ..." is if "img" was null, and given the code you posted, that can't happen since you would get an exception when you try to cast to Image in the line above it. Are you sure that's the line causing the error?
0
 
Claudiu10Author Commented:
3rd user same thing.  I have two roles which have access to these folders.  Users and Administrators.  Users in Users role don't seem to get this exception.  User in Administrator and User role works fine.  Checked my permissions and all Roles users and administrator have access to these folders.
0
 
Claudiu10Author Commented:
Yes went through step by step debugging and it is null when it gets to the img.ImageUrl line.  And when it doesn't work it's the img that is null.  When it works it's not null.
0
 
käµfm³d 👽Commented:
Hmmm....   seems I was mistaken. Apparently you can cast null in ASP.NET and C#....  weird.

Anyways, the problem is that ProfilePicture is not being found for some reason. Are you certain you didn't accidentally rename it on some page or somewhere else in code perhaps?
0
 
käµfm³d 👽Commented:
Perhaps also your Image control is being nested inside of some other control for certain users. FindControl, as I understand it, is not recursive, so you would have to search the bottom-most container.
0
 
Claudiu10Author Commented:
Yes I've trippled checked.  I have the same name in edit item template and item template of formview.  Would that matter?  It's weird that it only works fine for one user.
0
 
Claudiu10Author Commented:
it's only in one formview.
0
 
Claudiu10Author Commented:
It's something to do with the formview.  I took the image out of the formview and it works.  However the user details in formview not working for all users except for one.
The formview is from a web control which is loaded into a place holder depending on the step.

Code for formview:

<%@ Control Language="C#" AutoEventWireup="true" CodeFile="UserDetailsEdit.ascx.cs" Inherits="UserDetailsEdit" %>
<link href="../Styles/Site.css" rel="stylesheet" type="text/css" />

<asp:Image ID="ProfilePicture" runat="server" Height="150px" />
<asp:FormView ID="FormView1" runat="server"
    onitemcommand="FormView1_ItemCommand"
    onmodechanging="FormView1_ModeChanging" DefaultMode="Edit">
    <EditItemTemplate>
        <table cellpadding="2" cellspacing="2">
                        <tr>
                            <td>
                               
                            </td>
                            <td valign="top">
                                First Name:&nbsp;<asp:TextBox ID="FirstNameTextBox" runat="server" Text='<%# Eval("FirstName") %>'></asp:TextBox><br />
                                Last Name:&nbsp;<asp:TextBox ID="LastNameTextBox" runat="server" Text='<%# Eval("LastName") %>'></asp:TextBox>
                            </td>
                        </tr>
          </table>
        <table cellpadding="2" cellspacing="2">

            <tr>

                <td>
                    Marital Status:    
                </td>
                <td>
                    <asp:DropDownList ID="MaritalDropDownList" runat="server" DataTextField='<%# Eval("MaritalStatus") %>' DataValueField="0" >
                                           
                                            <asp:ListItem Value="Single">Single</asp:ListItem>
                                            <asp:ListItem Value="Dating">Dating</asp:ListItem>
                                            <asp:ListItem Value="Married">Married</asp:ListItem>
                                            <asp:ListItem Value="Widowed">Widowed</asp:ListItem>
                                        </asp:DropDownList>
                </td>
            </tr>
            <tr>
                <td>
                    Address1:
                </td>
                <td>
                    <asp:TextBox ID="Address1TextBox" runat="server" Text='<%# Eval("Address1") %>' />  
                </td>
            </tr>
            <tr>
                <td>
                    Adress2:
                </td>
                <td>
                    <asp:TextBox ID="Address2TextBox" runat="server" Text='<%# Eval("Address2") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    City:
                </td>
                <td>
                    <asp:TextBox ID="CityTextBox" runat="server" Text='<%# Eval("City") %>' />      
                </td>
            </tr>
            <tr>
                <td>
                    State:
                </td>
                <td>
                    <asp:TextBox ID="StateTextBox" runat="server" Text='<%# Eval("State") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Zip:
                </td>
                <td>
                    <asp:TextBox ID="ZipTextBox" runat="server" Text='<%# Eval("Zip") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Country:
                </td>
                <td>
                    <asp:TextBox ID="CountryTextBox" runat="server" Text='<%# Eval("Country") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Home Phone:
                </td>
                <td>
                    <asp:TextBox ID="HomePhoneTextBox" runat="server" Text='<%# Eval("HomePhone") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Mobile Phone:
                </td>
                <td>
                   <asp:TextBox ID="MobilePhoneTextBox" runat="server" Text='<%# Eval("MobilePhone") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Email:
                </td>
                <td>
                     <asp:TextBox ID="EmailTextBox" runat="server" Text='<%# Eval("Email") %>' />
                </td>
            </tr>
            <tr>
                <td>
                     Share picture only with friends:
                </td>
                <td>
                    <asp:CheckBox ID="PicsShowCheckBox" runat="server"  Checked ='<%# Eval("SharePics") %>' />
                </td>
            </tr>
            <tr>
                <td>
                     Share wall news feeds only with friends:
                </td>
                <td>
                     <asp:CheckBox ID="WallShowCheckBox" runat="server" Checked ='<%# Eval("ShareWall") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Hide address from friends:  
                </td>
                <td>
                      <asp:CheckBox ID="AddressShowCheckBox" runat="server" Checked='<%# Eval("ShareAddress") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Hide email from friends:  
                </td>
                <td>
                     <asp:CheckBox ID="EmailShowCheckBox" runat="server" Checked='<%# Eval("ShareEmail") %>' />
                </td>
            </tr>
            <tr>
                <td>
                   Hide phone numbers from friends:
                </td>
                <td>
                    <asp:CheckBox ID="PhoneShowCheckBox" runat="server" Checked='<%# Eval("SharePhone") %>' />
                </td>
            </tr>
 
        </table>

            <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True"
                CommandName="MyUpdate" Text="Update" />
            &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server"
            CausesValidation="False" CommandName="Cancel" Text="Cancel" />
    </EditItemTemplate>
   
    <ItemTemplate>
        <table cellpadding="2" cellspacing="2">
                        <tr>
                            <td>
                               
                            </td>
                            <td valign="top">
                                First Name:&nbsp;<asp:Label ID="FirstNameLabel" runat="server" Text='<%# Eval("FirstName") %>'></asp:Label><br />
                                Last Name:&nbsp;<asp:Label ID="LastNameLabel" runat="server" Text='<%# Eval("LastName") %>'></asp:Label>
                            </td>
                        </tr>
                    </table>
        <table cellpadding="2" cellspacing="2">
            <tr>
                <td>
                    Add photo to profile:
                </td>
            </tr>
           <tr>
                <td>
                    <asp:Label ID="StatusLabel" runat="server" Text="" Visible="false"></asp:Label><br />
                    <asp:FileUpload ID="ProfilePhotoFileUpload" runat="server" />&nbsp;&nbsp;
                    <asp:Button ID="AddButton" runat="server" Text="Add" onclick="addButton_Click" />
                </td>
           </tr>
         
        </table>
        <table cellpadding="2" cellspacing="2">

            <tr>
                <td>
                    Marital Status:    
                </td>
                <td>
                    <asp:Label ID="MaritalStatusLabel" runat="server" Text='<%# Eval("MaritalStatus") %>' />  
                     
                </td>
            </tr>
            <tr>
                <td>
                    Address1:
                </td>
                <td>
                    <asp:Label ID="Address1Label" runat="server" Text='<%# Eval("Address1") %>' />    
                </td>
            </tr>
            <tr>
                <td>
                    Adress2:
                </td>
                <td>
                    <asp:Label ID="Address2Label" runat="server" Text='<%# Eval("Address2") %>' />
               
                </td>
            </tr>
            <tr>
                <td>
                    City:
                </td>
                <td>
                    <asp:Label ID="CityLabel" runat="server" Text='<%# Eval("City") %>' />  
                </td>
            </tr>
            <tr>
                <td>
                    State:
                </td>
                <td>
                    <asp:Label ID="StateLabel" runat="server" Text='<%# Eval("State") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Zip:
                </td>
                <td>
                    <asp:Label ID="ZipLabel" runat="server" Text='<%# Eval("Zip") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Country:
                </td>
                <td>
                    <asp:Label ID="CountryLabel" runat="server" Text='<%# Eval("Country") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Home Phone:
                </td>
                <td>
                    <asp:Label ID="HomePhoneLabel" runat="server" Text='<%# Eval("HomePhone") %>' />
                </td>
            </tr>

            <tr>
                <td>
                    Mobile Phone:
                </td>
                <td>
                   <asp:Label ID="MobilePhoneLabel" runat="server" Text='<%# Eval("MobilePhone") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Email:
                </td>
                <td>
                    <asp:Label ID="EmailLabel" runat="server" Text='<%# Eval("Email") %>' />
                </td>
            </tr>
            <tr>
                <td>
                     Share picture only with friends:
                </td>
                <td>
                    <asp:CheckBox ID="PicsShowCheckBox" runat="server" Enabled="false"  Checked ='<%# Eval("SharePics") %>' />
                </td>
            </tr>
            <tr>
                <td>
                     Share wall news feeds only with friends:
                </td>
                <td>
                     <asp:CheckBox ID="WallShowCheckBox" runat="server" Enabled="false" Checked ='<%# Eval("ShareWall") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Hide address from friends:  
                </td>
                <td>
                      <asp:CheckBox ID="AddressShowCheckBox" runat="server" Enabled="false" Checked='<%# Eval("ShareAddress") %>' />
                </td>
            </tr>
            <tr>
                <td>
                    Hide email from friends:  
                </td>
                <td>
                     <asp:CheckBox ID="EmailShowCheckBox" runat="server" Enabled="false" Checked='<%# Eval("ShareEmail") %>' />
                </td>
            </tr>
            <tr>
                <td>
                   Hide phone numbers from friends:
                </td>
                <td>
                    <asp:CheckBox ID="PhoneShowCheckBox" runat="server" Enabled="false" Checked='<%# Eval("SharePhone") %>' />
                </td>
            </tr>
        </table>
       
            <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False"
            CommandName="Edit" Text="Edit" />
       
    </ItemTemplate>
</asp:FormView>

Now that I took the image out it displays and it doesn't give me any errors but nothing in the formview is displaying.  Only displays for the first user created.
0
 
Claudiu10Author Commented:
Here is the code behind also if it helps.  So everything works for the first user created but nothing in the formview displays for additional users.  Looks like it can't find the controls for any other users.

0
 
Claudiu10Author Commented:
Actually I can't put the code not sure what all tags must have a closing tag means.
0
 
käµfm³d 👽Commented:
According to the code you've posted, ProfilePicture is not a control used inside the FormView--it's just above it.
0
 
käµfm³d 👽Commented:
>>  what all tags must have a closing tag means

That means you have something like this:

[ code ]  ...  some code ...

and you need something like this:

[ code ]  ...  some code ... [ /code ]


Note, there are no spaces between the brackets and "code"; I added them to keep the site from rendering the block  = )
0
 
Claudiu10Author Commented:
Yeah I just moved it outside it and now it works fine.  It's something to do with the formview because now picture works fine but nothing in formview is actually displaying.  Stuff in the formview only displays for one user out of 4.
0
 
Claudiu10Author Commented:
Thanks for all your help.  It started working suddenly.  I have no idea what I did. hehe.  So can't post a solution because I don't know why.
0
 
Amandeep Singh BhullarCommented:
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0
All Courses

From novice to tech pro — start learning today.