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.

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

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>

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:

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

string connectionstring=configurationmanager.connectionstrings["nameof constring"].connectionstring;

also u can use the select command by stored procedures

Thank you very much, working perfectly now.

Your help is kindly appreciated.

string connectionstring = ConfigurationManager.ConnectionStrings["coffeeConnectionString1"].ConnectionString;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.