it is also not working. Object reference not set to an instance of an object. error message is displayed. when I debugged the code and copied the details then it gives the following message.
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=App_Code.wp83lrto
StackTrace:
at Upload.ProcessRequest(HttpContext context) in e:\Projects\Web Site Projects\Project\App_Code\Upload.cs:line 127
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
InnerException:
Is HttpContext.Current.User.Identity.Name actually returning what you are expecting? Can you give much more detail on how you are doing this (for example, are you using AspNetMembershipProvider?). Maybe you can go through this walkthrough and let us know
if you have issues:
in aspx page I am having a control to upload files, which in turn transfers the control to the Upload.cs class in App_code folder, where code is as follows (I am logged in at the time of uploading files)
public class Upload : IHttpHandler, IRequiresSessionState
{
public Upload()
{
}
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
using (ProjectEntities myEntities = new ProjectEntities())
{
MyFile myFile;
string virtualFolder = "~/Images/";
//string uploadPath = context.Server.MapPath(context.Request.ApplicationPath + virtualFolder);
string uploadPath = context.Server.MapPath(virtualFolder);
// loop through all the uploaded files
for(int j = 0; j < context.Request.Files.Count; j++)
{
// get the current file
HttpPostedFile uploadFile = context.Request.Files[j];
// if there was a file uploded
if (uploadFile.ContentLength > 0)
{
Can you verify that you are getting this value? Just have a page that does Response.Write(HttpContext.Current.User.Identity.Name);
My best guess is that you don't have a value there - you are maybe using Anonymous authentication? You could also just add a good username in the web.config to see if it gets past the error:
sophia_asp
Member
521 Points
469 Posts
store logged in user id in database from class file
Feb 24, 2012 04:51 PM|LINK
Hello all,
I am storing the user id for a record who creates it. It is working fine in aspx.cs file.
myReview.Owner = (Guid)Membership.GetUser().ProviderUserKey;
but when I use the same code in a class file in App_Code folder then it does not work and code breaks.
Is there any other way to get user id in case of class file?
Please help me I am stuck on it for a long time.
Many thanks...
Shellymn
Contributor
2602 Points
485 Posts
Re: store logged in user id in database from class file
Feb 24, 2012 05:39 PM|LINK
Try this.
use Context.user instead of Membership.GetUser() .
sophia_asp
Member
521 Points
469 Posts
Re: store logged in user id in database from class file
Feb 24, 2012 06:51 PM|LINK
HI Shelly,
I did not get any reference to Context.User
However I used HttpContext.Current.User.Identity.Name).ProviderUserKey;
but it is also not working...
hans_v
All-Star
35986 Points
6550 Posts
Re: store logged in user id in database from class file
Feb 25, 2012 09:11 AM|LINK
Membership.GetUser() is returning the surrently logged in User.
HttpContext.Current.User.Identity.Name is returning the Username of the currently logged in user
So try Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey
sophia_asp
Member
521 Points
469 Posts
Re: store logged in user id in database from class file
Feb 25, 2012 12:34 PM|LINK
Hi Hans,
it is also not working. Object reference not set to an instance of an object. error message is displayed. when I debugged the code and copied the details then it gives the following message.
System.NullReferenceException was unhandled by user code
Message=Object reference not set to an instance of an object.
Source=App_Code.wp83lrto
StackTrace:
at Upload.ProcessRequest(HttpContext context) in e:\Projects\Web Site Projects\Project\App_Code\Upload.cs:line 127
at System.Web.HttpApplication.CallHandlerExecutionStep.System.Web.HttpApplication.IExecutionStep.Execute()
at System.Web.HttpApplication.ExecuteStep(IExecutionStep step, Boolean& completedSynchronously)
InnerException:
Please help me.
Thanks
cts-mgraham
Contributor
3318 Points
642 Posts
Microsoft
Re: store logged in user id in database from class file
Feb 28, 2012 11:12 AM|LINK
Is HttpContext.Current.User.Identity.Name actually returning what you are expecting? Can you give much more detail on how you are doing this (for example, are you using AspNetMembershipProvider?). Maybe you can go through this walkthrough and let us know if you have issues:
http://msdn.microsoft.com/en-us/library/879kf95c.aspx
HTH
sophia_asp
Member
521 Points
469 Posts
Re: store logged in user id in database from class file
Feb 28, 2012 04:09 PM|LINK
HI I am trying to upload images using httphandler.
for this I have few settings in web.config as below.
<httpHandlers>
<remove verb="POST,GET" path="Upload.axd" />
<add verb="POST,GET" path="Upload.axd" type="Upload" />
</httpHandlers>
in aspx page I am having a control to upload files, which in turn transfers the control to the Upload.cs class in App_code folder, where code is as follows (I am logged in at the time of uploading files)
public class Upload : IHttpHandler, IRequiresSessionState
{
public Upload()
{
}
#region IHttpHandler Members
public bool IsReusable
{
get { return true; }
}
public void ProcessRequest(HttpContext context)
{
using (ProjectEntities myEntities = new ProjectEntities())
{
MyFile myFile;
string virtualFolder = "~/Images/";
//string uploadPath = context.Server.MapPath(context.Request.ApplicationPath + virtualFolder);
string uploadPath = context.Server.MapPath(virtualFolder);
// loop through all the uploaded files
for(int j = 0; j < context.Request.Files.Count; j++)
{
// get the current file
HttpPostedFile uploadFile = context.Request.Files[j];
// if there was a file uploded
if (uploadFile.ContentLength > 0)
{
string fileName = Guid.NewGuid().ToString();
string extension = Path.GetExtension(uploadFile.FileName);
myFile = new MyFile();
myFile.Name = uploadFile.FileName;
myFile.Url = virtualFolder + fileName + extension;
myFile.Owner = (Guid)Membership.GetUser().ProviderUserKey;
//myFile.Owner = (Guid)Membership.GetUser(HttpContext.Current.User.Identity.Name).ProviderUserKey;
myEntities.AddToMyFile(myFile);
myEntities.SaveChanges();
uploadFile.SaveAs(Path.Combine(uploadPath, fileName + extension));}
}
}
}
}
The code breaks at when storing logged in user's id. Please help me.
Thanks
sophia_asp
Member
521 Points
469 Posts
Re: store logged in user id in database from class file
Mar 03, 2012 04:52 PM|LINK
cts-mgraham
Contributor
3318 Points
642 Posts
Microsoft
Re: store logged in user id in database from class file
Mar 08, 2012 01:42 PM|LINK
Can you verify that you are getting this value? Just have a page that does Response.Write(HttpContext.Current.User.Identity.Name);
My best guess is that you don't have a value there - you are maybe using Anonymous authentication? You could also just add a good username in the web.config to see if it gets past the error:
<identity impersonate="true" userName="good user" password="password" />
sophia_asp
Member
521 Points
469 Posts
Re: store logged in user id in database from class file
Mar 10, 2012 10:59 AM|LINK
HI cts,
yes I was not getting the username there (I don't know why) but later I passed the userid directly through querystring, and then it is working.
If you can point out why it was not getting username in initial code then it would be very helpful for me to point out the mistake.
Many thanks