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.Save (Insert) 1 or 0 instead of Yes or No using C# and VB.Net in ASP.Net<p>Hi <a class="username" rel="indradeo"> indradeo</a>,</p> <p>Refer below sample.</p> <p><span style="text-decoration: underline;"><strong>SQL</strong></span> </p> <pre class="brush: sql">CREATE TABLE tblStatusCheck([Status] BIT)</pre> <p><span style="text-decoration: underline;"><strong>HTML</strong></span></p> <pre class="brush: html">&lt;asp:CheckBox ID=&#34;chkStatus&#34; Text=&#34;Status&#34; runat=&#34;server&#34; AutoPostBack=&#34;true&#34; OnCheckedChanged=&#34;Save&#34; /&gt; &lt;br /&gt; &lt;asp:GridView ID=&#34;gvStatus&#34; runat=&#34;server&#34; AutoGenerateColumns=&#34;false&#34; OnRowDataBound=&#34;gvStatus_RowDataBound&#34;&gt; &lt;Columns&gt; &lt;asp:BoundField DataField=&#34;Status&#34; HeaderText=&#34;Status&#34; /&gt; &lt;/Columns&gt; &lt;/asp:GridView&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>C#</strong></p> <pre class="brush: csharp">protected void Page_Load(object sender, EventArgs e) { if (!this.IsPostBack) { BindGrid(); } } private void BindGrid() { string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; SqlConnection con = new SqlConnection(constr); SqlCommand cmd = new SqlCommand(&#34;SELECT * FROM tblStatusCheck&#34;, con); SqlDataAdapter da = new SqlDataAdapter(cmd); DataTable dt = new DataTable(); da.Fill(dt); gvStatus.DataSource = dt; gvStatus.DataBind(); } protected void Save(object sender, EventArgs e) { bool status = chkStatus.Checked; string constr = ConfigurationManager.ConnectionStrings[&#34;constr&#34;].ConnectionString; SqlConnection con = new SqlConnection(constr); SqlCommand cmd = new SqlCommand(&#34;INSERT INTO tblStatusCheck VALUES(@Status)&#34;, con); cmd.Parameters.AddWithValue(&#34;@Status&#34;, status); con.Open(); cmd.ExecuteNonQuery(); con.Close(); BindGrid(); } protected void gvStatus_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (Convert.ToBoolean(e.Row.Cells[0].Text) == true) { e.Row.Cells[0].Text = &#34;Yes&#34;; } else { e.Row.Cells[0].Text = &#34;No&#34;; } } }</pre> <p><strong>VB.Net</strong></p> <pre class="brush: vb">Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)Handle Me.Load If Not Me.IsPostBack Then BindGrid() End If End Sub Private Sub BindGrid() 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 * FROM tblStatusCheck&#34;, con) Dim da As SqlDataAdapter = New SqlDataAdapter(cmd) Dim dt As DataTable = New DataTable() da.Fill(dt) gvStatus.DataSource = dt gvStatus.DataBind() End Sub Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs) Dim status As Boolean = chkStatus.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;INSERT INTO tblStatusCheck VALUES(@Status)&#34;, con) cmd.Parameters.AddWithValue(&#34;@Status&#34;, status) con.Open() cmd.ExecuteNonQuery() con.Close() BindGrid() End Sub Protected Sub gvStatus_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) If e.Row.RowType = DataControlRowType.DataRow Then If Convert.ToBoolean(e.Row.Cells(0).Text) = True Then e.Row.Cells(0).Text = &#34;Yes&#34; Else e.Row.Cells(0).Text = &#34;No&#34; End If End If End Sub</pre> <p><span style="text-decoration: underline;"><strong>Screenshot</strong></span></p> <p><strong>Database</strong></p> <table border="1"> <tbody> <tr style="height: 15.0pt;"> <td style="height: 15.0pt; width: 48pt;" width="64" height="20"><strong>Status</strong></td> </tr> <tr style="height: 15.0pt;"> <td style="height: 15.0pt;" align="right" height="20">1</td> </tr> <tr style="height: 15.0pt;"> <td style="height: 15.0pt;" align="right" height="20">0</td> </tr> <tr style="height: 15.0pt;"> <td style="height: 15.0pt;" align="right" height="20">1</td> </tr> <tr style="height: 15.0pt;"> <td style="height: 15.0pt;" align="right" height="20">0</td> </tr> </tbody> </table> <p><strong>Gridview</strong></p> <p><strong><img src="https://i.imgur.com/F2w4on4.gif" alt="" width="249" height="303" /></strong> </p>https://www.aspforums.net:443/Threads/808134/Save-Insert-1-or-0-instead-of-Yes-or-No-using-C-and-VBNet-in-ASPNet/https://www.aspforums.net:443/Threads/808134/Save-Insert-1-or-0-instead-of-Yes-or-No-using-C-and-VBNet-in-ASPNet/Mon, 29 Jul 2019 03:01:33 GMT