Solved

Authenticate Users (Login Page)

Posted on 2004-08-08
3
227 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Article by: Ivo
C# And Nullable Types Since 2.0 C# has Nullable(T) Generic Structure. The idea behind is to allow value type objects to have null values just like reference types have. This concerns scenarios where not all data sources have values (like a databa…
Introduction Although it is an old technology, serial ports are still being used by many hardware manufacturers. If you develop applications in C#, Microsoft .NET framework has SerialPort class to communicate with the serial ports.  I needed to…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

738 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