Pass (Send) value to another page using Session in ASP.Net using C# and VB.Net

akhter
 
on Nov 24, 2021 07:33 AM
Sample_123057.zip
424 Views

I want pass A_ID on another page after login below is my code.

protected void btn_login_Click(object sender, EventArgs e)
{
    con.Open();
    str = "Select * from tbl_Admin";
    com = new SqlCommand(str);
    sqlda = new SqlDataAdapter(com.CommandText, con);
    dt = new DataTable();
    sqlda.Fill(dt);       
    RowCount = dt.Rows.Count;
    for (int i = 0; i < RowCount; i++)
    {
        UserName = dt.Rows[i]["A_Name"].ToString();
        Password = dt.Rows[i]["A_Password"].ToString();
        a_id = dt.Rows[i]["A_ID"].ToString();
        Role = dt.Rows[i]["Role"].ToString();
        if (UserName == TextBox_user_name.Text && Password == TextBox_password.Text)
        {
            Session["A_Name"] = UserName;
            Session["A_ID"] = a_id;
            Session["Role"] = Role;
            if (dt.Rows[i]["Role"].ToString() == "Master")
 
                Response.Redirect("BBDispatch.aspx");
 
            else if (dt.Rows[i]["Role"].ToString() == "Input")
 
                Response.Redirect("~/BBinput.aspx");
 
            else if (dt.Rows[i]["Role"].ToString() == "Dispatch")
             
            Response.Redirect("BBDispatch.aspx");
            else if (dt.Rows[i]["Role"].ToString() == "Administrator")
 
                Response.Redirect("DashBoard.aspx");
 
        }
        else
        {
            //  error.Style.Add("display", "block");
        }            
    }
}

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
arjunv
 
on Nov 25, 2021 06:43 AM

Hi Akhter,

 Please refer below Sample and Use that master page's HTML & Code to your other page also.

HTML

Default

<table>
    <tr>
        <td>UserName :</td>
        <td><asp:TextBox runat="server" ID="txtUserName" /></td>
    </tr>
    <tr>
        <td>Password :</td>
        <td><asp:TextBox runat="server" ID="txtPassword" /></td>
    </tr>            
    <tr>
        <td><asp:Button ID="btnSend" Text="Send" runat="server" OnClick="Send" /></td>
    </tr>
</table>

Master

<table>
    <tr>
        <td>UserId :</td>
        <td><asp:Label runat="server" ID="lblUserId" /></td>
    </tr>
</table>

Namespaces

C#

using System.Data.SqlClient;
using System.Configuration;

VB.Net

Imports System.Data.SqlClient
Imports System.Configuration

Code

C#

Default

protected void Send(object sender, EventArgs e)
{
    string conString = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    using (SqlConnection con = new SqlConnection(conString))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT UserId, Username, Password, RoleId From Users WHERE UserName = @UserName and Password = @Password", con))
        {
            cmd.Parameters.AddWithValue("@UserName", txtUserName.Text);
            cmd.Parameters.AddWithValue("@Password", txtPassword.Text);
            con.Open();
            SqlDataReader sdr = cmd.ExecuteReader();
            if (sdr.Read())
            {
                Session["UserName"] = sdr["Username"];
                Session["UserId"] = sdr["UserId"];
                Session["Password"] = sdr["Password"];
                Session["RoleId"] = sdr["RoleId"];
            }
            con.Close();
        }
    }

    if (Session["RoleId"].ToString() == "1")
    {
        Response.Redirect("Master.aspx");
    }
    else if (Session["RoleId"].ToString() == "2")
    {
        Response.Redirect("Input.aspx");
    }
    else if (Session["RoleId"].ToString() == "3")
    {
        Response.Redirect("Dispatch.aspx");
    }
}

Master

protected void Page_Load(object sender, EventArgs e)
{
    lblUserId.Text = Session["UserId"].ToString();
}

VB.Net

Default

Protected Sub Send(ByVal sender As Object, ByVal e As EventArgs)
    Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
    Using con As SqlConnection = New SqlConnection(conString)
        Using cmd As SqlCommand = New SqlCommand("SELECT UserId, Username, Password, RoleId From Users WHERE UserName = @UserName and Password = @Password", con)
            cmd.Parameters.AddWithValue("@UserName", txtUserName.Text)
            cmd.Parameters.AddWithValue("@Password", txtPassword.Text)
            con.Open()
            Dim sdr As SqlDataReader = cmd.ExecuteReader()

            If sdr.Read() Then
                Session("UserName") = sdr("Username")
                Session("UserId") = sdr("UserId")
                Session("Password") = sdr("Password")
                Session("RoleId") = sdr("RoleId")
            End If

            con.Close()
        End Using
    End Using
    If Session("RoleId").ToString() = "1" Then
        Response.Redirect("MasterVB.aspx")
    ElseIf Session("RoleId").ToString() = "2" Then
        Response.Redirect("InputVB.aspx")
    ElseIf Session("RoleId").ToString() = "3" Then
        Response.Redirect("DispatchVB.aspx")
    End If
End Sub

Master

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    lblUserId.Text = Session("UserId").ToString()
End Sub

Screenshot