Solved

system null reference error when loading an image in formview

Posted on 2011-02-14
19
393 Views
Last Modified: 2012-05-11
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
0
Comment
Question by:Claudiu10
  • 10
  • 5
  • 2
  • +1
19 Comments
 
LVL 22

Expert Comment

by:plusone3055
ID: 34891858
one user has that folder the other doesn't
0
 
LVL 22

Expert Comment

by:plusone3055
ID: 34891871
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34891946
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
 

Author Comment

by:Claudiu10
ID: 34891958
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
 

Author Comment

by:Claudiu10
ID: 34891977
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34892031
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
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34892068
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
 

Author Comment

by:Claudiu10
ID: 34892070
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
 

Author Comment

by:Claudiu10
ID: 34892073
it's only in one formview.
0
Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:Claudiu10
ID: 34892131
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
 

Author Comment

by:Claudiu10
ID: 34892225
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
 

Author Comment

by:Claudiu10
ID: 34892235
Actually I can't put the code not sure what all tags must have a closing tag means.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34892300
According to the code you've posted, ProfilePicture is not a control used inside the FormView--it's just above it.
0
 
LVL 74

Expert Comment

by:käµfm³d 👽
ID: 34892313
>>  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
 

Author Comment

by:Claudiu10
ID: 34892317
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
 

Author Comment

by:Claudiu10
ID: 34892418
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
 

Accepted Solution

by:
Claudiu10 earned 0 total points
ID: 34892580
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
 
LVL 19

Expert Comment

by:Amandeep Singh Bhullar
ID: 35906833
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

Introduction Hi all and welcome to my first article on Experts Exchange. A while ago, someone asked me if i could do some tutorials on object oriented programming. I decided to do them on C#. Now you may ask me, why's that? Well, one of the re…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
This video discusses moving either the default database or any database to a new volume.
Illustrator's Shape Builder tool will let you combine shapes visually and interactively. This video shows the Mac version, but the tool works the same way in Windows. To follow along with this video, you can draw your own shapes or download the file…

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now