Hi,
I was new to c# and asp.net .I want to create a simple login page using ASP.net with c# code behind.I had 2 textboxs(username and id) and 1 button control.I stored all the username and id in MS ACCESS database.Database name "user.mdb" and table name
"table1" (only 2 fields username and password).Once users enter their username & id,and by clicking button control,it must check into the database.If is it in database means ,this login page call other .aspx page.if is it not in database means "error should arise not in the database".I dont how to proceed .........and i need a full code regarding on this.Please help me...........


Thanks
santhanalakshmi

//code for aspx page
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Login.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Untitled Page</title>
</head>
<body style="text-align: center; color: navy; background-color: #ffffcc;">
    <form id="form1" runat="server">
    <div >
        &nbsp;
        <asp:Label ID="lblLogin" runat="server" Font-Bold="True" Font-Names="Shruti" Font-Size="XX-Large"
            Height="48px" Text="User Login" Width="170px"></asp:Label><br />
        <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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &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; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;
        <asp:Panel ID="panLogin" runat="server" Height="184px" Width="529px">
            <br />
            &nbsp;&nbsp;
            <asp:Label ID="lblUser" runat="server" Font-Bold="False" Font-Names="Microsoft Sans Serif"
                Font-Size="Larger" Height="21px" Text="User Name" Width="100px"></asp:Label>
            &nbsp; &nbsp;&nbsp;
            <asp:TextBox ID="txtUser" runat="server" Font-Names="Times New Roman" Font-Size="Medium"
                Height="21px" Width="200px"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RUservalid" runat="server" ControlToValidate="txtUser"
                ErrorMessage="User Name Cannot be blank" Font-Size="Medium" SetFocusOnError="True"></asp:RequiredFieldValidator><br />
            <br />
            <asp:Label ID="lblPwd" runat="server" Font-Names="Microsoft Sans Serif" Font-Size="Larger"
                Height="21px" Text="Password" Width="100px"></asp:Label>
            &nbsp; &nbsp;&nbsp;
            <asp:TextBox ID="txtPwd" runat="server" Font-Names="Times New Roman" Font-Size="Medium"
                Height="21px" TextMode="Password" Width="200px"></asp:TextBox>
            <asp:RequiredFieldValidator ID="RPwdvalidate" runat="server" ControlToValidate="txtPwd"
                ErrorMessage="Password Cannot be blank" Font-Size="Medium" SetFocusOnError="True"></asp:RequiredFieldValidator><br />
            <br />
            &nbsp;<asp:RadioButton ID="RbtnAdmin" runat="server" Font-Names="Microsoft Sans Serif"
                Font-Size="Medium" GroupName="Login" Height="16px" Text="Administrator" Width="135px" Font-Bold="True" />
            &nbsp;<asp:RadioButton ID="RbtnUser" runat="server" Checked="True" Font-Names="Times New Roman"
                Font-Size="Large" GroupName="Login" Height="21px" Text="Users" Width="74px" /><br />
            <br />
            &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
            <asp:HyperLink ID="hlnkUpwd" runat="server" Width="135px" NavigateUrl="~/PasswordRecovery.aspx">Forgot Password!??</asp:HyperLink><br />
            <br />
            &nbsp; &nbsp; &nbsp; &nbsp;&nbsp;<asp:Button ID="btnLogin" runat="server" Font-Bold="True"
                Font-Size="Medium" ForeColor="DarkBlue" Height="25px" Text="LogIn" Width="85px" OnClick="btnLogin_Click" /><br />
            <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;<asp:HyperLink ID="hlinkUser"
                runat="server" Font-Names="Times New Roman" Font-Size="Medium" Height="21px"
                NavigateUrl="~/Registration.aspx" ToolTip="To create an Account to Become the member of this community"
                Width="218px">New User??? Create An Account</asp:HyperLink><br />
            &nbsp; &nbsp; &nbsp;
        </asp:Panel>
        &nbsp;&nbsp;<br />
        &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;
        &nbsp; &nbsp; &nbsp; 
        <asp:Label ID="lblDisplayError" runat="server" Font-Bold="True" Font-Size="Larger"
            ForeColor="Red" Height="30px" Width="251px"></asp:Label><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;
        <asp:Label ID="Label1" runat="server" ForeColor="#00C000" Height="28px" Width="212px"></asp:Label>
        &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;
        &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;
        &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;

    </div>
    </form>
</body>
</html>

//code for that page(Login.CS)
protected void btnLogin_Click(object sender, EventArgs e)
    {
        uname = txtUser.Text;
        pwd = txtPwd.Text;
        chkLogin();
    }
        ////To check whether the administrator is Logingin
    private void chkLogin()
    {
        if (RbtnAdmin.Checked == true)
        {
            try
            {
                sqlconn.Open();
                string CommandString = "SELECT ADMINISTRATOR,PASSWORD FROM ADMIN";
                objadapter = new SqlDataAdapter(CommandString, sqlconn.ConnectionString);
                dataset = new DataSet();
                objadapter.Fill(dataset, "ADMIN");
                DataTable datatable = dataset.Tables[0];//for filling dataset values into datatable
                for (int i = 0; i < datatable.Rows.Count; i++)
                {
                    string unam =datatable.Rows[i]["ADMINISTRATOR"].ToString();
                    string upwd = datatable.Rows[i]["PASSWORD"].ToString();

                    if ((unam == uname))
                    {
                        Server.Transfer("AdminSettings.aspx");
                    }
                    else
                    {
                        lblDisplayError.Focus();
                        lblDisplayError.Text = "Check UserName Or Password";
                    }
                }
            }
            catch (Exception lea)
            {
                lblDisplayError.Text = lea.Message;
            }
            finally
            {
                sqlconn.Close();
            }
        }
        if (RbtnUser.Checked == true && RbtnAdmin.Checked==false )
        {
            try
            {
                sqlconn.Open();
                string Commandssring = "SELECT FNAME,NAME,PASSWORD,STATUS FROM REGISTER";
                objadapter = new SqlDataAdapter(Commandssring, sqlconn.ConnectionString);
                dataset = new DataSet();
                objadapter.Fill(dataset, "Login");
                DataTable datatable = dataset.Tables[0];//for filling dataset values into datatable
                for (int i = 0; i < datatable.Rows.Count; i++)
                {
                    string unam1 = datatable.Rows[i]["NAME"].ToString();
                    string upwd1 = datatable.Rows[i]["PASSWORD"].ToString();
                    string Rname = datatable.Rows[i]["FNAME"].ToString();
                    int stat = Convert.ToInt32(datatable.Rows[i]["STATUS"]);
                    if (stat == 1)
                    {
                        if ((unam1 == uname) && (upwd1 == pwd))
                        {
                            Session["name"] = Rname;
                            Server.Transfer("Settings.aspx");
                        }
                         else
                         {
                             lblDisplayError.Focus();
                             lblDisplayError.Text = "Check UserName and Password";
                         }
                    }
                    else
                    {
                       Label1.Focus();
                       Label1.Text = "Check the Status";
                    }
                }
                /*if (flag == 0)
                {
                    lblDisplayError.Focus();
                    lblDisplayError.Text = "Check Status";
                }*/
            }
            catch (Exception lea)
            {
                lblDisplayError.Text = "Error" + lea.Data;
            }
            finally
            {
                sqlconn.Close();
            }

        }

Edited 3 Years Ago by Reverend Jim: Fixed formatting

Probably just bringing this back from the dead... but is there a reason why all the spacing is used? Is it neccesary?

This article has been dead for over six months. Start a new discussion instead.