Friday, 10 November 2017

Create Login Page through sqlserver using web.config file with the connected mode architecture in ASP.Net with C#

*These namespaces are must be required for SqlServer Connectivity in your web form:-
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

Login.aspx
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Login.aspx.cs" Inherits="ShowDataOnePage2010.Login" %>
<!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></title>
    <style type="text/css">
        .style1
        {
            width27%;
            margin-left363px;
        }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
   
        <table class="style1">
            <tr>
                <td colspan="4" align="center">
                    <b style="font-size: medium; font-weight: bold; font-family: Georgia; color: #800000;">Login</b></td>
            </tr>
            <tr>
                <td>
                    Name:</td>
                <td>
                    <asp:TextBox ID="txtName" runat="server"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Password:</td>
                <td>
                    <asp:TextBox ID="txtPass" runat="server" TextMode="Password"></asp:TextBox>
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td>
                    Select</td>
                <td>
                    <asp:DropDownList ID="DropDownList1" runat="server" style="font-weight: 700">
                        <asp:ListItem>----Select----</asp:ListItem>
                        <asp:ListItem>Admin</asp:ListItem>
                        <asp:ListItem>Visitor</asp:ListItem>
                    </asp:DropDownList>
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
            <tr>
                <td colspan="4">
                    <asp:Label ID="lblMsg" runat="server" Font-Bold="True"></asp:Label>
                </td>
            </tr>
            <tr>
                <td>
                    &nbsp;</td>
                <td align="center">
                    <asp:Button ID="btnClick" runat="server" Font-Bold="True"
                        onclick="btnClick_Click" Text="Login" />
                </td>
                <td>
                    &nbsp;</td>
                <td>
                    &nbsp;</td>
            </tr>
        </table>
    </div>
    </form>
</body>
</html>

Web.Config File
<?xml version="1.0"?>
<configuration>
  <appSettings>
    <add key="ConnectionString" value="Server=PRADEEP-HP;Database=Decofloore2;User Id=User_006;password=user006"/>
   </appSettings>
</configuration>

Login.aspx.cs Page
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace ShowDataOnePage2010
{
    public partial class Login : System.Web.UI.Page
    {
       
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected void btnClick_Click(object sender, EventArgs e)
        {
            string StrCon = ConfigurationManager.AppSettings["ConnectionString"];
            SqlConnection SqlCon = new SqlConnection(StrCon);
            string SqlQuery = "Select UserName,Password from Registration where UserName='" + txtName.Text.Trim() + "' and Password='" + txtPass.Text.Trim() + "'";
            SqlCommand SqlComm = new SqlCommand(SqlQuery, SqlCon);
            SqlCon.Open();
            SqlDataReader dr;
            dr = SqlComm.ExecuteReader(CommandBehavior.CloseConnection);
            if (dr.Read())
            {
                Session["userName"] = dr["UserName"].ToString();
                Session["password"] = dr["Password"].ToString();
                if (dr["Password"].ToString() != null)
                    Response.Redirect("~/Default2.aspx");
                else
                    Response.Redirect("~/Default.aspx");
            }
            else
            {
                lblMsg.Text = "Invalid User";
            }
            SqlCon.Close();
        }
    }
}

0 comments

Post a Comment