DOTNET:LOGIN CODE USING C#

Add ConnectionString in web.config as follows:

<connectionStrings>
<add name=”DefaultConnection” connectionString=”Data Source=.;Initial Catalog=Database name;Integrated Security=True;” providerName=”System.Data.SqlClient” />
</connectionStrings>

 

Declare connection and Datarow in C# page:

DataRow dr;
SqlConnection con = new SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings[“DefaultConnection”].ConnectionString);

protected void Btn_click(object sender, EventArgs e)
{
// to save the username and password in database
// nameBox = Request.Form["Email"];
string emailAccess = email.Text;
string pass=Password.Text;
SqlCommand cmd = new SqlCommand("select * from Register where email= @email and password=@password", con);
cmd.Parameters.AddWithValue("email", emailAccess);
cmd.Parameters.AddWithValue("password", pass);
SqlDataAdapter sda = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
sda.Fill(dt);
con.Open();
int i = cmd.ExecuteNonQuery();
con.Close();
if (dt.Rows.Count > 0)
{
dr = dt.Rows[0];
Session["email"] = emailAccess;
Session["id"] = Convert.ToString(dr[0]);
Response.Redirect("Home.aspx");
Session.RemoveAll();
}
else
{
Label1.Text = "Your Login details are incorrect";
}
}

Leave a Reply

Your email address will not be published. Required fields are marked *