SIGN UP MEMBER LOGIN:    
ARTICLE

Fetch a Data from database into a Login form

Posted by Brijesh Jalan Articles | ADO.NET in C# July 20, 2010
In this article we will learn how to fetch data from database in login form.
Reader Level:
 

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:Label ID="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:Label ID="Label3" runat="server" Text="Password"></asp:Label>&nbsp;<asp:TextBox ID="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:TextBox ID="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:Button ID="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

Login to add your contents and source code to this article
share this article :
post comment
 

how can do this code in oracle

Posted by anupam Jan 25, 2012

Nice article

Posted by mohan kumar Nov 21, 2011

Do u know then tell me...

Posted by Brijesh Jalan Jul 30, 2010

What?!? Do you know what Sql Injection is? Bad... Very bad...

Posted by Zen I Jul 20, 2010
Nevron Gauge for SharePoint
Become a Sponsor
PREMIUM SPONSORS
  • The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
    The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
Become a Sponsor