0

I have 3 checkbox, id which is chkIPP, chkOutReach and chkCCA. When user check one of those checkbox, the value will be inserted into the BlogType column field. The 3 values that I want to be inserted are IPP Stories, OutReach Activities and CCA. I only want to allow one checkbox to be checked only. Only one value will be inserted into the blogType column field.

I think that the approximate control should be radio button or dropdownlist since only one can be selected and inserted into the database, but my teacher told me to use checkbox. So user will type in the 3 textbox, and check one of the checkboxes and the values will be inserted into the database. I already coded the 3 textboxes. Left with the checkboxes. Thanks

enter image description here

enter image description here

following @serhads suggestion. Did I did it correct? enter image description here

protected void btnSubmit_Click(object sender, EventArgs e)
{
    string AdminNumber = Convert.ToString(txtAdmin.Text);
    string Name = Convert.ToString(txtName.Text);
    string BlogStory = Convert.ToString(txtStory.Text);

    insertGameRecord(AdminNumber, Name, BlogStory);
}

private void insertGameRecord(string admin, string name, string story)
{
    try
    {
        string strConnectionString = ConfigurationManager.ConnectionStrings["BlogConnectionString"].ConnectionString;
        SqlConnection myConnect = new SqlConnection(strConnectionString);

        string strCommandText = "INSERT EntryTable(AdminNumber, Name, BlogStory, DateEntry) Values(@AdminNumber, @Name, @BlogStory, @DateEntry)";

        SqlCommand cmd = new SqlCommand(strCommandText, myConnect);

        cmd.Parameters.AddWithValue("@AdminNumber", admin);
        cmd.Parameters.AddWithValue("@Name", name);
        cmd.Parameters.AddWithValue("@BlogStory", story);
        cmd.Parameters.Add("DateEntry", SqlDbType.DateTime);
        cmd.Parameters["DateEntry"].Value = DateTime.Now;

        myConnect.Open();

        int result = cmd.ExecuteNonQuery();

        if (result > 0)
        { lblError.Text = "Record Updated"; 
         bindResultGridView();
        }

        else { lblError.Text = "Update fail"; }

        myConnect.Close();
    }
    catch(Exception)
    {
        lblError.Text = "Please enter correct data";
    }


}
1
  • check my answer. Hope it will help you. Don't forget to upvote and mark it as an answer. Commented Mar 25, 2014 at 10:20

3 Answers 3

1

There are two ways to do it :

1. Using JQuery:

On click event of the each checkbox, disable all three checkboxes using JQuery as mentioned in below code sample :

HTML :

<asp:CheckBox runat="server" ID="chkIPP" CssClass="check" />
<asp:CheckBox runat="server" ID="chkOutReach" CssClass="check" />
<asp:CheckBox runat="server" ID="chkCCA" CssClass="check" />
<asp:Button runat="server" ID="buttonSubmit" Text="Submit" OnClick="buttonTest_OnClick" />

JavaScript :

$('.check').click(function () {
    $('.check').each(function () {
        $(this).prop('disabled', 'disabled');
    });
});

2. Using CheckeboxList with OnSelectedIndexChanged event :

HTML :

<asp:CheckBoxList runat="server" ID="cblType" OnSelectedIndexChanged="cblType_OnSelectedIndexChanged" AutoPostBack="True">
    <Items>
        <asp:ListItem Text="IPP Stories" Value="IPP Stories"></asp:ListItem>
        <asp:ListItem Text="OutReach Activities " Value="OutReach Activities"></asp:ListItem>
        <asp:ListItem Text="CCA" Value="CCA"></asp:ListItem>
    </Items>
</asp:CheckBoxList>

<asp:Button runat="server" ID="buttonSubmit" Text="Submit" OnClick="buttonTest_OnClick" />

Code Behind:

protected void cblType_OnSelectedIndexChanged(object sender, EventArgs e)
{
    this.cblType.Enabled = false;
}

Button Submit Click Event:

protected void btnSubmit_Click(object sender, EventArgs e)
{
    string AdminNumber = Convert.ToString(txtAdmin.Text);
    string Name = Convert.ToString(txtName.Text);
    string BlogStory = Convert.ToString(txtStory.Text);
    string BlogType = cblType.SelectedValue;
    insertGameRecord(AdminNumber, Name, BlogStory, BlogType);
}
Sign up to request clarification or add additional context in comments.

4 Comments

But how to write the query? like to specify the column field BlogType @SpiderCode
If you are not aware about jQuery, you can use my second option using OnCheckedChanged.
Guess I will use the second one. But I thought need to write the code so that it will know wat column field to go into the database @SpiderCode
Have a look at my Updated answer for second option.
1

Do you have another table for BlogType? if yes, I would suggest you to create another table between BlogType table and EntryTable. Than you can store selected checkbox values in this table. Table can be like this.

ID << primary key
BlogID << EntryTable referance id
BlogTypeID << BlogType table referance id

with this way you can insert a new record for each selected checkbox value by BlogId.

enter image description here

6 Comments

aw no, I only have 1 table. @serhads
I only want to allow one checkbox to be checked only. I edited my post above already. @serhads
in this case using checkbox is not logical. you can use radiobox or dropdown. and ask your teacher why he/she push you to use checkbox?
He told me in future the project, might need to check multiple values and inserted into database. So he told me to use checkbox for now, which only one value to be submitted to database. In future then change to multiple checkbox @serhads
ok. so you should create another table and put your BlogType values into this table and make a relation with other tables. Otherwise you can not add multiple values in the future programmatically. without touching a code in the future, you can add only new BlogType item into BlogType table.
|
0
  1. you need to replace checkbox with radio button or drop downlist to select any one option..

I assume that you created dropdown list named "drpBlogType"

 protected void btnSubmit_Click(object sender, EventArgs e)

{
    string AdminNumber = Convert.ToString(txtAdmin.Text);
    string Name = Convert.ToString(txtName.Text);
    string BlogStory = Convert.ToString(txtStory.Text);
    // get value or dropdown
    string BlogType = drpBlogType.SelectedValue;


        insertGameRecord(AdminNumber, Name, BlogStory,BlogType);
    }

    private void insertGameRecord(string admin, string name, string story,string BlogType)
    {
        try
        {
            string strConnectionString = ConfigurationManager.ConnectionStrings["BlogConnectionString"].ConnectionString;
            SqlConnection myConnect = new SqlConnection(strConnectionString);

            string strCommandText = "INSERT EntryTable(AdminNumber, Name, BlogStory, DateEntry,BlogType) Values(@AdminNumber, @Name, @BlogStory, @DateEntry,@BlogType)";

            SqlCommand cmd = new SqlCommand(strCommandText, myConnect);

            cmd.Parameters.AddWithValue("@AdminNumber", admin);
            cmd.Parameters.AddWithValue("@Name", name);
            cmd.Parameters.AddWithValue("@BlogStory", story);
            cmd.Parameters.AddWithValue("@BlogType", BlogType);

            cmd.Parameters.Add("DateEntry", SqlDbType.DateTime);
            cmd.Parameters["DateEntry"].Value = DateTime.Now;

            myConnect.Open();

            int result = cmd.ExecuteNonQuery();

            if (result > 0)
            { lblError.Text = "Record Updated"; 
             bindResultGridView();
            }

            else { lblError.Text = "Update fail"; }

            myConnect.Close();
        }
        catch(Exception)
        {
            lblError.Text = "Please enter correct data";
        }


    }

1 Comment

and u want to save all selected checkbox value into table ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.