Hide Button after second Login using C# and VB.Net in ASP.Net

Last Reply 12 days ago By pandeyism

Posted 12 days ago

I have 2 web page:

1 login and 1 shows text and button, textbox web control. 

After login, it shows session name and text, button, text box.

How this text box and button web control disappear after 2 login?

just show text only.

I'm using ASP.Net and C#

Posted 12 days ago Modified on 12 days ago

Hi KatieNgoc,

Create a field in table as count which will keep tracking of updated count of user login. Refer below code.

HTML

CS.aspx

Name : <asp:TextBox runat="server" ID="txtName" />
<asp:Button Text="Login" runat="server" OnClick="Login" />

Default.aspx

<asp:Label ID="lblName" runat="server" />
<asp:TextBox runat="server" ID="txtData" />
<asp:Button Text="Print" runat="server" ID="btnPrint" /><br />
<asp:LinkButton ID="LinkButton1" Text="Logout" runat="server" PostBackUrl="~/CS.aspx" />

Namespaces

C#

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

VB.Net

Imports System.Data
Imports System.Configuration
Imports System.Data.SqlClient

Code

CS.aspx.cs

protected void Login(object sender, EventArgs e)
{
    Session["Name"] = txtName.Text;
    Response.Redirect("Default.aspx");
}

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        int count = 1;
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        SqlConnection con = new SqlConnection(constr);
        SqlCommand cmd = new SqlCommand("SELECT tblData WHERE Name = @Name");
        cmd.Parameters.AddWithValue("@Name", Session["Name"].ToString());
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        if (dt.Rows.Count > 0)
        {
            cmd = new SqlCommand("UPDATE tblData SET Count = @Count WHERE Name = @Name");
            cmd.Parameters.AddWithValue("@Count", count + 1);
            cmd.Parameters.AddWithValue("@Name", Session["Name"].ToString());
            con.Open();
            cmd.ExecuteNonQuery();
            cmd = new SqlCommand("SELECT Count FROM tblData WHERE Name = @Name");
            cmd.Parameters.AddWithValue("@Count", Session["Name"].ToString());
            int updatedCount = Convert.ToInt16(cmd.ExecuteScalar());
            con.Close();
            if (updatedCount > 2)
            {
                txtData.Visible = false;
                btnPrint.Visible = false;
            }
        }
        lblName.Text = Session["Name"].ToString();
    }
}

CS.aspx.vb

Protected Sub Login(ByVal sender As Object, ByVal e As EventArgs)
    Session("Name") = txtName.Text
    Response.Redirect("Default.aspx")
End Sub

Default.aspx.vb

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    If Not Me.IsPostBack Then
        Dim count As Integer = 1
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Dim con As SqlConnection = New SqlConnection(constr)
        Dim cmd As SqlCommand = New SqlCommand("SELECT tblData WHERE Name = @Name")
        cmd.Parameters.AddWithValue("@Name", Session("Name").ToString())
        Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
        Dim dt As DataTable = New DataTable()
        da.Fill(dt)

        If dt.Rows.Count > 0 Then
            cmd = New SqlCommand("UPDATE tblData SET Count = @Count WHERE Name = @Name")
            cmd.Parameters.AddWithValue("@Count", count + 1)
            cmd.Parameters.AddWithValue("@Name", Session("Name").ToString())
            con.Open()
            cmd.ExecuteNonQuery()
            cmd = New SqlCommand("SELECT Count FROM tblData WHERE Name = @Name")
            cmd.Parameters.AddWithValue("@Count", Session("Name").ToString())
            Dim updatedCount As Integer = Convert.ToInt16(cmd.ExecuteScalar())
            con.Close()

            If updatedCount > 2 Then
                txtData.Visible = False
                btnPrint.Visible = False
            End If
        End If

        lblName.Text = Session("Name").ToString()
    End If
End Sub