-Log in
-Register (free)


-RSS Feeds
-New MyDesk Module
-Update to Profile

Alternate color for RS looping with For Next
Northwind / ASP - Nested select
ASP SQL Injection




 Forums / Back to page 1 /

Login - check database if user exists... (c#)
WizzKidd | Posted 9:18am 5. October 2007 Server Time |

Hi guys,

A very basic question of which I am stuck trying to get my head around as I have been a classic asp user for years.  Now I've moved to .net and from vb to c# i am slightly lost!

I am trying to to a simple login script that is a simple form with a username and password field. Upon clicking the submit button I would like to check the database if that username and password exist.  If it does I would like to set some session variables, if not then I would simply like to redirect.

As simple as it sounds, I can not achieve this.  Can anyone help?

I have managed to do the following...

string connectionString = "datasource=localhost;username=xxx;password=xxx;database=xxx";
MySqlConnection mySqlConnection = new MySqlConnection(connectionString);

string selectString =
"SELECT username, password " +
"FROM forum_members " +
"WHERE username = '" + frmUsername.Text + "' AND password = '" + frmPassword.Text + "'";

MySqlCommand mySqlCommand = new MySqlCommand(selectString, mySqlConnection);
mySqlConnection.Open();
String strResult = String.Empty;
strResult = (String)mySqlCommand.ExecuteScalar();
mySqlConnection.Close();

if (strResult.Length == 0)
{
  Label1.Text = "INCORRECT USER/PASS!"
  //could redirect to register page
} else {
  Label1.Text = "YOU ARE LOGGED IN!";
  //set loggin in sessions variables
}

As you can see above, I am using this ExecuteScalar() method which I've never seen before, but im not sure it's the best way to do this cause i've read that it returns just one result (humm im not sure).  Anyway, the code works almost. It gets as far as the if (strResultsLength == 0) block.  If the username and password exist in the database it is fine (ie. strLength is greater than 0) and shows the "YOU ARE LOGGED IN!" label, but if the username and password do not exist, i get an error: object reference not set to an instance of an object.

Help please?

- WizzKidd
Fredde | Posted 9:48am 5. October 2007 Server Time |

Can't you just reverse the if statement?  

if (strResult.Length > 0)
{
Label1.Text = "YOU ARE LOGGED IN!";
}

else
{
Label1.Text = "INCORRECT USER/PASS!"
}
stimpy | Posted 6:36pm 5. October 2007 Server Time |

Why bother doing this at all? Whizz, Look if you throw a Login control on the page and enable forms authentication in the web config file, and run either regiis or the web site Admin tool from within VWD and use that to set up the initial logins you'd be fine. Besides, Session variables are really not what you're looking for.
stimpy | Posted 6:36pm 5. October 2007 Server Time |

Hang ong and I'll post you a not to your email address in your profile and you can go from there... =)
stimpy | Posted 6:55pm 5. October 2007 Server Time |

Whizz just sent you an email to your hotmail account in your profile here. It contains the answer to all your questions about membership and security management
stimpy | Posted 6:56pm 5. October 2007 Server Time |

Check out the membership videos on ASP.NET in the video tutorials. It;s like 21 lines of code to set up logn access... LOL
WizzKidd | Posted 7:16am 7. October 2007 Server Time |

sweet stuff, gonna have a look now

- WizzKidd
WizzKidd | Posted 9:54am 7. October 2007 Server Time |

I've failed to mention that I am connecting to a MySQL database, and it seems that the Login control doesnt work with MySQL roles and memberships as they do not exist.  Do you have any knowledge in this?

- WizzKidd
stimpy | Posted 3:10pm 7. October 2007 Server Time |

Sure it does.. you need to get connector.net 5.12 I think and then I think there's some code on code project about building a custom membership provider I can send you. Otherwise, you could always just use the SQL Express DB for the login stuff and then run everything else out of MySQL. Most hosts that offer ASP.NET hosting also allow SQL Express DB's and as I recall, the max size of the SQL Express DB is something like 4GB so its got plenty of room just to do your login stuff. And if you're using mySQL 5 then MySQL 5 has full support for the ASP.NET Membership package.
stimpy | Posted 3:11pm 7. October 2007 Server Time |

Here's one example of what I am talking about
Membership and Role providers for MySQL
http://www.codeproject.com/aspnet/MySQLMembershipProvider.asp
nemesis1211 | Posted 1:19am 8. October 2007 Server Time |

I am developing an application using ASP.net; in that app during user registration I would like to check whether the username is available or not

And pop a label accordingly.

CaN i Get the code or any kinda algo to achieve this.


I have not used any login controls

Nemesis
1 2 3  [ Next Page >> ]
Show all replies to 'Login - check database if user exists... (c#)'
This post have been closed for new replies



| Info |
© Copyright 1997-2013 Alexander Haneng