Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6134805
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T17:24:05+00:00 2026-05-23T17:24:05+00:00

I’m trying to write to a database whenever a checkbox is checked or unchecked.

  • 0

I’m trying to write to a database whenever a checkbox is checked or unchecked. We want to be able to store a 1 (bit value) if the checkbox is checked, and a 0 if it is not. This way, if it has been checked, when the form loads up, we will see a check in the checkbox. Otherwise, there won’t be one. Here is what I have so far:

C#
    public partial class vieworders : System.Web.UI.Page
{
    private string orderByString;
    private string fieldString;
    private string address;
    private DataGrid dataGrid = new DataGrid();
    SqlDataAdapter dataAdapter;
    DataSet dataSet;


    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            orderByString = orderByList.SelectedItem.Value;
            fieldString = searchTextBox.Text;
            string sqlStatement = "SELECT fName,lName,zip,email,cwaSource,price,length FROM SecureOrders WHERE fName LIKE '%" + fieldString + "%' OR lName LIKE'%" + fieldString + "%'  OR zip LIKE'%" + fieldString + "%' OR zip LIKE'%" + fieldString + "%'  OR email LIKE'%" + fieldString + "%' OR cwaSource LIKE'%" + fieldString + "%' OR length LIKE'%" + fieldString + "%' OR price LIKE'%" + fieldString + "%' ORDER BY " + orderByString;
            ////////////////////////////




            connectionString = rootWebConfig.ConnectionStrings.ConnectionStrings["secureodb"];

            //TEST
            for (int i = 0; i < DefaultGrid.Rows.Count; i++)
            {
                CheckBox chkUpdate = (CheckBox)DefaultGrid.Rows[i].Cells[1].FindControl("CheckBoxProcess");
                if (chkUpdate != null)
                {
                    OrderBrowser.Text += "Test";
                }
            }



            // Create an SqlConnection to the database.
            // Create an SqlConnection to the database.
            using (SqlConnection connection = new SqlConnection(connectionString.ToString()))
            {
                connection.Open();

                dataAdapter = new SqlDataAdapter("SELECT * FROM SecureOrders", connection);

                // create the DataSet
                dataSet = new DataSet();
                // fill the DataSet using our DataAdapter               
                dataAdapter.Fill(dataSet, "SecureOrders");

                DataView source = new DataView(dataSet.Tables[0]);
                DefaultGrid.DataSource = source;
                DefaultGrid.DataBind();
                connection.Close();
            }
        }


         }




    protected void DefaultGrid_SelectedIndexChanged(Object sender, EventArgs e)
    {
        GridViewRow row = DefaultGrid.SelectedRow;
        string name = "Name: " + row.Cells[2].Text + " " + row.Cells[3].Text + "\r\n";
       // if (row.Cells[4].Text == "&nbsp;")
        //{
            //address = "Address: " + row.Cells[3].Text + "\r\n         " + row.Cells[5].Text + ", " + row.Cells[6].Text + " " + row.Cells[7].Text + " " + row.Cells[8].Text + "\r\n";

       // }
        //else
       // {
           // address = "Address: " + row.Cells[3].Text + "\r\n         " + row.Cells[4].Text + "\r\n         " + row.Cells[5].Text + ", " + row.Cells[6].Text + " " + row.Cells[7].Text + " " + row.Cells[8].Text + "\r\n";
        //}

        string zip = "Zip: " + row.Cells[4].Text + "\r\n";
        string email = "Email: " + row.Cells[5].Text + "\r\n";
        //string phone = "Phone: " + row.Cells[10].Text + "\r\n";
        //string cctype = "Credit Card Type: " + row.Cells[11].Text + "\r\n";
        //string ccnum = "Credit Card Number: " + row.Cells[12].Text + "\r\n";
        //string ccexp = "Credit Card Expiration: " + row.Cells[13].Text + "\r\n";
        string length = "Length: " + row.Cells[8].Text + "\r\n";
        //string delivery = "Delivery: " + row.Cells[15].Text + "\r\n";
        string price = "Price: " + row.Cells[7].Text + "\r\n";
        string source = "Source: " + row.Cells[6].Text + "\r\n";
        //string joined = "Joined: " + row.Cells[18].Text + "\r\n";
        //string url = "URL: " + row.Cells[19].Text + "\r\n";

        OrderBrowser.Text = name + email + length + price + source;
    }

    protected void CheckBoxProcess_CheckedChanged(object sender, EventArgs e)
    {
        CheckBox cb = (CheckBox)sender;
        GridViewRow gr = (GridViewRow)cb.NamingContainer;




        connectionString = rootWebConfig.ConnectionStrings.ConnectionStrings["secureodb"];

        using (SqlConnection connection = new SqlConnection(connectionString.ToString()))
        {
            connection.Open();


            dataAdapter = new SqlDataAdapter("SELECT * FROM SecureOrders", connection);

            // create the DataSet
            dataSet = new DataSet();
            // fill the DataSet using our DataAdapter               
            dataAdapter.Fill(dataSet, "SecureOrders");
            DataSet myDSchanged = dataSet.GetChanges(DataRowState.Modified);
            SqlCommandBuilder builder = new SqlCommandBuilder(dataAdapter);

            dataAdapter.Update(myDSchanged, "SecureOrders");
            dataSet.AcceptChanges();
            connection.Close();
        }




    }

    }

aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="vieworders.aspx.cs" Inherits="Cabot3.custserv.vieworders" %>

        <asp:DropDownList runat="server" ID="orderByList" AutoPostBack="true">
            <asp:ListItem Value="fName" Selected="True">First Name</asp:ListItem>
            <asp:ListItem Value="lName">Last Name</asp:ListItem>
            <asp:ListItem Value="state">State</asp:ListItem>
            <asp:ListItem Value="zip">Zip Code</asp:ListItem>
            <asp:ListItem Value="cwaSource">Source</asp:ListItem>
            <asp:ListItem Value="cwaJoined">Date Joined</asp:ListItem>
        </asp:DropDownList>
    </div>
    <div>
        <asp:Label runat="server" ID="searchLabel" Text="Search For: " />
        <asp:TextBox ID="searchTextBox" runat="server" Columns="30" />
        <asp:Button ID="searchButton" runat="server" Text="Search" />
    </div>
<div>
<asp:UpdatePanel ID = "up" runat="server">



    <ContentTemplate>
    <div style= "overflow:auto; height:150px; width:700px">
    <asp:GridView ID="DefaultGrid" runat = "server"  DataKeyNames = "fName, lName, zip"
    onselectedindexchanged = "DefaultGrid_SelectedIndexChanged"
    autogenerateselectbutton = "true"
    selectedindex="0">
    <SelectedRowStyle BackColor="Azure"
    forecolor="Black"
    font-bold="true" />
    <Columns>
    <asp:TemplateField HeaderText="Processed">
                <ItemTemplate>
                    <asp:CheckBox ID="CheckBoxProcess" AutoPostBack = "true" Checked ='<%#Eval("processed") %>' OnCheckedChanged="CheckBoxProcess_CheckedChanged"  runat="server" Enabled="true" />
                </ItemTemplate>
            </asp:TemplateField>
    </Columns>
    </asp:GridView>
    </div>
    </div>
    <asp:TextBox ID="OrderBrowser" columns="70" Rows="14" runat="server" Wrap="false" TextMode="MultiLine" ReadOnly = "true"/>
    </ContentTemplate>
    </asp:UpdatePanel>



</div>
</form>

Can anybody steer me in the right direction?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T17:24:07+00:00Added an answer on May 23, 2026 at 5:24 pm

    This is a static method I wrote as a helper method for you. It takes a boolean value, and sets a 0 or 1 bit value in a column. You would of course need to modify this with your table name, column names, etc. I just created a simple test database to show you how to do this.

    Now, this assumes that the row already exists in the database. In my test database my table is named ‘Settings’, and has three columns:

    • ID (int)
    • Name (nvarchar(50))
    • Value (nvarchar(50))

    A row already exists with an ID of 0, name of ‘CheckboxState’ and a Value of 0. So initially you should have some sort of mechanism that places the row in the database in the first place.

    But, essentially, you could just call this in the code-behind, and pass the CheckBox.Checked property into it.

    static void UpdateCheckedState(bool state) {
        string connectionstring = "<yourconnectionstring>";
        using (SqlConnection connection = new SqlConnection(connectionstring)) {
            try {
                connection.Open();
            }
            catch (System.Data.SqlClient.SqlException ex) {
                // Handle exception
            }
            string updateCommandText = "UPDATE Settings SET Value = @state WHERE Name = 'CheckboxState'";
            using (SqlCommand updateCommand = new SqlCommand(updateCommandText, connection)) {
                SqlParameter stateParameter = new SqlParameter("state", state);
                updateCommand.Parameters.Add(stateParameter);
                try {
                    updateCommand.ExecuteNonQuery();
                }
                catch (System.Data.SqlClient.SqlException ex) {
                    // Handle exception
                }
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I want to count how many characters a certain string has in PHP, but
For some reason, after submitting a string like this Jack’s Spindle from a text
this is what i have right now Drawing an RSS feed into the php,
I have a French site that I want to parse, but am running into
I want use html5's new tag to play a wav file (currently only supported

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.