0

i am new on asp.net.
i want to know how to check my username and password by using MYSQL...
i use the following code behing my .cs file

MySqlConnection conn = new MySqlConnection("Server=localhost;Database=dbabc;Uid=root;Pwd=;");
    protected void Page_Load(object sender, EventArgs e)
    {
        try
        {
            conn.Open();
            Response.Write("MySQL conenct successfully");

        }
        catch (MySqlException error)
        {
            Response.Write("MySQL could not be connect to the server try agian!");
        }

    }
    protected void signin_btn_Click(object sender, EventArgs e)
    {
        int i=0;
        string sql = "SELECT username, password from login where username='" + username_tb + "' and password = '" + password_tb + "'" ;
        MySqlCommand cmd = new MySqlCommand(sql, conn);
        MySqlDataReader rdr = cmd.ExecuteReader();
        if (rdr.Read())
        {
            result.InnerText = rdr[0].ToString();
        }
        else
        {
            result.InnerText = "Sorry record Not found!";
        }


        conn.Close();
    }

and these are the following code in index.aspx

Username : <input type="text" id="username_tb" name="username_tb" style="weight:250px; height: 25px;" runat="server"></input><br />
Password : <input type="password" id="password_tb" name="password_tb" style="weight:250px; height:25px;" runat="server"></input><br />
<input type="submit" name="submit" value="Signin" style="weight:30px; height:25px; margin-top: 3px;" runat="server" onserverclick="signin_btn_Click"></input><br />

</div>
<label id="result" runat="server"></label>

so finally i need to say that i want a result which says username and password are correct in case of sucess and username and password was not correct in case of failure

4
  • What exactly is your question? Or are you just asking us to write code for you? Commented Jan 8, 2012 at 17:34
  • yes. i want to know how to use SELECT command and return a string "Username and password is correcr" or "username and password is incorrect"... Commented Jan 8, 2012 at 17:39
  • or you can edit a command of MySQL in my given script as well... Commented Jan 8, 2012 at 17:43
  • my code work fine. but the problem is that whether i type coreect username and password.. it always give a "Sorry record Not found!" which exist in else{ result.InnerText = "Sorry record Not found!"; } Commented Jan 8, 2012 at 17:48

2 Answers 2

1

I assume username_tb and password_tb are TextBox controls? In that case, you need to refer to the .Text inside of them, not to the controls themselves. Try changing your sql string to this:

string sql = "SELECT username, password from login where username='" + username_tb.Text + "' and password = '" + password_tb.Text + "'" ;

There's a lot of other problems with your code (like it being very susceptible to SQL injection attacks), but I won't try to correct them all here.

Sign up to request clarification or add additional context in comments.

1 Comment

Yes i got my error... but one thing i mention at here that .text is written with asp textbox field. i use .value with my textbox.. thanks...
1

How to check the username and password using a Sql data source in ASP.NET:

protected void btsubmit_Click(object sender, EventArgs e)
{  
     string constr = ConfigurationManager.ConnectionStrings["remCoString"].ConnectionString;
     SqlConnection conn = new SqlConnection(constr);
     conn.Open();

    SqlCommand cmd = new SqlCommand("SELECT Username, Password from login where Username='" + usertxt.Text + "' and Password = '" + passtxt.Text + "'", conn);
    cmd.Parameters.AddWithValue("@Username", usertxt.Text);
    cmd.Parameters.AddWithValue("@Password", passtxt.Text);
    SqlDataAdapter da = new SqlDataAdapter(cmd);
    DataTable dt = new DataTable();
    da.Fill(dt);
    if (dt.Rows.Count > 0)
    {        
        Response.Redirect("Details.aspx");
    }
    else
    {    
        msg.Text = "user name and password are wrong";     
    }

    conn.Close();
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.