Set CheckBox checked based on database value after Login using C# and VB.Net in ASP.Net

Last Reply 4 months ago By pandeyism

Posted 4 months ago

How to display An agree text of a checkbox with a check mark on it after logout and log in again?

 have a web page document agreement to apply for job online. At the bottom of its page, it has a check box and a text  [I agree].  I do not use e-signature.  After I click on the check box, it has a check mark on it.  Then, I click on next button to go to the other page.  Then, logout.

after next visit (login again), the check mark is not displayed.  How this check mark on its check box object still display? So I don't click on it again for the next visit.

Posted 4 months ago

Hi KatieNgoc,

Refer below sample.

You need to create a table where is having user login details and checkbox check and uncheck should be stored.

SQL

CREATE TABLE tblStatusCheck(Name VARCHAR(MAX), [Status] BIT)

HTML

Login.aspx

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

Default.aspx

<asp:CheckBox Text="Remember Me" runat="server" OnCheckedChanged="Save" ID="chckStatus" AutoPostBack="true" /><br />
<asp:Button Text="NextPage" runat="server"  OnClick="NextPage"/>

Logout.aspx

<asp:HyperLink NavigateUrl="~/Login.aspx" runat="server" Text="Logout" />

Namespaces

C#

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

VB.Net

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

Code

Login.aspx.cs

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

Login.aspx.vb

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

Default.aspx.cs

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
        SqlConnection con = new SqlConnection(constr);
        SqlCommand cmd = new SqlCommand("SELECT Status FROM tblStatusCheck WHERE @Name = @Name", con);
        cmd.Parameters.AddWithValue("@Name", Session["Name"].ToString());
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        chckStatus.Checked = Convert.ToBoolean(dt.Rows[0]["Status"]);
    }
}

protected void Save(object sender, EventArgs e)
{
    bool status = chckStatus.Checked;
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand("UPDATE tblStatusCheck SET Status = @Status WHERE Name = @Name", con);
    cmd.Parameters.AddWithValue("@Status", status);
    cmd.Parameters.AddWithValue("@Name", Session["Name"].ToString());
    con.Open();
    cmd.ExecuteNonQuery();
    con.Close();
}

protected void NextPage(object sender, EventArgs e)
{
    Response.Redirect("Logout.aspx");
}

Default.aspx.vb

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
        If Not Me.IsPostBack Then
            Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
            Dim con As SqlConnection = New SqlConnection(constr)
            Dim cmd As SqlCommand = New SqlCommand("SELECT Status FROM tblStatusCheck WHERE @Name = @Name", con)
            cmd.Parameters.AddWithValue("@Name", Session("Name").ToString())
            Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
            Dim dt As DataTable = New DataTable()
            da.Fill(dt)
            chckStatus.Checked = Convert.ToBoolean(dt.Rows(0)("Status"))
        End If
    End Sub

    Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
        Dim status As Boolean = chckStatus.Checked
        Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
        Dim con As SqlConnection = New SqlConnection(constr)
        Dim cmd As SqlCommand = New SqlCommand("UPDATE tblStatusCheck SET Status = @Status WHERE Name = @Name", con)
        cmd.Parameters.AddWithValue("@Status", status)
        cmd.Parameters.AddWithValue("@Name", Session("Name").ToString())
        con.Open()
        cmd.ExecuteNonQuery()
        con.Close()
    End Sub

    Protected Sub NextPage(ByVal sender As Object, ByVal e As EventArgs)
        Response.Redirect("Logout.aspx")
    End Sub

Screenshot