Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

checkbox list and database

Posted on 2014-02-08
3
Medium Priority
?
341 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

Ask an Anonymous Question!

Don't feel intimidated by what you don't know. Ask your question anonymously. It's easy! Learn more and upgrade.

Question has a verified solution.

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

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…
This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
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…
Suggested Courses

610 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