943,104 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Unsolved
  • Views: 937
  • ASP.NET RSS
Feb 9th, 2010
0

asp.net login with sql

Expand Post »
I'm trying to create a login system that checks the username and passsword from an sql database.

I've created a local server named coffee that has a table named 'users' - I have created fields for username & password.

I'm using ASP.NET web application and I want to connect to my SQL database 'coffee' and check that if username / password typed into textboxes is the same as database then redirect to new page.

I've tried looking at a few tutorials but its confusing the hell out of me.

I've tried creating my connection string to the database.

ASP.NET Syntax (Toggle Plain Text)
  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. string connectionstring = @"Data Source=localhost\sqlexpress;Initial Catalog=coffee;Integrated Security=True;Pooling=False";
  4. SqlConnection connection = new SqlConnection(connectionstring);
  5. connection.Open();
  6. }

However once submit button is clicked it needs to check the user input against the database. Any help i'd be extremely grateful.
Similar Threads
Reputation Points: 10
Solved Threads: 1
Light Poster
dawsonz is offline Offline
49 posts
since Feb 2010
Feb 9th, 2010
0
Re: asp.net login with sql
If u write ur connection string in config file that would be good,
however as a basic tutorial can look that, can get coon string from db properties

Quote ...
STRING USER=TEXBOX1.TEXT;
STRING PASS =TEXBOX2.TEXT
string connectionstring = @"data source=.\\sqlexpress;attachdbfilename=database1.mdf;integrated security=true;user instance=true";

sqlconnection connection = new sqlconnection(connectionstring);
string str = "select * from cafffe where username= '" + user + "'" + "and password='" + password + "'";
sqlcommand cmd = new sqlcommand(str, connect);
connect.open();
sqldatareader red = cmd.executereader();
if (red.hasrows)
{

session["user"] = user;
session["pass"] = password;
red.close();
connect.close();
response.redirect("userpage.aspx");
}
Reputation Points: 14
Solved Threads: 9
Junior Poster in Training
kdcorp87 is offline Offline
54 posts
since Jan 2010
Feb 9th, 2010
0
Re: asp.net login with sql
Thanks for your help

I've written in my web.config

ASP.NET Syntax (Toggle Plain Text)
  1. <appSettings/>
  2. <connectionStrings>
  3. <add name="coffeeConnectionString1" connectionString="Data Source=localhost\sqlexpress;Initial Catalog=coffee;Integrated Security=True;Pooling=False"
  4. providerName="System.Data.SqlClient" />
  5. </connectionStrings>
Reputation Points: 10
Solved Threads: 1
Light Poster
dawsonz is offline Offline
49 posts
since Feb 2010
Feb 9th, 2010
0
Re: asp.net login with sql
Click to Expand / Collapse  Quote originally posted by dawsonz ...
Thanks for your help

I've written in my web.config

ASP.NET Syntax (Toggle Plain Text)
  1. <appSettings/>
  2. <connectionStrings>
  3. <add name="coffeeConnectionString1" connectionString="Data Source=localhost\sqlexpress;Initial Catalog=coffee;Integrated Security=True;Pooling=False"
  4. providerName="System.Data.SqlClient" />
  5. </connectionStrings>
U R IN COMPLETELY RIGHT TRACK
Reputation Points: 14
Solved Threads: 9
Junior Poster in Training
kdcorp87 is offline Offline
54 posts
since Jan 2010
Feb 9th, 2010
0
Re: asp.net login with sql
Thanks for your help kdcorp.

I've got it working creating my connection string, how can I get it so that I dont have to type a connection string and do it through my web.config

Code:

ASP.NET Syntax (Toggle Plain Text)
  1. namespace number1
  2. {
  3. public partial class _Default : System.Web.UI.Page
  4. {
  5. protected void Page_Load(object sender, EventArgs e)
  6. {
  7.  
  8. }
  9.  
  10. protected void btnSubmit_Click1(object sender, EventArgs e)
  11. {
  12. string username = txtUsername.Text;
  13. string password = txtPassword.Text;
  14.  
  15. string connectionstring = @"Data Source=localhost\sqlexpress;Initial Catalog=coffee;Integrated Security=True;Pooling=False";
  16.  
  17. SqlConnection connection = new SqlConnection(connectionstring);
  18.  
  19. string str = "SELECT * FROM users WHERE username= '" + username + "'" + "AND password='" + password + "'";
  20.  
  21. SqlCommand cmd = new SqlCommand(str, connection);
  22. connection.Open();
  23. SqlDataReader login = cmd.ExecuteReader();
  24.  
  25. if (login.HasRows)
  26. {
  27. Session["username"] = username;
  28. Session["password"] = password;
  29. login.Close();
  30. connection.Close();
  31. Response.Redirect("After_Login.aspx");
  32. }
  33. }
  34. }
Reputation Points: 10
Solved Threads: 1
Light Poster
dawsonz is offline Offline
49 posts
since Feb 2010
Feb 9th, 2010
0
Re: asp.net login with sql
Quote ...
string connectionstring=configurationmanager.connectionstrings["nameof constring"].connectionstring;
also u can use the select command by stored procedures
Last edited by kdcorp87; Feb 9th, 2010 at 11:17 am.
Reputation Points: 14
Solved Threads: 9
Junior Poster in Training
kdcorp87 is offline Offline
54 posts
since Jan 2010
Feb 9th, 2010
0
Re: asp.net login with sql
Thank you very much, working perfectly now.

Your help is kindly appreciated.

ASP.NET Syntax (Toggle Plain Text)
  1. string connectionstring = ConfigurationManager.ConnectionStrings["coffeeConnectionString1"].ConnectionString;
Reputation Points: 10
Solved Threads: 1
Light Poster
dawsonz is offline Offline
49 posts
since Feb 2010

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: timer_tick event in ASp.net C#
Next Thread in ASP.NET Forum Timeline: JavaScript and CheckBox





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC