Solved

checkbox list and database

Posted on 2014-02-08
3
328 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
  • 2
3 Comments
 
LVL 28

Accepted Solution

by:
sammySeltzer earned 500 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 28

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

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…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
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)
The viewer will learn the basics of jQuery including how to code hide show and toggles. 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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now