Is it possible to store user preferences without Login

Hi Everyone,

I am creating a car share website. I have a google map which will show icons. Currently the icon info is stored in an XML file which is populated from a SQL server database. What I want to do is for the map to show the icons based on the user's preferences.

How can I store a user's preferences without having a login? Is it possible to do that?
xmalcolmAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Reza RadConsultant, TrainerCommented:
I'm afraid you can not.
because application needs to now the preference related to what user? and this means that user should log in first. or at least a user id must specified somewhere in input.
0
xmalcolmAuthor Commented:
Ok so is there no way I can have a session variable somewhere in the code and populate the relevant data manually as opposed to automatically gathering the preferences from the database for that user?
0
Reza RadConsultant, TrainerCommented:
you can use session, but you should a session variable for USERID at first, if this variable was set before so you have no problem for using it.

0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

xmalcolmAuthor Commented:
So can I juut manually create a userid and put my preferences in there and then refer to it for filtering the result?
0
Reza RadConsultant, TrainerCommented:
yes, you can do this, but this will not save anywhere. everytime internet browser closed your session data will abandoned.
0
xmalcolmAuthor Commented:
ah ok. But r u suggesting its best to have a login feature? and if so - do you think it can be done fairly easily or fast cos I dont have much time.
0
Reza RadConsultant, TrainerCommented:
I recommend login.
if you have few time, don't worry, you can use asp.net membership provider,
this will create a database for users and logins and all authentications.
there are controls in asp.net which works with this membership provider as well. like login control ,
the only thing you need is a tutorial on ASP.NET MEMBERSHIP PROVIDER
0
Reza RadConsultant, TrainerCommented:
0
mohammedrafiqrajaCommented:
Using Cookies you can restore the last logged user's preference without login.
Set the cookies expire time to some days or weeks

Try below code

Mohammed Rafi
UserPreference.txt
UserPreference.aspx.vb
0
xmalcolmAuthor Commented:
Hi Mohammad,

Thanks for the code, but I am not sure if cookie will be appropriate. I wont have a form which will need a user to input their preferences. The preferences of the user is already stored in the database. What I want my website to do is to retrieve the preferences of the user who is logged in from the database automatically. And then use that preference info to refer to when filtering results.
0
xmalcolmAuthor Commented:
Also I am using ASP.NEt with C# and not VB
0
Reza RadConsultant, TrainerCommented:
using cookie has this issue:
if two different users use same machine, preference of one will used for another and reverse.
0
mohammedrafiqrajaCommented:
0
xmalcolmAuthor Commented:
No I cant I am sorry. But what I want to clarify is that - I am designing this website for my final year project at uni. So its only gonna be used once to show that it works, which is why I could say I have used session variable to store the preferences but when a user will log in their preferences will be retrieved from the database. And I will only need to show that it works once on my demonstration day.

So considering the above info, do you think I can just put a session variable in and if so how do I do it and where do i put the code?
0
xmalcolmAuthor Commented:
Ok i had a look at the tutorial, but my question is - how can I use an existing database to authenticate users. I do not want to create another database, is it not possible to use the email address and password field from my table which i have already created?
0
xmalcolmAuthor Commented:
Hi Reza,

I managed to get login sorted but I dont have any session variable right now. I need to have one and thats what I am trying to do as I do not know how to. Also I need my users to remain logged in - how do i ensure that they remain logged in. And how do i get the user's preferences to be retrieved and stored in the session variable as soon as the user logs in?
0
illusioSoftware Architect / Team LeaderCommented:

The context of your asp.net webpage holds the session.
HttpContext.Current.Session <-- you can use this to write and read the session values
Or you can access the session on the page you are using through the Session property of the Page.

Upon a succesful log in, you can retrieve the values from the database and store them into the session. Best create a typed class that accesses the HttpSessionState to read and write the values.
0
xmalcolmAuthor Commented:
ANy ideas on how I could retrieve the values from db and store it in the session variable. I currently have profiles which basically stores it. but it does not retrieve the values from db. i have attached my .cs code for login.

This is how I am storing my preferences on profile right now.

Profile.SmokeP = rbSmokeP.SelectedValue;
        Profile.GenderP = rbGenderP.SelectedValue;
        Profile.JourneyP = rbJourneyP.SelectedValue;
        Profile.Save();
