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 Ex9
{
public
partial class WebForm1 : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void ButLogin_Click(object sender, EventArgs e)
{
string maniconn =
ConfigurationManager.ConnectionStrings["Myconnection"].ConnectionString;
SqlConnection sqlconn = new SqlConnection(maniconn);
SqlCommand sqlcomm = new SqlCommand("select * from [dbo].[Table]
where Email=@Email and Password=@Password", sqlconn);
sqlcomm.Parameters.AddWithValue("Email", TxtUsername.Text);
sqlcomm.Parameters.AddWithValue("Password", Txtpwd.Text);
SqlDataAdapter
sda = new SqlDataAdapter(sqlcomm);
DataTable dt = new DataTable();
sda.Fill(dt);
sqlconn.Open();
sqlcomm.ExecuteNonQuery();
sqlconn.Close();
if(dt.Rows.Count>0)
{
Session["id"] = TxtUsername.Text;
Response.Redirect("Welcome.aspx");
Session.RemoveAll();
}
else
{
LitMsg.Text = "Username & Password are wrong !";
}
}
}
}
<?xml version="1.0"
encoding="utf-8"?>
<!--
For more
information on how to configure your ASP.NET application, please visit
https://go.microsoft.com/fwlink/?LinkId=169433
-->
<configuration>
<connectionStrings>
<add
name="Myconnection" connectionString="Data
Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\DHANSHRI\Documents\UserLogin.mdf;Integrated
Security=True;Connect Timeout=30"
providerName="System.Data.SqlClient"/>
</connectionStrings>
<system.web>
<compilation debug="true" targetFramework="4.7.2"
/>
<httpRuntime targetFramework="4.7.2" />
</system.web>
<system.codedom>
<compilers>
<compiler language="c#;cs;csharp" extension=".cs"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.CSharpCodeProvider,
Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:default
/nowarn:1659;1699;1701" />
<compiler language="vb;vbs;visualbasic;vbscript"
extension=".vb"
type="Microsoft.CodeDom.Providers.DotNetCompilerPlatform.VBCodeProvider,
Microsoft.CodeDom.Providers.DotNetCompilerPlatform, Version=2.0.1.0,
Culture=neutral, PublicKeyToken=31bf3856ad364e35"
warningLevel="4" compilerOptions="/langversion:default
/nowarn:41008 /define:_MYTYPE=\"Web\" /optionInfer+"
/>
</compilers>
</system.codedom>
</configuration>
- Open the Visual Studio IDE and then go to View > Server Explorer.
- The Server Explorer will appear to the left side panel in Visual Studio IDE.
- If you don’t have your own database connected, you can click on “Connect to Database”.
- Select your Data Source Name, which is (Microsoft SQL Server Database File (SqlClient)), then add a name to your database and click on OK.
- Now you have successfully created and connected to your own database.
- Now under the Server Explorer panel, you will be able to see your database name, then Right-Click on it.
- Click on Properties, inside the properties you will be able to see your database Connection String copy it and paste it inside the <connectionString></connectionString> tag in the (add name = " ") tag.
- That's it.
<%@ Page Language="C#"
AutoEventWireup="true" CodeBehind="Welcome.aspx.cs"
Inherits="Ex9.Welcome" %>
<!DOCTYPE html>
<html
xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form
id="form1" runat="server">
<div>
<h1>Welcome User!<asp:Label ID="Labusrname"
runat="server"></asp:Label></h1><br />
<asp:LinkButton ID="ButLogout" runat="server"
OnClick="ButLogout_Click">Logout</asp:LinkButton>
</div>
</form>
</body>
</html>
Welcome.aspx.cs -
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
namespace Ex9
{
public
partial class Welcome : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if(Session["id"]!=null)
{
Labusrname.Text = Session["id"].ToString();
}
else
{
Response.Redirect("WebForm1.aspx");
}
}
protected void ButLogout_Click(object sender, EventArgs e)
{
Session.RemoveAll();
Session.Abandon();
Response.Redirect("WebForm1.aspx");
}
}
}
**Remember:- You have to add the data (means Username and Password) in the database manually then login in the application.
Output -
Login page -
Entering the Username and Password which is in the Database(Username: manthan, Password: 123456) -
If the username and password is wrong the following error will be generated –
After clicking on the logout button the user will be logged out and redirected to the login page –
0 Comments