0

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.

2
Contributors
6
Replies
9
Views
7 Years
Discussion Span
Last Post by dawsonz
0

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

Edited by Nick Evan: Fixed formatting

0

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>
0

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:

0

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

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

also u can use the select command by stored procedures

Edited by kdcorp87: n/a

0

Thank you very much, working perfectly now.

Your help is kindly appreciated.

string connectionstring = ConfigurationManager.ConnectionStrings["coffeeConnectionString1"].ConnectionString;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.