Click here to Skip to main content
15,891,704 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
static void Main(string[] args)
        {
            SPSite mySite = new SPSite("sitename");
            SPWeb myWeb = mySite.OpenWeb();
            SPContext currentContext = SPContext.Current;
            string userEmail = "emailaddress"; // get from txtEmail.Text control
            string userName = Membership.GetUserNameByEmail(userEmail);
            Console.WriteLine("Your UserName is: " + userName);
        }


This code is retuning NULL. What am i doing wrong in this code. i am trying to get userID using email .

[edit]Code block added - OriginalGriff[/edit]

[Added from OP]
I meant to say. UserName is Null. Membership.GetUserNameByEmail is returning Null value.
Posted
Updated 14-Jun-11 7:31am
v6
Comments
Sergey Alexandrovich Kryukov 14-Jun-11 11:04am    
The code is only Main. This is a void method. How can it return null?
What returns null?
--SA
Peter Mulholland 14-Jun-11 11:46am    
Do you need to load the Membership details into Membership from somewhere before calling GetUserNameByEmail?
Is it possible "emailaddress" (not a valid email address) is not in Membership and the return value of null should be expected for this address?
anjumnavid 14-Jun-11 11:48am    
no i don't. emailaddress is in the database. not matter which email i put in, result is Null.

Do you have the proper settings in the app.config file?

SPSite mySite = new SPSite("sitename");
SPWeb myWeb = mySite.OpenWeb();
SPContext currentContext = SPContext.Current;


You seem to be attempting to work with SharePoint without a proper understanding of it and how to work with it.

SPContext.Current has no meaning on a console application, there is no SharePoint context this code is running under. You are also not properly disposing of the SPSite object. And really, there is not need whatsoever you use any of this with the code you have given. Period.
 
Share this answer
 
Comments
anjumnavid 14-Jun-11 14:07pm    
My Config file settings is good. All the other stuff i put on , just for the hack.

This is the original code.


string userName = Membership.GetUserNameByEmail(userEmail );
if (userName != null)
Console.WriteLine("Your UserName is: " + userName);

else
Console.WriteLine(userEmail + " is not a member of this site");

Console.ReadLine();
}
[no name] 14-Jun-11 20:28pm    
Instead of posting code that has been given to you why don't you post the app.config. The settings may be there but are they correct?
This is .ascx File

C#
namespace UserByEmail
{
    public partial class UserByEmail : System.Web.UI.UserControl
    {
        string _User = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            string _Email = _txtEmail.Text;
            _User = Membership.GetUserNameByEmail(_Email);
        }
        protected void Btn_Send_Click(object sender, EventArgs e)
        {
            Response.Write("This is the User : " + _User);
        }
    }
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900