954,174 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

asp.net login with sql

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.

protected void Page_Load(object sender, EventArgs e)
        {
            string connectionstring = @"Data Source=localhost\sqlexpress;Initial Catalog=coffee;Integrated Security=True;Pooling=False";                        
            SqlConnection connection = new SqlConnection(connectionstring);
            connection.Open();
        }


However once submit button is clicked it needs to check the user input against the database. Any help i'd be extremely grateful.

dawsonz
Light Poster
49 posts since Feb 2010
Reputation Points: 10
Solved Threads: 1
 

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

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"); }

kdcorp87
Junior Poster in Training
54 posts since Jan 2010
Reputation Points: 14
Solved Threads: 9
 

Thanks for your help :)

I've written in my web.config

<appSettings/>
    <connectionStrings>
        <add name="coffeeConnectionString1" connectionString="Data Source=localhost\sqlexpress;Initial Catalog=coffee;Integrated Security=True;Pooling=False"
            providerName="System.Data.SqlClient" />
    </connectionStrings>
dawsonz
Light Poster
49 posts since Feb 2010
Reputation Points: 10
Solved Threads: 1
 

Thanks for your help :)

I've written in my web.config

<appSettings/>
    <connectionStrings>
        <add name="coffeeConnectionString1" connectionString="Data Source=localhost\sqlexpress;Initial Catalog=coffee;Integrated Security=True;Pooling=False"
            providerName="System.Data.SqlClient" />
    </connectionStrings>

U R IN COMPLETELY RIGHT TRACK:cool:

kdcorp87
Junior Poster in Training
54 posts since Jan 2010
Reputation Points: 14
Solved Threads: 9
 

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:

namespace number1
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
     
        }

        protected void btnSubmit_Click1(object sender, EventArgs e)
        {
            string username = txtUsername.Text;
            string password = txtPassword.Text;

            string connectionstring = @"Data Source=localhost\sqlexpress;Initial Catalog=coffee;Integrated Security=True;Pooling=False";

            SqlConnection connection = new SqlConnection(connectionstring);

            string str = "SELECT * FROM users WHERE username= '" + username + "'" + "AND password='" + password + "'";

            SqlCommand cmd = new SqlCommand(str, connection);
            connection.Open();
            SqlDataReader login = cmd.ExecuteReader();

            if (login.HasRows)
            {
                Session["username"] = username;
                Session["password"] = password;
                login.Close();
                connection.Close();
                Response.Redirect("After_Login.aspx");     
            }
         }
    }
dawsonz
Light Poster
49 posts since Feb 2010
Reputation Points: 10
Solved Threads: 1
 
string connectionstring=configurationmanager.connectionstrings["nameof constring"].connectionstring;

also u can use the select command by stored procedures

kdcorp87
Junior Poster in Training
54 posts since Jan 2010
Reputation Points: 14
Solved Threads: 9
 

Thank you very much, working perfectly now.

Your help is kindly appreciated.

string connectionstring = ConfigurationManager.ConnectionStrings["coffeeConnectionString1"].ConnectionString;
dawsonz
Light Poster
49 posts since Feb 2010
Reputation Points: 10
Solved Threads: 1
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You