hello ;
my question is, how we can create login in c-sharp. it means, if i enter username and password and then i press login button. the name and password compare from the table of "login" that are exist in the sql database . if username and password will be match from the username and password of the database table then display on the top of the web page "welcome MR OR MS XYZ". if the username and password are not match then display "invalid". i am writing this code in the login button. can u help me ?
protected void Button_login_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("select * from login where [user_name]='"+TextBox_user_name.Text+"' and [password]='"+TextBox_password.Text+"'");
cmd.Connection = connection.get();
cmd.Parameters.AddWithValue(" username ",TextBox_user_name.Text);
cmd.Parameters.AddWithValue(" password",TextBox_password.Text);
int c = cmd.ExecuteNonQuery();
if (c == 0)
{
Label_result.Text = "invalid user name and password";
}
else
Label_display_user_name.Text = "WELLCOME ";
}
regards
sumaira
Loading

Prabhu RajaPosted Aug 30, 2011, 3:35 AM
Your SqlCommand String is not good. try this one, create sqlcommand and pass parameters as,
SqlCommand cmd = new SqlCommand ("select * from login where username = @UserName and password=@Password")
cmd.Parameters.Add("@UserName", TextBox_user_name.Text);
cmd.Parameters.Add("@Password", TextBox_password.Text);
You try this one,
protected void Button_login_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand ("select * from login where username = @UserName and password=@Password");
cmd.Parameters.Add("@UserName", TextBox_user_name.Text);
cmd.Parameters.Add("@Password", TextBox_password.Text);
cmd.Connection = connection.get();
int c = cmd.ExecuteNonQuery();
if (c >0)
{
Label_display_user_name.Text = "WELLCOME ";
}
else
{
Label_result.Text = "invalid user name and password";
}
}
Thank You
sumaira manzoor hussain khanPosted Sep 1, 2011, 6:31 AM
using your code the same exception occur. actually i made a class of connection. my question is how i can establish connection of sql with my code.
this is my connection class.
this is my button code :
in which place, i am doing mistake.
regards
sumaira
Satyapriya NayakPosted Sep 1, 2011, 6:06 AM
Note:- ExecuteScalar() method requires connection to be opened.Have you opened your connection.
protected void Button1_Click(object sender, EventArgs e)
{
//Here UserName=Raj and Password=kumar present in database//
object obj = null;
SqlConnection con = new SqlConnection(strConnString);
con.Open();
Session["username"] = TextBox1.Text;
Session["password"] = TextBox2.Text;
//str = ("Select count(*) from Login where UserName='" + Session["username"] + "' and Password ='") + Session["password"] + "' ";
str = "Select count(*) from Login where UserName='" + Session["username"] + "' and Password ='" + Session["password"] + "' ";
//com = new SqlCommand(str, con);
SqlCommand com = new SqlCommand(str, con);
obj = com.ExecuteScalar();
if (Convert.ToInt32(obj) != 0)
{
lb1.Text = "Welcome:::" + Session["username"];
}
else
{
lb1.Text = "Invalid user name and password";
}
con.Close();
}
Thanks
If this post helps you mark it as answer
sumaira manzoor hussain khanPosted Sep 1, 2011, 5:11 AM
i wrote your code in the login button . but your code gave an exception . i am posting a file in docx format. u can see this
regards
sumaira
Satyapriya NayakPosted Aug 30, 2011, 7:10 AM
Here is your desired code.Please run the attachments.
using System;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class _Default : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
string str = null;
SqlCommand com ;
object obj = null;
protected void Button1_Click(object sender, EventArgs e)
{
//Here UserName=Raj and Password=kumar present in database//
SqlConnection con = new SqlConnection(strConnString);
con.Open();
Session["username"] = TextBox1.Text;
Session["password"] = TextBox2.Text;
str = ("Select count(*) from Login where UserName='" + Session["username"] + "' and Password ='") + Session["password"] + "' ";
com = new SqlCommand(str, con);
obj = com.ExecuteScalar();
if (Convert.ToInt32(obj) != 0)
{
lb1.Text = "Welcome:::" + Session["username"];
}
else
{
lb1.Text = "Invalid user name and password";
}
con.Close();
}
}
Thanks
If this post helps you mark it as answer