User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 392,091 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,933 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser:
Views: 4776 | Replies: 15
Reply
Join Date: Aug 2007
Posts: 9
Reputation: Shannanigin is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Shannanigin Shannanigin is offline Offline
Newbie Poster

Validating a User Imput with SQL Database (Login)

  #1  
Aug 23rd, 2007
OK this is what I want to do:
  1. Log the User in by checking with a SQL Database with a table that has user name and password. Also that table is linked to another table with all there personal information
  2. After they login, they have to enter information into the field and send it to the database or they can get presetsfrom a database and and edit them if need be. From there they will send there specific information to a diffrent table which will contain there data.

A program like this has alreadh been created, its called FS ACARS that program also has some other cool functions, that I dont need.

The problem I am having trouble with is when the user enters there user name and password into 2 different text boxes, how do I check that information with the database?

So an example:

User name [ txtbox1 ] Password [ txtbox2 ]

I need to search the database to find that txtbox1.text is a valid user name. and if so, does txtbox2.text = the corresponding password?


I hope this is enough detail!

Thanks ahead of time!!!!
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Jun 2007
Posts: 321
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Rep Power: 3
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Validating a User Imput with SQL Database (Login)

  #2  
Aug 23rd, 2007
Send the text of those two text boxes as arguments to a stored procedure. The stored procedure should check if the username exists and the password matches. Something like this I guess.
create procedure ValidateUserLogin
  @UserName varchar(30)
  , @Password varchar(30)
as
begin
  if exists (select * from UsersTable as ut
    where ut.UserName = @UserName AND ut.Password = @Password)
    select 1;
  else
    select 0;
end
In your C# program you can run it like this.
  1. private bool IsValidatedUser( string username, string password ) {
  2. try {
  3. bool rv = false;
  4.  
  5. using ( SqlConnection con = new SqlConnection( connectionString ) ) {
  6. using ( SqlCommand cmd = new SqlCommand() ) {
  7. con.Open();
  8.  
  9. cmd.Connection = con;
  10. cmd.CommandType = CommandType.StoredProcedure;
  11. cmd.CommandText = "ValidateUserLogin";
  12.  
  13. cmd.Parameters.Add( "@UserName", SqlDbType.VarChar, 30 ).Value = username;
  14. cmd.Parameters.Add( "@Password", SqlDbType.VarChar, 30 ).Value = password;
  15.  
  16. rv = Convert.ToBoolean( cmd.ExecuteScalar() );
  17.  
  18. con.Close();
  19. }
  20. }
  21.  
  22. return rv;
  23. } catch ( Exception ex ) {
  24. // Log errors
  25. throw;
  26. }
  27. }
The truth does not change according to our ability to stomach it.
Reply With Quote  
Join Date: Aug 2007
Posts: 9
Reputation: Shannanigin is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Shannanigin Shannanigin is offline Offline
Newbie Poster

Re: Validating a User Imput with SQL Database (Login)

  #3  
Aug 23rd, 2007
Error 1 } expected 19 10 Costal
Error 2 Type or namespace definition, or end-of-file expected 68 5 Costal

And I am kinda confused on a couple of things:

1. Where do I put the C# code... would it go into the button_1 click event handler. OR should that be its own class??

How does it grab the data from the text boxes to validate it?
Reply With Quote  
Join Date: Jun 2007
Posts: 321
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Rep Power: 3
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Validating a User Imput with SQL Database (Login)

  #4  
Aug 23rd, 2007
Where do I put the C# code...
I don't know. That's not even code for your project, it's just something to give you ideas on how you might do it for yourself. I can help you place it if you describe how your project is laid out...
The truth does not change according to our ability to stomach it.
Reply With Quote  
Join Date: Aug 2007
Posts: 9
Reputation: Shannanigin is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Shannanigin Shannanigin is offline Offline
Newbie Poster

Re: Validating a User Imput with SQL Database (Login)

  #5  
