I have a login form im building for.. and got that working..
you can authenticate and view the main window if successful.
BUT i can set the values in my class and step thru the code and values are there until you get to the main window and they are set back to original null or false state.
Im only setting the record id and bool value if the user was authenticated during login.
So in my class i have this
public int _id { get; set; }
public bool _auth { get; set; }
Then within my dataset i have this
_id = Convert.ToInt32(dsser.Tables["Info"].Rows[0]["id"]);
_auth = Convert.ToBoolean(dsser.Tables["Info"].Rows[0]["auth"]);
Then on my login page im trying to check the value in order to display controls
if (dac._auth)
{
stackPanel1.Visibility = Visibility.Visible;
}
What am i overlooking?
Intermediate ASP.net User, Using VS2008/VS2010 with C# and SQL2005, SQL2008, Silverlight 3
---------------------
Mark as Answered if it helped
This is an ASP.Net forum so you probably won't get very good answers. Here is my not very good answer -
How are you setting the values in the dataset? Show us the code.
Have you used the debugger to see what values are in dsser.Tables["Info"].Rows[0] when you try to extract them? Are _id and _auth being set to what you expect? Are the values in the dataset what you expect? If not, what are they?
And as stated, the values are set and are the expected values, BUT never make it to where i need them.. So im more curious with how i have them setup in my class if its right or did i do something wrong there..
Intermediate ASP.net User, Using VS2008/VS2010 with C# and SQL2005, SQL2008, Silverlight 3
---------------------
Mark as Answered if it helped
Are you saying that UID and AUTH have the values that you expect? So, where is dsser declared? Is the other code you have shown in the same class as this code? What happens between calling the two pieces of code? How often is the code which fills dsser
executed? (Are you 100% certain? Did you put a breakpoint in to check?) Is there any other code which fills dsser? Are you sure that this is the same dsser variable in both pieces of code?
It is difficult with the limited information that you have given but the most likely explanation is that you are overwriting the values at some stage, maybe by calling the code you have shown more than once or in some other code that you have not shown.
Either the dsser you look at later in the code is a different variable or your code is overwriting the contents at some point.
Are there other tables in the dataset? Are they being manipulated (loaded/altered)?
UID and AUTH have the values i want only after the dataset is filled in the above code provided. The dataset is only called once which is on the login button event.
The properties and dataset are in my class, the if statement is in my button event.
I have placed break points on every line that has anything to do with the dataset and button event.
When i supply my id and password, it gets passed to the method and the breakpoints get hit and the correct values are returned..i continue to step thru the code and it returns to the button event and then the if condition is hit..BUT the value is no longer
correct. I have already searched and there is no other place the dataset is even referenced.
Is the way i have the UID and AUTH declared and set correct. Im i setting them in the right place, should i be setting the values somewhere else.. i mean at this point, im only working with 2 pages and a class, authentication page and page after you are
authenticated.
Intermediate ASP.net User, Using VS2008/VS2010 with C# and SQL2005, SQL2008, Silverlight 3
---------------------
Mark as Answered if it helped
I am more confused now. What variables are you using? Is it _id and _auth or are you using UID and AUTH? or are they different variables being set in different places? Or have you changed your mind about what you call them? Are you storing something
in the dataset and then reading back out more than once or just once into some vairables and then reusing those variables? It is difficult to know the scope that things have been declared at because you have not shown the structure and declaration of the
variables.
Perhaps it is time to show all the code? Or at least all the code that references any of the variables of interest.
Is this what you mean..in my dataaccess class i have the following:
private int _uid;
private bool _auth;
public int UID
{
get
{
return _uid;
}
set
{
_uid = value;
}
}
public bool AUTH
{
get
{
return _auth;
}
set
{
_auth = value;
}
}
Those two pieces you have just shown are surrounded by
public class dataaccess
is that right?
The second piece of code looks like it is part of a method. What is the signature of that method?
How is this class used? What pieces of code create instances of it? Is it possible to post the
complete files so that we don't have to guess how the pieces fit together? I am wondering if you are creating multple instances of this dataaccess class.
I thought you said that this was a WPF application but you seem to be inheriting from System.Web.UI.Page, I'm confused. In btnLogin_Click you are setting DialogResult which looks like forms. Can you confirm if this is a WPF application, in which case the
definition of MainMenu looks very strange or is this a WebForms application in which case the use of DialogResult looks wrong.
Anyway, in MainMenu you create a new DatabaseInterface, can you show the definition of that class? What relationship does it have to DataAccess.Class1? You create a new DatabaseInterface and then immediately access the value of the AUTH property, this
means that AUTH will have whatever value is set in the parameterless constructor or false if no value is set.
On the login page you do set AUTH, assuming dac is of type DataAccess.Class1 - it would be good to see all the code so that we can see how things are declared, their scope and access level rather than having to guess all the time. If this is a web forms
application then you need to save your state somehow (ViewState, cookie, etc) so that it is available to the next postback. If it is a WPF application then you need to save the result of the login check in some global location so that it is available to the
MainMenu class.
In Class1.UserLogin you retrieve the userpassword column and give it an alias of 'auth'. The userpassword column seems to be some variety of varchar. A couple of lines later you convert 'auth' to a bool. If a user's password is not 'True', 'False' or
null this will throw an exception. Are you sure that this is the code?
If this is a web forms application then I suggest that you use the inbuilt authentication system rather than trying to create your own, one place to get started is
http://www.asp.net/web-forms/tutorials/tailspin-spyworks/tailspin-spyworks-part-6. Storing plaintext passwords in a database is one of the very worst things you can do. Also, you might want to research sql injection attacks, as you are vulnerable.
Did I mention that it is very hard to help if you a) don't know what type of application you are creating and b) if you won't show us the code you are actually using?
cubangt
Contributor
3052 Points
2402 Posts
Trying to set property when executing authentication against DB but values are lost later in the ...
Apr 22, 2012 02:54 AM|LINK
Then within my dataset i have this _id = Convert.ToInt32(dsser.Tables["Info"].Rows[0]["id"]); _auth = Convert.ToBoolean(dsser.Tables["Info"].Rows[0]["auth"]);Then on my login page im trying to check the value in order to display controls if (dac._auth) { stackPanel1.Visibility = Visibility.Visible; } What am i overlooking?---------------------
Mark as Answered if it helped
Paul Linton
Star
13431 Points
2535 Posts
Re: Trying to set property when executing authentication against DB but values are lost later in ...
Apr 22, 2012 04:02 AM|LINK
This is an ASP.Net forum so you probably won't get very good answers. Here is my not very good answer -
How are you setting the values in the dataset? Show us the code.
Have you used the debugger to see what values are in dsser.Tables["Info"].Rows[0] when you try to extract them? Are _id and _auth being set to what you expect? Are the values in the dataset what you expect? If not, what are they?
cubangt
Contributor
3052 Points
2402 Posts
Re: Trying to set property when executing authentication against DB but values are lost later in ...
Apr 22, 2012 04:40 AM|LINK
I posted here because im having issues with c#..and i already updated my original post.
I posted how im setting the values in my dataset. Here is more code around it
try { if (conn.State != ConnectionState.Open) { conn.Open(); } NpgsqlDataAdapter npgData = new NpgsqlDataAdapter(command); npgData.Fill(dsser, "Info"); UID = Convert.ToInt32(dsser.Tables["Info"].Rows[0]["id"]); AUTH = Convert.ToBoolean(dsser.Tables["Info"].Rows[0]["auth"]); }And as stated, the values are set and are the expected values, BUT never make it to where i need them.. So im more curious with how i have them setup in my class if its right or did i do something wrong there..
---------------------
Mark as Answered if it helped
Paul Linton
Star
13431 Points
2535 Posts
Re: Trying to set property when executing authentication against DB but values are lost later in ...
Apr 22, 2012 05:16 AM|LINK
Are you saying that UID and AUTH have the values that you expect? So, where is dsser declared? Is the other code you have shown in the same class as this code? What happens between calling the two pieces of code? How often is the code which fills dsser executed? (Are you 100% certain? Did you put a breakpoint in to check?) Is there any other code which fills dsser? Are you sure that this is the same dsser variable in both pieces of code?
It is difficult with the limited information that you have given but the most likely explanation is that you are overwriting the values at some stage, maybe by calling the code you have shown more than once or in some other code that you have not shown. Either the dsser you look at later in the code is a different variable or your code is overwriting the contents at some point.
Are there other tables in the dataset? Are they being manipulated (loaded/altered)?
cubangt
Contributor
3052 Points
2402 Posts
Re: Trying to set property when executing authentication against DB but values are lost later in ...
Apr 22, 2012 03:35 PM|LINK
UID and AUTH have the values i want only after the dataset is filled in the above code provided. The dataset is only called once which is on the login button event.
The properties and dataset are in my class, the if statement is in my button event.
I have placed break points on every line that has anything to do with the dataset and button event.
When i supply my id and password, it gets passed to the method and the breakpoints get hit and the correct values are returned..i continue to step thru the code and it returns to the button event and then the if condition is hit..BUT the value is no longer correct. I have already searched and there is no other place the dataset is even referenced.
Is the way i have the UID and AUTH declared and set correct. Im i setting them in the right place, should i be setting the values somewhere else.. i mean at this point, im only working with 2 pages and a class, authentication page and page after you are authenticated.
---------------------
Mark as Answered if it helped
Paul Linton
Star
13431 Points
2535 Posts
Re: Trying to set property when executing authentication against DB but values are lost later in ...
Apr 22, 2012 11:06 PM|LINK
I am more confused now. What variables are you using? Is it _id and _auth or are you using UID and AUTH? or are they different variables being set in different places? Or have you changed your mind about what you call them? Are you storing something in the dataset and then reading back out more than once or just once into some vairables and then reusing those variables? It is difficult to know the scope that things have been declared at because you have not shown the structure and declaration of the variables.
Perhaps it is time to show all the code? Or at least all the code that references any of the variables of interest.
cubangt
Contributor
3052 Points
2402 Posts
Re: Trying to set property when executing authentication against DB but values are lost later in ...
Apr 23, 2012 01:10 AM|LINK
Is this what you mean..in my dataaccess class i have the following:
private int _uid; private bool _auth; public int UID { get { return _uid; } set { _uid = value; } } public bool AUTH { get { return _auth; } set { _auth = value; } }Here is my dataset within my dataaccess class
try { if (conn.State != ConnectionState.Open) { conn.Open(); } NpgsqlDataAdapter npgData = new NpgsqlDataAdapter(command); npgData.Fill(dsser, "Info"); UID = Convert.ToInt32(dsser.Tables["Info"].Rows[0]["id"]); AUTH = Convert.ToBoolean(dsser.Tables["Info"].Rows[0]["auth"]); } catch (NpgsqlException ex) { string msg = "FAILED -- "; msg += ex.Message; throw new Exception(msg); } finally { conn.Close(); } return dsser;---------------------
Mark as Answered if it helped
Paul Linton
Star
13431 Points
2535 Posts
Re: Trying to set property when executing authentication against DB but values are lost later in ...
Apr 23, 2012 01:15 AM|LINK
Those two pieces you have just shown are surrounded by
public class dataaccess
is that right?
The second piece of code looks like it is part of a method. What is the signature of that method?
How is this class used? What pieces of code create instances of it? Is it possible to post the complete files so that we don't have to guess how the pieces fit together? I am wondering if you are creating multple instances of this dataaccess class.
cubangt
Contributor
3052 Points
2402 Posts
Re: Trying to set property when executing authentication against DB but values are lost later in ...
Apr 23, 2012 02:47 AM|LINK
Here is my class file:
using System; using Npgsql; using System.Data; namespace DataAccess { class Class1 { private int _uid; private bool _auth; public int UID { get { return _uid; } set { _uid = value; } } public bool AUTH { get { return _auth; } set { _auth = value; } } private string _dbConn { get { return "Server=xxxxxxxx;Port=xxxxx;User Id=xxxxxx;Password=xxxxxx;Database=xxxxxxx;"; } } public DataSet UserLogin(string username, string userpass) { DataSet dsser = new DataSet(); NpgsqlConnection conn = new NpgsqlConnection(_dbConn); NpgsqlCommand command = new NpgsqlCommand("select id, userpassword as auth from info where username = '" + username + "' AND userpassword = '" + userpass + "';", conn); try { if (conn.State != ConnectionState.Open) { conn.Open(); } NpgsqlDataAdapter npgData = new NpgsqlDataAdapter(command); npgData.Fill(dsser, "Info"); UID = Convert.ToInt32(dsser.Tables["Info"].Rows[0]["id"]); AUTH = Convert.ToBoolean(dsser.Tables["Info"].Rows[0]["auth"]); } catch (NpgsqlException ex) { string msg = "FAILED -- "; msg += ex.Message; throw new Exception(msg); } finally { conn.Close(); } return dsser; } } }The page i need to check the variables on has this code..
public partial class MainMenu : System.Web.UI.Page { DatabaseInterface dac = new DatabaseInterface(); protected void Page_Load(object sender, EventArgs e) { if (dac.AUTH) { } else { } } }Then the login page where the method is called is like this
protected void btnLogin_Click(object sender, EventArgs e) { DataSet ds1 = dac.UserLogin(txtUser.Text, txtPassword.Text); if ((ds1 != null) && ds1.Tables[0].Rows.Count > 0) { DialogResult = true; this.Close(); } else { lblMessage.Content = "Login Failed! Try Again."; } }Thats the stripped down version of the only 3 pages/files that have anything to do with this.
---------------------
Mark as Answered if it helped
Paul Linton
Star
13431 Points
2535 Posts
Re: Trying to set property when executing authentication against DB but values are lost later in ...
Apr 23, 2012 07:35 AM|LINK
I thought you said that this was a WPF application but you seem to be inheriting from System.Web.UI.Page, I'm confused. In btnLogin_Click you are setting DialogResult which looks like forms. Can you confirm if this is a WPF application, in which case the definition of MainMenu looks very strange or is this a WebForms application in which case the use of DialogResult looks wrong.
Anyway, in MainMenu you create a new DatabaseInterface, can you show the definition of that class? What relationship does it have to DataAccess.Class1? You create a new DatabaseInterface and then immediately access the value of the AUTH property, this means that AUTH will have whatever value is set in the parameterless constructor or false if no value is set.
On the login page you do set AUTH, assuming dac is of type DataAccess.Class1 - it would be good to see all the code so that we can see how things are declared, their scope and access level rather than having to guess all the time. If this is a web forms application then you need to save your state somehow (ViewState, cookie, etc) so that it is available to the next postback. If it is a WPF application then you need to save the result of the login check in some global location so that it is available to the MainMenu class.
In Class1.UserLogin you retrieve the userpassword column and give it an alias of 'auth'. The userpassword column seems to be some variety of varchar. A couple of lines later you convert 'auth' to a bool. If a user's password is not 'True', 'False' or null this will throw an exception. Are you sure that this is the code?
If this is a web forms application then I suggest that you use the inbuilt authentication system rather than trying to create your own, one place to get started is http://www.asp.net/web-forms/tutorials/tailspin-spyworks/tailspin-spyworks-part-6. Storing plaintext passwords in a database is one of the very worst things you can do. Also, you might want to research sql injection attacks, as you are vulnerable.
Did I mention that it is very hard to help if you a) don't know what type of application you are creating and b) if you won't show us the code you are actually using?