Solved

Authenticate Users (Login Page)

Posted on 2004-08-08
3
222 Views
Last Modified: 2010-04-15
Hi guys,
I have two forms, form1 and form2. I also have this login page written in C# in form1. I'm trying to authenticate users through SQL database, if authenticated they will login to form 2, if not an error message should pop up and not allow form2 to open. Also after authentication is sucessfull, form1 should close and form2 should stay open. First concentrate on authenticating users and allowwing form2 to open. Afterwards form1 should close automatically if users are authenticated.Here's the code:

da.Open();

SqlCommand thisCommand = da.CreateCommand();
String sQuery = "SELECT count(*) FROM License WHERE Name = '"+ textBox1.Text +"' AND LicenseID = '"+ textBox2.Text +"'"

SqlCommand cmd = new SqlCommand(sQuery, da);
SqlDataReader dr = cmd.ExecuteReader();
Form2 frm = new Form2();
//if (dr.["License"].Rows.Count == 0)
//MessageBox.Show("Incorrect UserName and/or ID, Try again!");
da.Close();
frm.Show();

I'm stuck on authentication and closing the form1, can someone direct me where i have gone wrong.
Thanks, Les
0
Comment
Question by:lesmydad
3 Comments
 
LVL 2

Expert Comment

by:jkrill
ID: 11746998
I believe this MSDN article describes how to accomplish what you are looking for:

http://support.microsoft.com/default.aspx?scid=kb;EN-US;301240
0
 
LVL 9

Accepted Solution

by:
s_sansanwal earned 35 total points
ID: 11749948
Replace Code as

da.Open();
SqlCommand thisCommand = da.CreateCommand();
String sQuery = "SELECT count(*) FROM License WHERE Name = '"+ textBox1.Text +"' AND LicenseID = '"+ textBox2.Text +"'"
SqlCommand cmd = new SqlCommand(sQuery, da);
SqlDataReader dr = cmd.ExecuteReader();
                  if(dr.Read())
                  {
                        dr.Close();
                                                                da.Close();
                        this.Visible = false;
                        Form2 frm = new Form2();
                        frm.ShowDialog();
                        this.Close();
                  }
                  else
                  {
                        dr.Close();
                                                                da.Close();
                        MessageBox.Show("Incorrect UserName and/or ID, Try again!");
                  }


Cheers,
S Sansanwal
0
 
LVL 1

Author Comment

by:lesmydad
ID: 11751032
S Sansanwal,
The above script does not authenticate users, but it closes form1 and opens form2. Any this was my fault, thw sQuery should had this command
SELECT Name, LicenseID FROM License     instead of

SELECT count(*) FROM License
This authticates users, then opens form2 and closes form1. If users are not authenticated the error message is shown.
Any way thanks for your help.
Les
0

Featured Post

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

Introduction                                                 Was the var keyword really only brought out to shorten your syntax? Or have the VB language guys got their way in C#? What type of variable is it? All will be revealed.   Also called…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Access reports are powerful and flexible. Learn how to create a query and then a grouped report using the wizard. Modify the report design after the wizard is done to make it look better. There will be another video to explain how to put the final p…

743 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now