Save (Insert) 1 or 0 instead of Yes or No using C# and VB.Net in ASP.Net

Last Reply 20 days ago By pandeyism

Posted 21 days ago

Dear Sir ,

how to save yes or no as 1 or 0 format using asp c# but user see Yes or no

i am unable to complate mention query. please help me.

Regards,

indradeo

Posted 20 days ago Modified on 20 days ago

Hi indradeo,

Refer below sample.

SQL

CREATE TABLE tblStatusCheck([Status] BIT)

HTML

<asp:CheckBox ID="chkStatus" Text="Status" runat="server" AutoPostBack="true" OnCheckedChanged="Save" />
<br />
<asp:GridView ID="gvStatus" runat="server" AutoGenerateColumns="false" OnRowDataBound="gvStatus_RowDataBound">
    <Columns>
        <asp:BoundField DataField="Status" HeaderText="Status" />
    </Columns>
</asp:GridView>

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

C#

protected void Page_Load(object sender, EventArgs e)
{
    if (!this.IsPostBack)
    {
        BindGrid();
    }
}

private void BindGrid()
{
    string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand("SELECT * FROM tblStatusCheck", 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["constr"].ConnectionString;
    SqlConnection con = new SqlConnection(constr);
    SqlCommand cmd = new SqlCommand("INSERT INTO tblStatusCheck VALUES(@Status)", con);
    cmd.Parameters.AddWithValue("@Status", 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 = "Yes";
        }
        else
        {
            e.Row.Cells[0].Text = "No";
        }
    }
}

VB.Net

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("constr").ConnectionString
        Dim con As SqlConnection = New SqlConnection(constr)
        Dim cmd As SqlCommand = New SqlCommand("SELECT * FROM tblStatusCheck", 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("constr").ConnectionString
        Dim con As SqlConnection = New SqlConnection(constr)
        Dim cmd As SqlCommand = New SqlCommand("INSERT INTO tblStatusCheck VALUES(@Status)", con)
        cmd.Parameters.AddWithValue("@Status", 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 = "Yes"
            Else
                e.Row.Cells(0).Text = "No"
            End If
        End If
    End Sub

Screenshot

Database

Status
1
0
1
0

Gridview