I receive the error message "Cannot perform runtime binding on a null reference" on my Profile page, where users store their contact information and a profile image. I used to direct the user to a page that would prompt them to add their basic profile
information right after they registered on the site but I am changing the flow.
I believe am getting the error as a result of the page looking for data that is not there. I have tried removing a few elements but I continue to get the error. Can anyone make a recommendation on how I can alter the code? The Profile Page code and and example
of the error I am receiving is below...
Thank you!
@if (!WebSecurity.IsAuthenticated)
{
Response.Redirect("~/Account/Login");
}
else
{
Layout = "~/_SiteLayout_Member.cshtml";
Page.Title = "Profile";
}
@{
<script type="text/javascript">
var _gaq = _gaq || [];
_gaq.push(['_setAccount', 'UA-37389467-1']);
_gaq.push(['_setDomainName', 'mumba.me']);
_gaq.push(['_setAllowLinker', true]);
_gaq.push(['_trackPageview']);
(function () {
var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true;
ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js';
var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s);
})();
</script>
}
@{
int UserId = WebSecurity.CurrentUserId;
var db = Database.Open("MumbaDB1");
var sql = "SELECT * FROM ProfileInfo WHERE UserId = @0";
var info = db.QuerySingle(sql, WebSecurity.CurrentUserId);
var imageprofile = "SELECT * FROM Files WHERE UserId = @0";
var image = db.QuerySingle(imageprofile, WebSecurity.CurrentUserId);
var FirstName = "";
var LastName = "";
var CompanyName = "";
var Address1 = "";
var Address2 = "";
var City = "";
var State = "";
var Zip = "";
var OfficePhone = "";
var MobilePhone = "";
var ProfileId = "";
if (IsPost)
{
FirstName = Request.Form["FirstName"];
LastName = Request.Form["LastName"];
CompanyName = Request.Form["CompanyName"];
Address1 = Request.Form["Address1"];
Address2 = Request.Form["Address2"];
City = Request.Form["City"];
State = Request.Form["State"];
Zip = Request.Form["Zip"];
OfficePhone = Request.Form["OfficePhone"];
MobilePhone = Request.Form["MobilePhone"];
ProfileId = Request.Form["ProfileId"];
var updateCommand = "UPDATE ProfileInfo SET FirstName=@0, LastName=@1, CompanyName=@2, Address1=@3, Address2=@4, City=@5, State=@6, Zip=@7, OfficePhone=@8, MobilePhone=@9 WHERE ProfileId=@10";
db.Execute(updateCommand, FirstName, LastName, CompanyName, Address1, Address2, City, State, Zip, OfficePhone, MobilePhone, ProfileId);
Response.Redirect("Profile");
}
}
<!DOCTYPE html>
<html>
<head>
<title>Profile</title>
<meta charset="utf-8" />
</head>
<body>
<form method="post" action="DeleteImage">
<div id="ProfileImage">
@if (image == null)
{
<img src="~/images/ProfileImages/ProfileImage.jpg" alt="" />
<p><a href="~/AddProfileImage"><span style="font-size: .75em;">Add Profile Image</span></a></p>
}
else
{
<img src="~/images/ProfileImages/@image.Filename" alt="" />
<input type="submit" value="Delete" style="margin-left: 20px; margin-top: 15px;" />
}
</div>
</form>
<div id="ProfileMes">
<p>Your profile picture, name, company, office phone and email will be used on the publication</p>
</div>
<form method="post" action="">
<fieldset id="ProfileInfo">
<legend>Profile Information</legend>
<p><label for="FirstName">First Name:</label>
<input type="text" name="FirstName" value="@info.FirstName" /></p>
<p><label for="LastName">Last Name:</label>
<input type="text" name="LastName" value="@info.LastName" /></p>
<p><label for="CompanyName">Company Name:</label>
<input type="text" name="CompanyName" value="@info.CompanyName" /></p>
<p><label for="Address1">Address:</label>
<input type="text" name="Address1" value="@info.Address1" /></p>
<p><label for="Address2">Unit:</label>
<input type="text" name="Address2" value="@info.Address2" /></p>
<p><label for="City">City:</label>
<input type="text" name="City" value="@info.City" /></p>
<p><label for="State">State:</label>
<input type="text" name="State" value="@info.State" /></p>
<p><label for="Zip">Zip:</label>
<input type="text" name="Zip" value="@info.Zip" /></p>
<p><label for="OfficePhone">Office Phone: <span style="font-style: italic; font-size: 10px;">(000-000-0000)</span></label></label>
<input type="text" name="OfficePhone" value="@info.OfficePhone" /></p>
<p><label for="MobilePhone">Mobile Phone: <span style="font-style: italic; font-size: 10px;">(000-000-0000)</span></label></label>
<input type="text" name="MobilePhone" value="@info.MobilePhone" /></p>
<input type="hidden" name="ProfileId" value="@info.ProfileId" />
<p><input type="submit" name="buttonSubmit" value="Save Changes" /></p>
</fieldset>
<div id="ProfileBenefits">
<p>Market Research: <br />social media users with a profile picture experience ten times more activity online than users without a profile picture</p>
</div>
</form>
</body>
</html>
The type of error I am receiving is as follows:
Server Error in '/' Application.
--------------------------------------------------------------------------------
Cannot perform runtime binding on a null reference
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: Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: Cannot perform runtime binding on a null reference
Source Error:
Line 87: <img src="~/images/ProfileImages/@image.Filename" alt="" />
Line 88: <input type="submit" value="Delete" style="margin-left: 20px; margin-top: 15px;" />
Line 89: }
Line 90: </div>
Line 91: </form>
Source File: d:\hosting\8960948\html\Profile.cshtml Line: 89
Stack Trace:
[RuntimeBinderException: Cannot perform runtime binding on a null reference]
CallSite.Target(Closure , CallSite , Object ) +124
System.Dynamic.UpdateDelegates.UpdateAndExecute1(CallSite site, T0 arg0) +4196736
CallSite.Target(Closure , CallSite , Object ) +222
ASP._Page_Profile_cshtml.Execute() in d:\hosting\8960948\html\Profile.cshtml:89
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +209
System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +68
System.Web.WebPages.WebPage.ExecutePageHierarchy() +123
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +78
System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContextBase httpContext) +121
Thank you for the response. Where would I place that code and would I write it the same way? I apologize for asking, I am still learning. Thanks again.
Thanks. I updated the code and I am still receiving the same error message. Is it possible that is it looking for data from the ProfileInfo table and the error is caused if the table is empty? I set-up a test user with information populated in that table
(ProfileInfo, containing basic contact info) and I do not receive the error.
Whenever you use this type of syntax you must guarantee that the object, info, is not null. Trying to access a property of a null object will result in a Null Reference Exception. You can either create a "default" object to use if the database call does
not return anything or use ternery operator in the view like:
Thank you! I am going to try first to update the code using the example provided and see if that works. I will let you know how it turns out. Thanks again.
1. The line you are representing, Line 89 contains only "}". According to me error is in any other line.
2. The method you are using, can be assumed that you are trying to delete some image.
Here, these are the steps.
First, you need to check that whether the image name the user is providing contains some value, or not?
If it is null than tell him, that some error is here like
if(image == null) {
<p>Sorry no image found in database, with this name.</p>
} else {
<p>Here is the image you requested to be deleted</p>
// Show the image here..
// Also the form to submit the image name, and delete!
}
This way the server will look for the image. If image is present, the result wont show any error. Other wise it will say, there is no image. Next time you will check the database, not the code to see error!
Please "Marks As Answer" if any answer helped you out!
~~! FIREWALL !~~
Hi, thanks for your feedback. I think the logic for the picture isn't the root cause of the error. If I populate the ProfileInfo table with contact information, I do not get an error and the generic image is used. If the ProfileInfo table is empty, I get the
error. I think I need to alter the code to allow the page to load, even if that table is empty. I appreciate any additional thoughts. Thanks!
kookoomoo
Member
54 Points
92 Posts
Cannot perform runtime binding on a null reference
Feb 26, 2013 08:32 PM|LINK
Hello,
I receive the error message "Cannot perform runtime binding on a null reference" on my Profile page, where users store their contact information and a profile image. I used to direct the user to a page that would prompt them to add their basic profile information right after they registered on the site but I am changing the flow.
I believe am getting the error as a result of the page looking for data that is not there. I have tried removing a few elements but I continue to get the error. Can anyone make a recommendation on how I can alter the code? The Profile Page code and and example of the error I am receiving is below...
Thank you!
@if (!WebSecurity.IsAuthenticated) { Response.Redirect("~/Account/Login"); } else { Layout = "~/_SiteLayout_Member.cshtml"; Page.Title = "Profile"; } @{ <script type="text/javascript"> var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-37389467-1']); _gaq.push(['_setDomainName', 'mumba.me']); _gaq.push(['_setAllowLinker', true]); _gaq.push(['_trackPageview']); (function () { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })(); </script> } @{ int UserId = WebSecurity.CurrentUserId; var db = Database.Open("MumbaDB1"); var sql = "SELECT * FROM ProfileInfo WHERE UserId = @0"; var info = db.QuerySingle(sql, WebSecurity.CurrentUserId); var imageprofile = "SELECT * FROM Files WHERE UserId = @0"; var image = db.QuerySingle(imageprofile, WebSecurity.CurrentUserId); var FirstName = ""; var LastName = ""; var CompanyName = ""; var Address1 = ""; var Address2 = ""; var City = ""; var State = ""; var Zip = ""; var OfficePhone = ""; var MobilePhone = ""; var ProfileId = ""; if (IsPost) { FirstName = Request.Form["FirstName"]; LastName = Request.Form["LastName"]; CompanyName = Request.Form["CompanyName"]; Address1 = Request.Form["Address1"]; Address2 = Request.Form["Address2"]; City = Request.Form["City"]; State = Request.Form["State"]; Zip = Request.Form["Zip"]; OfficePhone = Request.Form["OfficePhone"]; MobilePhone = Request.Form["MobilePhone"]; ProfileId = Request.Form["ProfileId"]; var updateCommand = "UPDATE ProfileInfo SET FirstName=@0, LastName=@1, CompanyName=@2, Address1=@3, Address2=@4, City=@5, State=@6, Zip=@7, OfficePhone=@8, MobilePhone=@9 WHERE ProfileId=@10"; db.Execute(updateCommand, FirstName, LastName, CompanyName, Address1, Address2, City, State, Zip, OfficePhone, MobilePhone, ProfileId); Response.Redirect("Profile"); } } <!DOCTYPE html> <html> <head> <title>Profile</title> <meta charset="utf-8" /> </head> <body> <form method="post" action="DeleteImage"> <div id="ProfileImage"> @if (image == null) { <img src="~/images/ProfileImages/ProfileImage.jpg" alt="" /> <p><a href="~/AddProfileImage"><span style="font-size: .75em;">Add Profile Image</span></a></p> } else { <img src="~/images/ProfileImages/@image.Filename" alt="" /> <input type="submit" value="Delete" style="margin-left: 20px; margin-top: 15px;" /> } </div> </form> <div id="ProfileMes"> <p>Your profile picture, name, company, office phone and email will be used on the publication</p> </div> <form method="post" action=""> <fieldset id="ProfileInfo"> <legend>Profile Information</legend> <p><label for="FirstName">First Name:</label> <input type="text" name="FirstName" value="@info.FirstName" /></p> <p><label for="LastName">Last Name:</label> <input type="text" name="LastName" value="@info.LastName" /></p> <p><label for="CompanyName">Company Name:</label> <input type="text" name="CompanyName" value="@info.CompanyName" /></p> <p><label for="Address1">Address:</label> <input type="text" name="Address1" value="@info.Address1" /></p> <p><label for="Address2">Unit:</label> <input type="text" name="Address2" value="@info.Address2" /></p> <p><label for="City">City:</label> <input type="text" name="City" value="@info.City" /></p> <p><label for="State">State:</label> <input type="text" name="State" value="@info.State" /></p> <p><label for="Zip">Zip:</label> <input type="text" name="Zip" value="@info.Zip" /></p> <p><label for="OfficePhone">Office Phone: <span style="font-style: italic; font-size: 10px;">(000-000-0000)</span></label></label> <input type="text" name="OfficePhone" value="@info.OfficePhone" /></p> <p><label for="MobilePhone">Mobile Phone: <span style="font-style: italic; font-size: 10px;">(000-000-0000)</span></label></label> <input type="text" name="MobilePhone" value="@info.MobilePhone" /></p> <input type="hidden" name="ProfileId" value="@info.ProfileId" /> <p><input type="submit" name="buttonSubmit" value="Save Changes" /></p> </fieldset> <div id="ProfileBenefits"> <p>Market Research: <br />social media users with a profile picture experience ten times more activity online than users without a profile picture</p> </div> </form> </body> </html>The type of error I am receiving is as follows:
RichardY
Star
8376 Points
1573 Posts
Re: Cannot perform runtime binding on a null reference
Feb 26, 2013 08:59 PM|LINK
You test for image == null but you should also check that image.Filename is not null or whitespace.
kookoomoo
Member
54 Points
92 Posts
Re: Cannot perform runtime binding on a null reference
Feb 27, 2013 01:18 AM|LINK
RichardY
Star
8376 Points
1573 Posts
Re: Cannot perform runtime binding on a null reference
Feb 27, 2013 12:00 PM|LINK
Replace the above line with:
kookoomoo
Member
54 Points
92 Posts
Re: Cannot perform runtime binding on a null reference
Feb 27, 2013 01:01 PM|LINK
Thanks. I updated the code and I am still receiving the same error message. Is it possible that is it looking for data from the ProfileInfo table and the error is caused if the table is empty? I set-up a test user with information populated in that table (ProfileInfo, containing basic contact info) and I do not receive the error.
I appreciate the continued help!
RichardY
Star
8376 Points
1573 Posts
Re: Cannot perform runtime binding on a null reference
Feb 27, 2013 03:56 PM|LINK
Whenever you use this type of syntax you must guarantee that the object, info, is not null. Trying to access a property of a null object will result in a Null Reference Exception. You can either create a "default" object to use if the database call does not return anything or use ternery operator in the view like:
value="@(info == null ? String.Empty : info.Firstname)"
I am sure there are other ways as well.
kookoomoo
Member
54 Points
92 Posts
Re: Cannot perform runtime binding on a null reference
Feb 27, 2013 04:30 PM|LINK
Afzaal.Ahmad...
Contributor
2660 Points
1039 Posts
Re: Cannot perform runtime binding on a null reference
Feb 27, 2013 04:37 PM|LINK
1. The line you are representing, Line 89 contains only "}". According to me error is in any other line.
2. The method you are using, can be assumed that you are trying to delete some image.
Here, these are the steps.
First, you need to check that whether the image name the user is providing contains some value, or not?
If it is null than tell him, that some error is here like
if(image == null) { <p>Sorry no image found in database, with this name.</p> } else { <p>Here is the image you requested to be deleted</p> // Show the image here.. // Also the form to submit the image name, and delete! }This way the server will look for the image. If image is present, the result wont show any error. Other wise it will say, there is no image. Next time you will check the database, not the code to see error!
~~! FIREWALL !~~
rajendraram
Participant
812 Points
258 Posts
Re: Cannot perform runtime binding on a null reference
Feb 27, 2013 04:52 PM|LINK
Answered in this same question
http://forums.asp.net/t/1869019.aspx/1
kookoomoo
Member
54 Points
92 Posts
Re: Cannot perform runtime binding on a null reference
Feb 27, 2013 04:56 PM|LINK