Show hide Label based on user status using C# in ASP.Net

Last Reply 7 months ago By Indresh

Posted 7 months ago

if user account in database is true how do i show lable on page member

 

  public DataTable GetProfile(string username)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DB_A000E6_FIRSTAPP001"].ConnectionString);
    SqlDataAdapter adp = new SqlDataAdapter("GetUserPOSTS", conn);
    adp.SelectCommand.CommandType = CommandType.StoredProcedure;
   // adp.SelectCommand.Parameters.AddWithValue("@Email", username);
    adp.SelectCommand.Parameters.AddWithValue("@UserName", username);
    adp.SelectCommand.Parameters.AddWithValue("@Id", Id);
    DataTable dt = new DataTable();
        //
    adp.Fill(dt);
    return dt;

sql

 

CREATE TABLE [dbo].[User3](
	[UserId] [int] IDENTITY(1,1) NOT NULL,
	[UserName] [nvarchar](200) NULL,
	[Name] [nvarchar](500) NOT NULL,
	[Email] [nvarchar](500) NULL,
	[Password] [nvarchar](500) NOT NULL,
	[Profession] [nvarchar](500) NULL,
	[AccountCategory] [nvarchar](200) NULL,
	[Gender] [nvarchar](500) NULL,
	[MaritalStatus] [nvarchar](500) NULL,
	[BirthDay] [date] NULL,
	[State] [nvarchar](500) NULL,
	[Country] [nvarchar](500) NULL,
	[ImageName] [nvarchar](505) NULL,
	[ImageBanner] [nvarchar](505) NULL,
	[RegisterdDate] [date] NULL,
	[UserStatus] [bit] NULL,
	[LastLogin] [datetime] NULL,
 CONSTRAINT [PK_User3] PRIMARY KEY CLUSTERED 
(
	[UserId] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

ALTER TABLE [dbo].[User3] ADD  CONSTRAINT [DF_User3_RegisterdDate]  DEFAULT (getdate()) FOR [RegisterdDate]
GO

ALTER TABLE [dbo].[User3] ADD  CONSTRAINT [DF_User3_LastLogin]  DEFAULT (getdate()) FOR [LastLogin]
GO


 

Posted 7 months ago Modified on 7 months ago

Hi micah,

I have created sample code which fullfill your requirement.

HTML

<form id="form1" runat="server">
<div>
    <asp:TextBox ID="txtuserName" runat="server" />
    <br />
    <br />
    <asp:Button ID="btnSubmit" Text="Submit" runat="server" OnClick="Unnamed1_Click" />
    <br />
    <br />
    <asp:DataList ID="dlStatus" runat="server" OnItemDataBound="dlStatus_ItemDataBound">
        <HeaderTemplate>
            <table>
        </HeaderTemplate>
        <ItemTemplate>
            <tr>
                <td>
                    <asp:Label ID="lblStatusName" Visible="false" Text="Status:" runat="server" />
                    <asp:Label ID="lblStatus" Text='<%#Eval("Status")%>' Visible="false" runat="server" />
                </td>
            </tr>
            <tr>
                <td>
                    <asp:Image ID="Image1" Width="200px" Visible="false" Height="100px" ImageUrl="~/images/Desert.jpg"
                        runat="server" />
                </td>
            </tr>
        </ItemTemplate>
        <FooterTemplate>
            </table>
        </FooterTemplate>
    </asp:DataList>
</div>
</form>

C#

protected void Submit_Click(object sender, EventArgs e)
{
    using (SqlConnection con = new SqlConnection(Constr))
    {
        using (SqlCommand cmd = new SqlCommand("SELECT UserName, status FROM UserLogin WHERE UserName= '" + txtuserName.Text + "'", con))
        {
            DataTable dt = new DataTable();
            using (SqlDataAdapter sda = new SqlDataAdapter(cmd))
            {
                sda.Fill(dt);
                dlStatus.DataSource = dt;
                dlStatus.DataBind();
            }
        }
    }

}

protected void dlStatus_ItemDataBound(object sender, DataListItemEventArgs e)
{
    if (e.Item.ItemType == ListItemType.Item || e.Item.ItemType == ListItemType.AlternatingItem)
    {
        if ((e.Item.FindControl("lblStatus") as Label).Text.ToUpper() == "TRUE")
        {
            (e.Item.FindControl("Image1") as Image).Visible = true;
            (e.Item.FindControl("lblStatus") as Label).Visible = true;
            (e.Item.FindControl("lblStatusName") as Label).Visible = true;
        }

    }
}

 Screenshot

 

I agree, here is the link: https://www.e-iceblue.com/Introduce/spire-office-for-net-free.html?aff_id=108