[Webinar] Streamline your web hosting managementRegister Today

x
?
Solved

Is it possible to store user preferences without Login

Posted on 2010-04-05
24
Medium Priority
?
482 Views
Last Modified: 2012-05-09
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?
0
Comment
Question by:xmalcolm
  • 12
  • 6
  • 4
  • +1
24 Comments
 
LVL 30

Expert Comment

by:Reza Rad
ID: 29772806
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
 

Author Comment

by:xmalcolm
ID: 29774057
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
 
LVL 30

Expert Comment

by:Reza Rad
ID: 29774303
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
The new generation of project management tools

With monday.com’s project management tool, you can see what everyone on your team is working in a single glance. Its intuitive dashboards are customizable, so you can create systems that work for you.

 

Author Comment

by:xmalcolm
ID: 29774796
So can I juut manually create a userid and put my preferences in there and then refer to it for filtering the result?
0
 
LVL 30

Expert Comment

by:Reza Rad
ID: 29775325
yes, you can do this, but this will not save anywhere. everytime internet browser closed your session data will abandoned.
0
 

Author Comment

by:xmalcolm
ID: 29775789
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
 
LVL 30

Expert Comment

by:Reza Rad
ID: 29776222
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
 
LVL 30

Expert Comment

by:Reza Rad
ID: 29776396
0
 
LVL 2

Expert Comment

by:mohammedrafiqraja
ID: 29778564
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
 

Author Comment

by:xmalcolm
ID: 29779247
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
 

Author Comment

by:xmalcolm
ID: 29779278
Also I am using ASP.NEt with C# and not VB
0
 
LVL 30

Expert Comment

by:Reza Rad
ID: 29779712
using cookie has this issue:
if two different users use same machine, preference of one will used for another and reverse.
0
 
LVL 2

Expert Comment

by:mohammedrafiqraja
ID: 29781335
0
 

Author Comment

by:xmalcolm
ID: 29781959
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
 

Author Comment

by:xmalcolm
ID: 29910247
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
 

Author Comment

by:xmalcolm
ID: 29928336
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
 
LVL 7

Expert Comment

by:illusio
ID: 30809947

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
 

Author Comment

by:xmalcolm
ID: 30814936
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
 
LVL 7

Accepted Solution

by:
illusio earned 2000 total points
ID: 30819551
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
 

Author Comment

by:xmalcolm
ID: 30827260
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
 

Author Comment

by:xmalcolm
ID: 30833130
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
 
LVL 7

Expert Comment

by:illusio
ID: 30873947
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
 

Author Comment

by:xmalcolm
ID: 30910173
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
 
LVL 7

Assisted Solution

by:illusio
illusio earned 2000 total points
ID: 30922817

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

Featured Post

[Webinar] Kill tickets & tabs using PowerShell

Are you tired of cycling through the same browser tabs everyday to close the same repetitive tickets? In this webinar JumpCloud will show how you can leverage RESTful APIs to build your own PowerShell modules to kill tickets & tabs using the PowerShell command Invoke-RestMethod.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
As many of you are aware about Scanpst.exe utility which is owned by Microsoft itself to repair inaccessible or damaged PST files, but the question is do you really think Scanpst.exe is capable to repair all sorts of PST related corruption issues?
The Relationships Diagram is a good way to get an overall view of what a database is keeping track of. It is also where relationships are defined. A relationship specifies how two tables connect to each other. As you build tables in Microsoft Ac…

611 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question