Aug 23rd, 2007
I would really appreciate it... do you have an MSN or AIM or ICQ?? That way we can meet up sometime and you could help me? Or we can do it all here up to you!
Reply With Quote  
Join Date: Jun 2007
Posts: 321
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Rep Power: 3
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Validating a User Imput with SQL Database (Login)

  #6  
Aug 24th, 2007
It's easier to do it all here.
The truth does not change according to our ability to stomach it.
Reply With Quote  
Join Date: Aug 2007
Posts: 9
Reputation: Shannanigin is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Shannanigin Shannanigin is offline Offline
Newbie Poster

Re: Validating a User Imput with SQL Database (Login)

  #7  
Aug 24th, 2007
Ok here goes:

I have a button with two text boxes above it on its own form.

The button says OK. after the user enters his/her username & Password they click thte ok button and it validates them.

On the database side I have a .mdf file with 1 table in it that has three columns. UserName Password and userID. The UserID is for me its an identity column so i can keep track of users.

I also have the procedure you gave me above.

Where do i begin?

I know i need to get the data from the textboxes. and put them into variables.

then send them to the procedure to check in the database

then the procedure needs to send back to code. and change a bool variable to true... or give an error.

Thats what I know.. am I missing anything?
Reply With Quote  
Join Date: Jun 2007
Posts: 321
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Rep Power: 3
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Validating a User Imput with SQL Database (Login)

  #8  
Aug 24th, 2007
I guess you could just put the method in the form class. Then you call it in the button's click event.
  1. void buttonOK_Click( object sender, EventArgs e ) {
  2. bool isValid = this.IsValidatedUser(
  3. this.textBoxUserName.Text
  4. , this.textBoxPassword.Text );
  5.  
  6. if ( isValid ) {
  7. // Do whatever for a valid login
  8. } else {
  9. // Show an error for an invalid login
  10. }
  11. }
The truth does not change according to our ability to stomach it.
Reply With Quote  
Join Date: Aug 2007
Posts: 9
Reputation: Shannanigin is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
Shannanigin Shannanigin is offline Offline
Newbie Poster

Re: Validating a User Imput with SQL Database (Login)

  #9  
Aug 24th, 2007
Error 1 The type or namespace name 'SqlConnection' could not be found (are you missing a using directive or an assembly reference?) 17 24 Costal
Error 2 The type or namespace name 'SqlConnection' could not be found (are you missing a using directive or an assembly reference?) 17 48 Costal
Error 3 The name 'connectionString' does not exist in the current context 17 62 Costal
Error 4 The type or namespace name 'SqlCommand' could not be found (are you missing a using directive or an assembly reference?) 20 28 Costal
Error 5 The type or namespace name 'SqlCommand' could not be found (are you missing a using directive or an assembly reference?) 20 49 Costal
Error 6 The name 'CommandType' does not exist in the current context 27 43 Costal
Error 7 The name 'SqlDbType' does not exist in the current context 31 57 Costal
Error 8 The name 'SqlDbType' does not exist in the current context 33 57 Costal


I get all these errors?


What I did:

I made a new class file and put that method into the class. Is this what you meant or did you want me to put it into the program.cs file or put it into the form1.cs file?
Reply With Quote  
Join Date: Jun 2007
Posts: 321
Reputation: Hamrick will become famous soon enough Hamrick will become famous soon enough 
Rep Power: 3
Solved Threads: 33
Hamrick's Avatar
Hamrick Hamrick is offline Offline
Posting Whiz

Re: Validating a User Imput with SQL Database (Login)

  #10  
Aug 24th, 2007
You need to add the SqlClient reference to your project and add a using statement wherever you use those classes.
  1. using System.Data.SqlClient;
You need to store the connection string somewhere, that's what connectionString is; a variable in your application that stores a SQL connection string.
The truth does not change according to our ability to stomach it.
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

DaniWeb C# Marketplace
Thread Tools Display Modes

Similar Threads
Other Threads in the C# Forum

All times are GMT -4. The time now is 12:40 pm.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC