Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 4 > ASP.NET 4 General Discussion
|
ASP.NET 4 General Discussion For ASP.NET 4 discussions not relating to a specific Wrox book
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 4 General Discussion section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 26th, 2012, 12:52 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile store logged in user id in database from class file

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...
 
Old February 28th, 2012, 12:06 PM
Friend of Wrox
 
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Smile

Hi sophia

what error do you receive with App_Code?

as you know Membership Provider settings are stored in web.config file. so maybe you have two web.config file in your program (in sub-directories) with different Membership settings. but I'm not sure about what I said!!

are you getting error or it does not return the right result?
Are you using Windows Authentication?
__________________
happy every time, happy every where

Reza Baiat
 
Old February 28th, 2012, 02:02 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

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.Application Path + 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
 
Old February 28th, 2012, 08:06 PM
Friend of Wrox
 
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Smile

Hi sophia

I think the problem maybe with Handler-Call-Time. when your handler is called?

Code:
public void Init(HttpApplication context)
{
    context.AuthorizeRequest += new EventHandler(context_AuthorizeRequest);
    context.PostAuthorizeRequest += new EventHandler(context_PostAuthorizeRequest);
}

void context_PostAuthorizeRequest(object sender, EventArgs e)
{
    throw new NotImplementedException();
}

void context_AuthorizeRequest(object sender, EventArgs e)
{
    HttpApplication context = (HttpApplication)sender;
    Rewrite(context);
}
these are two different calls: you may need to call your handler in PostAuthorizeRequest event of HttpContext object
__________________
happy every time, happy every where

Reza Baiat
 
Old February 29th, 2012, 05:34 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

can you please tell me where I should update my code
Thanks
 
Old March 2nd, 2012, 12:41 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Hi Imar,
kindly look here, can you please spot out the mistake.
 
Old March 3rd, 2012, 01:15 PM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Kindly any one please tell me how this can be overcome?
I am struggling with this problem for many days.

Pleas help.
Thanks
 
Old March 4th, 2012, 12:56 AM
Friend of Wrox
 
Join Date: Nov 2009
Posts: 156
Thanks: 13
Thanked 16 Times in 16 Posts
Smile

I'll send it 4 hours later. I'm busy now ...
__________________
happy every time, happy every where

Reza Baiat
 
Old March 6th, 2012, 01:59 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

Hi, are you still busy?
 
Old March 10th, 2012, 07:46 AM
Friend of Wrox
 
Join Date: Oct 2009
Posts: 341
Thanks: 14
Thanked 3 Times in 3 Posts
Smile

OK done....
I passed id through querystring.





Similar Threads
Thread Thread Starter Forum Replies Last Post
how to query database tables depending on the logged-in user. Sapan ASP.NET 4 General Discussion 1 September 20th, 2011 02:40 PM
Read file and store it to database sparsh2010 Access VBA 3 July 17th, 2008 08:27 AM
How to store MS Word File in database kesar SQL Language 2 March 1st, 2007 06:34 AM
Setting logged in UserName to Session ID Admiral1701 ASP.NET 2.0 Professional 10 December 1st, 2006 10:35 AM
how to store text file in database vivekkumar_23 Servlets 1 February 13th, 2006 05:29 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.