Solved

checkbox list and database

Posted on 2014-02-08
3
333 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

In this article you'll learn how to use Ajax calls within your CodeIgniter application. To explain this, I'll illustrate how to implement a simple contact form to allow visitors to send you an email through your web site.
Introduction JSON is an acronym for JavaScript Object Notation.  It is a text-string data transport mechanism, capable of representing simple or complex data structures in a consistent and easy-to-read manner.  Similar in concept to XML, but more e…
The viewer will learn how to dynamically set the form action using jQuery.
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…

895 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

18 Experts available now in Live!

Get 1:1 Help Now