Fetch a Data from database into a Login form

In this article I will describe, how a user can fetch a data from database using login Form. This is an elementary level of article for fetching data from database. By using this technique you can fetch entire data from the database to a web form.

  1. Open visual studio

  2. Drag a panel to web form for login box.

  3. Now drag some Textboxes ,Labels and Buttons on web form

    <asp:Panel ID="Panel1" runat="server"BackColor="Lime"BackImageUrl="~/App_Data/img1.JPG"BorderColor=
    "#00CC00"
    Height="224px"Style="margin-left: 287px"Width="488px">&nbsp;&nbsp;<br />
    &nbsp;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:LabelID="Label2"
    runat="server"Text="Username"></asp:Label>&nbsp;<asp:TextBox ID="TextBox1" runat="server"Height="22px"OnTextChanged="TextBox1_TextChanged1"Width="229px"
    AutoCompleteType="Disabled"></asp:TextBox><br/><br/>
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<asp:LabelID=
    "Label3"
    runat="server"Text="Password"></asp:Label>&nbsp;<asp:TextBoxID=
    "TextBox2"
    runat="server"Width="226px"
    TextMode="Password">
    </asp:TextBox>&nbsp;<br/>
    &nbsp;&nbsp;&nbsp;&nbsp
    ;&nbsp;&nbsp;&nbsp;&nbsp;
    <br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;
    Email id&nbsp;&nbsp;&nbsp;&nbsp;<asp:TextBoxID="TextBox3"runat="server"
    Width="223px"AutoCompleteType ="Disabled"></asp:TextBox><br />
    &nbsp;&nbsp;&nbsp;<br /> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    &nbsp;&nbsp;
    <asp:ButtonID="Button1"runat="server"OnClick="Button1_Click"Text="Sign In" /><br/>

  4. Now create database Using sqlserver.

  5. Create connection for database
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page

{
SqlConnection con;
SqlCommand cmd;
SqlDataReader dr;
string str;

protected void Page_Load(object sender, EventArgs e)

{
con =new SqlConnection("Data source =MCNDESKTOP06 ;Initial Catalog=empinfo;UserID=sa;Password = wintellect");
con.Open();
}

6. Insert the data into the database which you are created

7. Now write the code on button click event

protected void Button1_Click(object sender, EventArgs e)
{
str = "select * from login info  where name ='" + TextBox1.Text + "' and Password ='" + TextBox2.Text + "' and emailid ='" + TextBox3.Text + "'";
cmd = new SqlCommand(str, con);
dr = cmd.ExecuteReader();
if (dr.Read())
{
Session["name"] = dr.GetString(0);
Response.Redirect("account.aspx");
}
else
{
Label4.Text = "Invalid user";
}
}

Output of the application


_img1..jpg

After clicking button Sign In


_img2..jpg


Similar Articles