Simple login form in asp.net
In this article we will discuss about How to create or design a simple Login form in ASP.net with the help of C sharp and store the entries in SQL Server database.
You will need following software's to accomplish this:
Follow the steps below to make it:
- Visual Studio 2010
- SQL Server 2008
Follow the steps below to make it:
Step 1: First open your visual studio 2010 -> File -> New -> Web site -> ASP.NET Empty Web site -> click OK. (Make sure to select Visual C# from installed template and you can give the name to the website.)
Step 2: Now in Solution Explorer -> Add New Item -> Web Form -> click on Add.
Step 3: Now make the basic html table in the source section of empty form and insert the fields like -
Step 3: Already created registration table was used here.
To setup SQL Server connectivity Go to -> Server explorer -> Right click on Data Connections -> Add connection -> Select Data Source Microsoft SQL Server(SqlClient) -> Select server name -> Select or enter the name of database -> Test the connection -> Click Ok.
(To select the data source Go to -> Server explorer -> Right click on newly added database -> Properties -> Copy the connection string)
Step 5: Now double click on Login button to write C# code and copy paste the following code.
using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
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;
public partial class login : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void btnSubmit_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=SELSAK;Initial Catalog=Masterpage;Integrated Security=True");
con.Open();
SqlCommand cmd = new SqlCommand("select * from register where Username =@username and Pass=@password",con);
cmd.Parameters.AddWithValue("@username", txtUserName.Text);
cmd.Parameters.AddWithValue("@password", txtPWD.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if(dt.Rows.Count>0)
{
Session["new"] = txtUserName.Text;
Response.Redirect("Default2.aspx");
}
else
{
ClientScript.RegisterStartupScript(Page.GetType(), "validation", "<script language='javascript'>alert('Invalid Username and Password')</script>");
}
}
}
Html code of form:
<table id="logintable">
<tr>
<td><p >LOGIN FORM</p></td>
</tr>
<tr>
<td><asp:TextBox class="textbox" ID="txtUserName" runat="server" placeholder="Enter UserName Here"/></td>
<td><asp:RequiredFieldValidator ID="rfvUser" ErrorMessage="Please enter Username" ControlToValidate="txtUserName" runat="server" />
</td>
</tr>
<tr>
<td><asp:TextBox class="textbox" ID="txtPWD" runat="server" TextMode="Password" placeholder="Enter Password Here"/>
</td><td> <asp:RequiredFieldValidator ID="rfvPWD" runat="server" ControlToValidate="txtPWD" ErrorMessage="Please enter Password"/>
</td>
</tr>
<tr>
<td><asp:Button class="submit" ID="btnSubmit" runat="server" Text="Submit" onclick="btnSubmit_Click" /></td>
<td> </td>
</tr>
</table>
Step 6: Now hit on debug button to test the code. To check whether the entered username and password was already exists in your database. or return error message.
No comments:
Post a Comment