?
Solved

checkbox list and database

Posted on 2014-02-08
3
Medium Priority
?
340 Views
Last Modified: 2014-02-16
I have the table like below

product
productid desc.    cost
1               pc        100
2               laptop  200
3               tablet   300

recordtran.
id cost
1  100
2  200


I already have datalist to read product table and now using checkbox within the datalist
If the checkbox is checked, I want to insert a row into the table (recordtable)
if the checkbox is not checked, do nothing

Can you show me how to code in aspx/c# code behind?

Thanks
0
Comment
Question by:CoolDev2014
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 29

Accepted Solution

by:
sammySeltzer earned 2000 total points
ID: 39845256
Hi,

Assuming that you are inserting products into the database, you would have this markup (replace with yours)

    <form id="form1" runat="server">
    <div>
Hobbies:
<asp:CheckBoxList ID="chkProducts" runat="server">
</asp:CheckBoxList>
<br />
        <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Insert" />
    </div>
    </form>

Open in new window


Then codebehind

protected void Button1_Click(object sender, EventArgs e)

    {

        String str = "";

        for (int i = 0; i <= chkProducts.Items.Count - 1; i++)

        {




            if (chkProducts.Items[i].Selected)

            {




                if (str == "")

                {

                    str = chkProducts.Items[i].Text;

                }

                else

                {

                    str += "," + chkProducts.Items[i].Text;




                }




            }

        }

        con.Open();

        SqlCommand cmd = new SqlCommand("Insert into YourTable(Products) values('" + str + "')", con);

        cmd.ExecuteNonQuery();

        Clear();

    }

    public void Clear()

    {

        chkProducts.SelectedIndex = -1;

    }

Open in new window



Not tested but the concept is solid
0
 

Author Comment

by:CoolDev2014
ID: 39845661
hope to have working codes.....
0
 
LVL 29

Expert Comment

by:sammySeltzer
ID: 39846055
I can't test it for you because you have other pieces of the code.

You need to integrate with your code and test.

Then if you have any issues, post back.
0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction Chart.js, used properly, can visually add a difference to your charting applications. It engages your visitors and allows them to interact with data they otherwise wouldn't be able to without expensive and complicated systems. For this…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
Suggested Courses
Course of the Month15 days, 4 hours left to enroll

771 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question