473,386 Members | 1,819 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

Login controls

Hi,

I am new to asp.net. I created an application using login controls.Now I have a problem. In SQLserver I have a database. In that I created a signup table.
the data which I enter in sign up application which contains username,password and email are stored in to that table.when user wants to login he enters username and password, Now these username and password values check with all the values in the table. If this username and password are in table, then user goes to next page, other wise A error message should be thrown. How the code for this.can you help me.
Nov 20 '07 #1
4 1274
shweta123
692 Expert 512MB
Hi,

You can do it like this way ,
e.g. You have taken Login control called as Login1

Expand|Select|Wrap|Line Numbers
  1.  
  2.      Dim Comm as new OleDbCommand
  3.     Comm.CommandText = "Select loginame from signup where login_name = '" & Login1.UserName.Trim & "' and password ='" & Login1.Password.Trim & "' "
  4.  
  5.             Comm.CommandType = CommandType.Text
  6.             Comm.Connection = conn
  7.  
  8.            Dim login_name as string = ""
  9.             If Not IsNothing(Comm.ExecuteScalar()) Then
  10.                  login_name = Comm.ExecuteScalar().ToString()
  11.            End If
  12.  
  13.             If  login_name = "" Then
  14.                 ''''''''Display Error Message , if loginId does not exist
  15.             Else
  16.                     '''''''Redirect to the page you want
  17.                   Response.Redirect("Someotherpage.asp")
  18.             End If
  19.  
Hi,

I am new to asp.net. I created an application using login controls.Now I have a problem. In SQLserver I have a database. In that I created a signup table.
the data which I enter in sign up application which contains username,password and email are stored in to that table.when user wants to login he enters username and password, Now these username and password values check with all the values in the table. If this username and password are in table, then user goes to next page, other wise A error message should be thrown. How the code for this.can you help me.
Nov 20 '07 #2
There are many ways to approach this scenario and accomplish what you are trying to write. The above sample code posted by shweta123 is a nice example but I do recommend a little different road to take.

This example above uses in-line SQL statements, one major mistake if you want to become victim of SQL injection attacks. But it really just depends on how secure and "code tight" you want your application. In the beginning, I think every developer used in-line SQL but as we continue to learn from others and keep an open mind every developer I know uses stored procedures to accomplish these types of tasks.

I am going to provide you an example of how my team would write out this type of EXAMPLE code to check the database for User Name, Password and Email so we can continue with the script. We would have all of this code broken down into different layers (Data Access, Business Logic and Presentation) but this is a simple code example.

THIS EXAMPLE BELOW IS RELATED TO SIGNING UP A USER, BUT YOU CAN GATHER THE SYNTAX FROM IT TO CHECK WHETHER THE USERS USERNAME AND PASSWORD ARE CORRECT.

DATABASE TABLE
Expand|Select|Wrap|Line Numbers
  1. App_Users (DATABASE TABLE)
  2. UserID (integer)
  3. UserName (nvarchar(64))
  4. UserPassword (nvarchar(256))
  5. UserEmail (nvarchar(50))
  6.  
DATABASE STORED PROCEDURE
Expand|Select|Wrap|Line Numbers
  1. CREATE PROCEDURE [App_Users_SignUpUser]
  2. (
  3. @UserName nvarchar(64),
  4. @UserPassword nvarchar(256),
  5. @UserEmail nvarchar(50)
  6. )
  7. AS
  8. IF EXISTS(SELECT UserID FROM App_Users WHERE UserName = @UserName OR UserEmail = @UserEmail)
  9. BEGIN
  10. -- USER EXISTS SO THROW ERROR IN CODE MATCHING VALUE USEREXISTS AND HAVE USER FILL OUT DIFFERENT INFORMATION
  11. -- return table 1
  12. SELECT 'UserExists' AS 'STATUS'
  13. END
  14. ELSE
  15. BEGIN
  16. -- USER DOES NOT EXIST YOU CAN CONTINUE CREATING NEW USER
  17. -- return table 1
  18. SELECT 'NoUserExisted' AS 'STATUS'
  19. -- insert new user information into table
  20. INSERT INTO App_Users ( UserName, UserPassword, UserEmail ) VALUES ( @UserName, @UserPassword, @UserEmail )
  21. END
  22. GO
  23.  
C# ASP.NET 2.0 CODE
Assuming you already have these items below here is the code behind page:
  1. Three textbox server controls and a button on the page with the OnClick event equal to btnSignIn_Click
  2. Downloaded SQL Helper from Microsoft (I can provide this if needed)
  3. You have a connection string declared in your web.config
