Solved

checkbox list and database

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

3 Use Cases for Connected Systems

Our Dev teams are like yours. They’re continually cranking out code for new features/bugs fixes, testing, deploying, testing some more, responding to production monitoring events and more. It’s complex. So, we thought you’d like to see what’s working for us.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Media.Imaging 1 18
jquery datatables, asp.net MVC 9 41
JQuery div modal popup is not working with newer version of Java Script/JQuery ? 3 23
jQuery for Each 12 20
I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
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…

773 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