The example illustrates simple login form using Asp.Net
In this example I will create simple login form using SQL in Asp.Net. Let's see step by step.
Let's create a simple login page.
<table>
<tr>
<td>User Name:</td>
<td><asp:TextBox ID="txtUserName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td>Password:</td>
<td><asp:TextBox ID="txtPassword" runat="server" TextMode="Password"></asp:TextBox></td>
</tr>
<tr>
<td>
<asp:Button ID="btnSubmit" runat="server" Text="Login" OnClick="btnSubmit_Click" />
</td>
<td>
<asp:Label ID="mess" runat="server"></asp:Label>
</td>
</tr>
</table>
Patte below code to perform login.
protected void btnSubmit_Click(object sender, EventArgs e)
{
if(!string.IsNullOrWhiteSpace(txtUserName.Text) && !string.IsNullOrWhiteSpace(txtPassword.Text))
{
cstring.Open();
SqlCommand cmd1 = new SqlCommand("select * from userLogin where username='" + txtUserName.Text.Trim() + "' and Password='" + txtPassword.Text + "'", cstring);
SqlDataReader dr = cmd1.ExecuteReader();
if (dr.HasRows)
{
dr.Read();
string usr = dr[2].ToString();
FormsAuthentication.RedirectFromLoginPage(usr, false);
Session["user"] = dr["UserName"].ToString();
dr.Close();
}
else
{
mess.Text = "Please enter correct user name, password..";
}
cstring.Close();
}
else
{
mess.Text = "Please enter correct user name, password..";
}
}
Hop this can help you.