ASPForums.Net RSS Feedhttp://www.aspforums.net/Handlers/RSS.ashxLatest additions to the content that appears on ASPForums.Net(c) 2019 www.aspforums.com. All rights reserved.Set CheckBox checked based on database value after Login using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="KatieNgoc"> KatieNgoc</a>,</p> <p>Refer below sample.</p> <p>You need to create a table where is having user login details and checkbox check and uncheck should be stored.</p> <p><span style="text-decoration: underline;"><strong>SQL</strong></span></p> <pre class="brush: sql">CREATE TABLE tblStatusCheck(Name VARCHAR(MAX), [Status] BIT)</pre> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <p><strong>Login.aspx</strong></p> <pre class="brush: html">Name : &lt;asp:TextBox runat=&#34;server&#34; ID=&#34;txtName&#34; /&gt; &lt;br /&gt; &lt;asp:Button Text=&#34;Login&#34; runat=&#34;server&#34; OnClick=&#34;Logins&#34; /&gt;</pre> <p><strong>Default.aspx</strong></p> <pre class="brush: html">&lt;asp:CheckBox Text=&#34;Remember Me&#34; runat=&#34;server&#34; OnCheckedChanged=&#34;Save&#34; ID=&#34;chckStatus&#34; AutoPostBack=&#34;true&#34; /&gt;&lt;br /&gt; &lt;asp:Button Text=&#34;NextPage&#34; runat=&#34;server&#34; OnClick=&#34;NextPage&#34;/&gt;</pre> <p><strong>Logout.aspx</strong></p> <pre class="brush: html">&lt;asp:HyperLink NavigateUrl=&#34;~/Login.aspx&#34; runat=&#34;server&#34; Text=&#34;Logout&#34; /&gt;</pre> <p><span style="text-decoration: underline;"><strong>Namespaces</strong></span></p> <p><strong>C#</strong></p> <pre class="brush: csharp">using System.Data.SqlClient; using System.Data; using System.Configuration;</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Imports System.Data.SqlClient Imports System.Data Imports System.Configuration</pre> <p><span style="text-decoration: underline;"><strong>Code</strong></span></p> <p><strong>Login.aspx.cs</strong></p> <pre class="brush: csharp">protected void Logins(object sender, EventArgs e) { Session[&#34;Name&#34;]=txtName.Text; Response.Redirect(&#34;Default.aspx&#34;); }</pre> <p><strong>Login.aspx.vb</strong></p> <pre class="brush: vb">Protected Sub Logins(ByVal sender As Object, ByVal e As EventArgs) Session(&#34;Name&#34;) = txtName.Text Response.Redirect(&#34;Default.aspx&#34;) End Sub</pre> <p><strong>Default.aspx.cs</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; SqlConnection con = new SqlConnection(constr); SqlCommand cmd = new SqlCommand(&#34;SELECT Status FROM tblStatusCheck WHERE @Name = @Name&#34;, con); cmd.Parameters.AddWithValue(&#34;@Name&#34;, Session[&#34;Name&#34;].ToString()); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); chckStatus.Checked = Convert.ToBoolean(dt.Rows[0][&#34;Status&#34;]); } } protected void Save(object sender, EventArgs e) { bool status = chckStatus.Checked; string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; SqlConnection con = new SqlConnection(constr); SqlCommand cmd = new SqlCommand(&#34;UPDATE tblStatusCheck SET Status = @Status WHERE Name = @Name&#34;, con); cmd.Parameters.AddWithValue(&#34;@Status&#34;, status); cmd.Parameters.AddWithValue(&#34;@Name&#34;, Session[&#34;Name&#34;].ToString()); con.Open(); cmd.ExecuteNonQuery(); con.Close(); } protected void NextPage(object sender, EventArgs e) { Response.Redirect(&#34;Logout.aspx&#34;); }</pre> <p><strong>Default.aspx.vb</strong></p> <pre class="brush: vb"> Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) If Not Me.IsPostBack Then Dim constr As String = ConfigurationManager.ConnectionStrings(&#34;constr&#34;).ConnectionString Dim con As SqlConnection = New SqlConnection(constr) Dim cmd As SqlCommand = New SqlCommand(&#34;SELECT Status FROM tblStatusCheck WHERE @Name = @Name&#34;, con) cmd.Parameters.AddWithValue(&#34;@Name&#34;, Session(&#34;Name&#34;).ToString()) Dim da As SqlDataAdapter = New SqlDataAdapter(cmd) Dim dt As DataTable = New DataTable() da.Fill(dt) chckStatus.Checked = Convert.ToBoolean(dt.Rows(0)(&#34;Status&#34;)) 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(&#34;constr&#34;).ConnectionString Dim con As SqlConnection = New SqlConnection(constr) Dim cmd As SqlCommand = New SqlCommand(&#34;UPDATE tblStatusCheck SET Status = @Status WHERE Name = @Name&#34;, con) cmd.Parameters.AddWithValue(&#34;@Status&#34;, status) cmd.Parameters.AddWithValue(&#34;@Name&#34;, Session(&#34;Name&#34;).ToString()) con.Open() cmd.ExecuteNonQuery() con.Close() End Sub Protected Sub NextPage(ByVal sender As Object, ByVal e As EventArgs) Response.Redirect(&#34;Logout.aspx&#34;) End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><img src="https://i.imgur.com/k4DFppm.gif" alt="" width="250" height="227" /></p>https://www.aspforums.net:443/Threads/122807/Set-CheckBox-checked-based-on-database-value-after-Login-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/122807/Set-CheckBox-checked-based-on-database-value-after-Login-using-C-and-VBNet-in-ASPNet/Fri, 26 Jul 2019 05:04:44 GMT