protected void Login_Authenticate(object sender, AuthenticateEventArgs e)
    {

        bool Authenticated = false;
        Authenticated = SiteLevelCustomAuthenticationMethod(Login1.UserName, Login1.Password);
        e.Authenticated = Authenticated;
        if (Authenticated == true)
        {
            FormsAuthentication.SetAuthCookie(Login1.UserName.ToString(), Login1.RememberMeSet);
            Session["Username"] = Login1.UserName.ToString();
        }
        Response.Redirect("SearchPage.aspx");
    }
    
    private bool SiteLevelCustomAuthenticationMethod(string UserName, string Password)
    {
        bool boolReturnValue = false;
        string strConnection = "Data Source=XG7;Initial Catalog=Car_Share_XG;Integrated Security=True";
        SqlConnection myConn = new SqlConnection(strConnection);
        String strSQL = "Select * From tbl_Member";
        SqlCommand command = new SqlCommand(strSQL, myConn);
        SqlDataReader Dr;
        myConn.Open();
        Dr = command.ExecuteReader();
        while (Dr.Read())
        {
            if ((UserName == Dr["Member_email"].ToString()) & (Password == Dr["Member_password"].ToString()))
            {
                boolReturnValue = true;
                break;
            }
            
        }
        Dr.Close();
        return boolReturnValue;
    }

Open in new window

0
illusioSoftware Architect / Team LeaderCommented:
my oh my...

A few comments:
- if (Authenticated == true) <-- normally "if (Authenticated)" is enough, it doesn't turn more true if you put the equation...
- if ((UserName == Dr["Member_email"].ToString()) & (Password == Dr["Member_password"].ToString()) <-- you use a bitwise combination "&", this should be a "&&" (logical AND)

- String strSQL = "SELECT * FROM tbl_Member"; <-- you can make this a whole lot easier if you would just do: "select count(*) from tbl_Member WHERE Member_email=@email and Member_password=@password";
you then have
SqlCommand command = new SqlCommand(strSQL, myConn);
command.Parameters.AddWithValue("@email", UserName);
command.Parameters.AddWithValue("@email", Password);
command.Connection.Open()
try
{
dim result as Object = command.ExecuteScalar();
boolReturnValue = (result != null && Convert.ToInt32(result) == 1);
}
finally
{command.Connection.Close();}

- if I'm correct you have trouble writing the query to "load" your preferences again?
just do the same thing you did to query the login, only go to the table that has the preferences. Forsee a filter on the user with a where clause and retrieve his preferences.
Add a static function Load to your Profile class: public static Profile Load(string Username) { ... }
in that static function you connect to the databank, using a sqldatareader as you do in your snippet and read the values of each datarecord in a new profile object.
When you have authenticated your user, do the Profile.Load(...) and you are ready to go.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
xmalcolmAuthor Commented:
Hi illusio,

thanks for the info. I am a bit new at this so been taking the long way around by the looks of it.
btw i tried this but i get a red underline under the method loginsitelevel. And I noticed you gave me some vb code which obviously wont work with mine as i am using C# so i made some changes to that too.

private bool SiteLevelCustomAuthenticationMethod(string UserName, string Password)
    {
        bool boolReturnValue = false;
        string strConnection = "Data Source=XG7;Initial Catalog=Car_Share_XG;Integrated Security=True";
        SqlConnection myConn = new SqlConnection(strConnection);
        String strSQL = "SELECT COUNT * from tbl_Member WHERE Member_email=@email AND Member_password=@password";
        SqlCommand command = new SqlCommand(strSQL, myConn);
        command.Parameters.AddWithValue("@email", UserName);
        command.Parameters.AddWithValue("@password", Password);
        myConn.Open();
        try
        {
            Object result = command.ExecuteScalar();
            boolReturnValue = (result != null && Convert.ToInt32(result) == 1);
        }
        finally
        {
            command.Connection.Close();
            myConn.Close();
           
        }
0
xmalcolmAuthor Commented:
Well i just noticed i was not returning a bool value.
return boolReturnValue;

thats sorted but i get this error saying
Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near the keyword 'FROM'.
0
illusioSoftware Architect / Team LeaderCommented:
it's count(*) and not count *
and sorry about the vb.net/c# mix up - I use both languages and switch often - it leaves me confused from time to time.
0
xmalcolmAuthor Commented:
Hi illusio,

that sorted it out - thank you. but could you explain if possible what is exactly happening in the following statement?

I know its to do with returning the value back to the previous method. But what is it doin exactly?
try
        {
            Object result = command.ExecuteScalar();
            boolReturnValue = (result != null && Convert.ToInt32(result) == 1);
        }
0
illusioSoftware Architect / Team LeaderCommented:

Object result = command.ExecuteScalar();
--- ExecuteScalar gives the first column of the first record of the query you execute
--- we store this into an object because it's not always clear what you will get as result
--- if no records are returned, the result will be "null"

boolReturnValue = (result != null && Convert.ToInt32(result) == 1);
--- here we construct a boolean result
--- (result != null) <-- check for null value, should evaluate to "true"
--- (Convert.ToInt32(result) == 1) <-- converts the result to an integer (the count of records) and checks if you just got only one record as result (which should be the result since you evaluate a login). Evaluates to "true"
--- That gives "true" && "true", gives "true"

The fun part is that if the (result!=null) returns "false" the second part won't be evaluated since "false" and something else always evaluates as "false"
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.