Expand|Select|Wrap|Line Numbers
  1. public DataSet App_Users_SignUpUserMethod(string userName, string userPassword, string userEmail)
  2. {
  3.     DataSet ds = new DataSet();
  4.  
  5.  
  6.     try
  7.     {
  8.         // this sets up connection string from web.config file
  9.         // in order for the configuration manager to display, you
  10.         // must declare using System.Configuration namespace at top of page
  11.         string strMyConnectionString =    ConfigurationManager.ConnectionStrings["MY_DB_CONN"].ConnectionString;
  12.  
  13.         // this sqlhelper saves lots of time
  14.         ds = SqlHelper.ExecuteDataset(strMyConnectionString, "App_Users_SignUpUser", userName, userPassword, userEmail);
  15.      }
  16.     catch (Exception ex)
  17.     {
  18.         throw new Exception(ex.Message);
  19.     }
  20.  
  21.     return ds;
  22.  
  23. protected void btnSignIn_Click (object sender, EventArgs e)
  24. {
  25.     string userName = txtUserName.Text.Trim(); string userPassword = txtUserPassword.Text.Trim();string userEmail = txtUserEmail.Text.Trim();
  26.  
  27.     DataSet ds;
  28.     ds = App_Users_SignUpUserMethod(userName, userPassword, userEmail);
  29.  
  30.     // this STATUS is what is returned from the stored procedure
  31.     string status = ds.Tables[0].Rows[0]["STATUS"].ToString();
  32.  
  33.     // now we want to specify what we are doing next depending
  34.     // on what status was returned from the database
  35.     try
  36.     {
  37.         switch (status)
  38.         {    case "NoUserExisted":
  39.             // now you can write your method here for next step
  40.             // maybe you want to redirect them to thank you page
  41.             // note: this information has been inserted
  42.             // into the database because the user did not exist
  43.  
  44.             case "UserExists":
  45.             // now because a user exists with this information
  46.             // you may want to let them know in a label server control
  47.             lblMessage.Visible = true;
  48.             // in order for color.red to show, you must declare
  49.             // using System.Drawing namespace at top of page
  50.             lblMessage.ForeColor = Color.Red;
  51.             lblMessage.Text = "User Name / Email Already Exists";
  52.              break; 
  53.         }
  54.     }
  55.     catch (Exception ex)
  56.     {
  57.             throw new Exception(ex.Message);
  58.     }
  59.  
SHORT ENDING
Please remember every developer has their own style of coding. So once you find yours, you will be able to accomplish anything you set your mind to and be able to do it in your own way. This example is simply how we would have written it quickly. Also, of course I don't recommend using raw text for passwords stored in database neither so you may want to look into Rijndael Encryption Methods to encrypt the users passwords.

ALSO, IF YOU WANT AN EXAMPLE ON FORMS AUTHENTICATION WE MAY WANT TO TYPE IT UP IN A DIFFERENT THREAD.
Nov 20 '07 #3
Frinavale
9,735 Expert Mod 8TB
There are many ways to approach this scenario and accomplish what you are trying to write. The above sample code posted by shweta123 is a nice example but I do recommend a little different road to take.

This example above uses in-line SQL statements, one major mistake if you want to become victim of SQL injection attacks. But it really just depends on how secure and "code tight" you want your application. In the beginning, I think every developer used in-line SQL but as we continue to learn from others and keep an open mind every developer I know uses stored procedures to accomplish these types of tasks.

I am ...
Way to go Aaronsandoval!
Your response was very impressive.
Thanks

-Frinny
Nov 20 '07 #4
Thank you for your help.it is very helpful for me.
Nov 21 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Ash | last post by:
I want to provide the ability for my users to login from any page that they are on. ie. have a login user control with a username and password box on each page (not a link to a login page). ...
5
by: RedEye | last post by:
Hello, I am working on a test site to explore the new login controls and membership features of ASP.NET v2.0. I have tested the controls using SQL Express and have now decided to try using a...
14
by: clintonG | last post by:
This is an appeal for peer support sent to Microsoft as will be noted in closing. The Login control does not include a Cancel button. The only option is to convert the Login control to a...
3
by: Brian Henry | last post by:
I have an existing user managment system that has been in our asp.net 1.1 app for a couple years now, all the user managment is done through an application that is a win form, and stored in SQL...
5
by: John | last post by:
Hi What db does the built-in user control in asp.net uses? sql server? Access? is it possible to choose? Thanks Regards
5
by: Ronald S. Cook | last post by:
It's been longer that I remember since writing windows (not web) apps. 1) I want to load a main form 2) User clicks login button which brings up login form (on top of main form) 3) Upon...
3
by: ad | last post by:
I have create a custom membership provider. The common usage of custom membership is set it as default Membership Provider win web.config, and use login controls with it. How can I use custom...
1
by: frolda | last post by:
Hi, I moved my login.aspx page from root to a subdirectory and made -hopefully- all necessary changes for all Login controls. All the controls work just fine, except one. To my regret, the...
1
by: pj | last post by:
I'm trying to redirect users to another page after they Authenticate with the ASP.NET login controls. The user is able to login, but I can't get the response.redirect to work. Can anyone help? ...
2
by: mdavis | last post by:
Hello, I'm using a Login Control on asp.net 2.0 website and it works fine on IE & Firefox, however I have recently added a javscript function to listen for a 'return-key-click' and am encountering...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.