Hi vereato,
Check this example. Now please take its reference and correct your code.
HTML
Login
<table>
<tr>
<td>Name</td>
<td><asp:TextBox runat="server" ID="txtUserName" /></td>
</tr>
<tr>
<td>Password</td>
<td><asp:TextBox runat="server" ID="txtPassword" TextMode="Password" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button Text="Log In" runat="server" OnClick="OnLogin" />
</td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Label ID="lblMessage" runat="server" Visible="false" />
</td>
</tr>
</table>
Home
<asp:Label ID="lblUsername" runat="server" />
Namespaces
C#
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
VB.Net
Imports System.Configuration
Imports System.Data
Imports System.Data.SqlClient
Imports System.Drawing
Code
C#
Login
protected void OnLogin(object sender, EventArgs e)
{
string ConnStr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(ConnStr))
{
string str = "SELECT COUNT(*) FROM Users WHERE Username = @Username AND Password = @Password";
SqlCommand cmd = new SqlCommand(str, con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@Username", txtUserName.Text);
cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
object obj;
con.Open();
obj = cmd.ExecuteScalar();
con.Close();
if (Convert.ToInt32(obj) > 0)
{
Session["Username"] = txtUserName.Text;
Response.Redirect("~/HomeCS.aspx");
}
else
{
lblMessage.Visible = true;
lblMessage.Text = "Invalid UserName or Password";
lblMessage.ForeColor = Color.Red;
}
}
}
Home
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Username"] != null)
{
Load_Username();
}
}
private void Load_Username()
{
string ConnStr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(ConnStr))
{
SqlCommand cmd = new SqlCommand("SELECT Username FROM Users WHERE username = @Username", con);
cmd.Parameters.AddWithValue("@Username", Session["Username"].ToString());
con.Open();
string username = Convert.ToString(cmd.ExecuteScalar());
con.Close();
lblUsername.Text = "Welcome " + username;
}
}
VB.Net
Login
Protected Sub OnLogin(ByVal sender As Object, ByVal e As EventArgs)
Dim ConnStr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(ConnStr)
Dim str As String = "SELECT COUNT(*) FROM Users WHERE Username = @Username AND Password = @Password"
Dim cmd As SqlCommand = New SqlCommand(str, con)
cmd.CommandType = CommandType.Text
cmd.Parameters.AddWithValue("@Username", txtUserName.Text)
cmd.Parameters.AddWithValue("@Password", txtPassword.Text)
Dim obj As Object
con.Open()
obj = cmd.ExecuteScalar()
con.Close()
If Convert.ToInt32(obj) > 0 Then
Session("Username") = txtUserName.Text
Response.Redirect("~/HomeVB.aspx")
Else
lblMessage.Visible = True
lblMessage.Text = "Invalid UserName or Password"
lblMessage.ForeColor = Color.Red
End If
End Using
End Sub
Home
Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
If Session("Username") IsNot Nothing Then
Load_Username()
End If
End Sub
Private Sub Load_Username()
Dim ConnStr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(ConnStr)
Dim cmd As SqlCommand = New SqlCommand("SELECT Username FROM Users WHERE username = @Username", con)
cmd.Parameters.AddWithValue("@Username", Session("Username").ToString())
con.Open()
Dim username As String = Convert.ToString(cmd.ExecuteScalar())
con.Close()
lblUsername.Text = "Welcome " & username
End Using
End Sub
Screenshot