ASP Forum
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);
String strResult = String.Empty;
strResult = (String)mySqlCommand.ExecuteScalar();

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!";

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 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
nemesis1211 | Posted 1:19am 8. October 2007 Server Time |

I am developing an application using; 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

WizzKidd | Posted 2:10am 8. October 2007 Server Time |

I am using connecter 5.12 and funnily enough I found and followed the instructions on codeproject to install the membershp and role providers for mysql, but still no luck. Im doing something wrong obviously, but debugging my problem is most likely the main problem for me.

As for using a SQL Express database to manage to login stuff, that does infact sound like a option that will work, but crossing over database may not be the best option (im sure youd agree), this would mean i'd have to at least duplicate the users_id so that the same id exists in both database, so that whenever im refering to a username or email address in my pages, i can use the user_id ...but this seems all unnecessary.

I just feel stuck at my very first and what seems to be, a small hurdle. :(


I'm probably not the best one to give advice given my knowledge of this framework, however have a look on google for ".net login script AND ExecuteScalar()", im sure you'll get a lot of helpful results.

- WizzKidd
stimpy | Posted 6:08am 8. October 2007 Server Time |

Whizz, why would you need to duplicate user ID in the MySQL?

Just use the SQL Express to control login, access to certain directories etc and just run your content right out of MySQL.

I think I may have working example of a MySQL DB working with the login controls at home. I'll check later tonight.

Otherwise I think what you're looking for is a control or control extender in the AJax controls or Control Extender toolkit. However, having said that the forums over at http:// are fairly comprehensive. They even have a forum on connnecting ASP.NET to MySQL where you can ask all your questions about making the login controls work together.
stimpy | Posted 11:35am 8. October 2007 Server Time |

Here's a handy post about migrating ASP to ASP.NET

THe Data Access tutorial is VERY USEFUL
stimpy | Posted 4:47pm 8. October 2007 Server Time |

Hey Whizz, request received and answered. Check your email. You'll need something that can extract .rar files though. I don't have winzip, sorry.

There should be a complete project in there including the SQL script you'll need to build the database for the project. Just copy and paste the whole folder to your websites directory on your development box and the run it using F5. Don't forget to update the username and password for the DB in the web.config file once you get it set up.

Hopefully this will get you started...

Aren't you glad I'm basically trying to do the same thing you are at the same time??? LOL Although this project I gave you is as far as I have gotten with mine, although I have been studying my C#.

Anyway, you've got my gmail addy. contact me there if you need a hand or have questions.

Pretty soon you're going to need to put me on the payroll chief.. LOL
stimpy | Posted 5:21pm 8. October 2007 Server Time |

Whizz, just realized I uploaded the wrong version of the project. So I'll get the right version together for you and post that and send you an email when its up.
stimpy | Posted 6:13pm 8. October 2007 Server Time |

It seems that I did put the right version up... and as an added bonus I think it has the create user piece working too. I'm not sure, I just don't remember if I got that far or not yet.

Anyway, you know where to find me if you need anything.
stimpy | Posted 7:09pm 8. October 2007 Server Time |

Whizz, found an old post here Never pegged you for a Batman fan... The Mrs. is big into Bats..

And to quote A7N, so, whatever happened to kokogirls?
stimpy | Posted 3:05am 9. October 2007 Server Time |

Nemesis1211, I believe that the login controls for ASP.NET won't allow you to sign up for more than one account using the same email address. In short, you couldn't sign up under two screen names where both screen names had the email address as their email address.
WizzKidd | Posted 11:22am 29. October 2007 Server Time |

Are you using the login control?  If so, there are attributes (by default) that only allow a unique username to be added.  There are also attributes for unique email addresses too.  Just a matter of setting it to true or false in the web.config.

- WizzKidd
WizzKidd | Posted 11:23am 29. October 2007 Server Time |

The post above was in reply to nemisis's question.

- WizzKidd
Happyjai | Posted 11:36pm 2. October 2008 Server Time |

Dear Stimpy,

I am also having the same problem.  I have a database in which there is a table which contains Username, Password and Email entries.  

Now, I want to make a login page and validate these entries of this Login page with the attached database. Please help me out!!!!!!
bilbobaggins | Posted 11:54pm 1. February 2011 Server Time |

I am also having the same problem

<a href="/">#</a>
Sundaylove | Posted 7:13pm 17. March 2011 Server Time |

So I am interested in this! Yes,
codereflex | Posted 10:41pm 7. April 2011 Server Time |

[SOLVED] This link will solve your problem

Reply to Post Login - check database if user exists... (c#)

Back to Forum Page