How to add multiple check box values to single column in database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sarvanhsr
    New Member
    • Jan 2010
    • 8

    How to add multiple check box values to single column in database

    Code:
    <table cellpadding="0" cellspacing="0" width="100%">
                            <tr>
                                <td style="width: 156px">
                                    <asp:CheckBox ID="News" runat="server" Text="News"/>
                                    &nbsp;
                                    <br />
                                    <br />
                                </td>
                                <td style="width: 162px">
                                    <asp:CheckBox ID="HnF" runat="server" Text="Home &amp; Family"/>
                                    <br />
                                    <br />
                                </td>
                                <td style="width: 155px">
                                    <asp:CheckBox ID="HnN"  runat="server" Text="Health &amp; Nutrition"/>
                                    <br />
                                    <br />
                                </td>
                                <td>
                                    <asp:CheckBox ID="Auto" runat="server" Text="Automobiles"/>
                                    <br />
                                    <br />
                                </td>                            
                            </tr>
                            <tr>
                                <td style="width: 156px">
                                    <asp:CheckBox ID="Education" runat="server" Text="Education"/>
                                    <br />
                                    <br />
                                </td>
                                <td style="width: 162px">
                                    <asp:CheckBox ID="Loand" runat="server" Text="Loans"/>
                                    <br />
                                    <br />
                                </td>
                                <td style="width: 155px">
                                    <asp:CheckBox ID="Travel" runat="server" Text="Travel"/>
                                    <br />
                                    <br />
                                </td>
                                <td>
                                    <asp:CheckBox ID="Computers" runat="server" Text="Computers"/>
                                    <br />
                                    <br />
                                </td>                            
                            </tr>                        
                            <tr>
                                <td style="width: 156px">
                                    <asp:CheckBox ID="Shopping" runat="server" Text="Shopping"/>
                                    <br />
                                    <br />
                                </td>
                                <td style="width: 162px">
                                    <asp:CheckBox ID="Insurance" runat="server" Text="Insurance"/>
                                    <br />
                                    <br />
                                </td>
                                <td style="width: 155px">
                                    <asp:CheckBox ID="Beauty" runat="server" Text="Beauty "/>
                                    <br />
                                    <br />
                                </td>
                                <td>
                                    <asp:CheckBox ID="Fashion" runat="server" Text="Fashion"/>
                                    <br />
                                    <br />
                                </td>                            
                            </tr>
                            <tr>
                                <td style="width: 156px">
                                    <asp:CheckBox ID="Sports" runat="server" Text="Sports "/>
                                    <br />
                                    <br />
                                </td>
                                <td style="width: 162px">
                                    <asp:CheckBox ID="OnlineGames" runat="server" Text="Online Gaming"/>
                                    <br />
                                    <br />
                                </td>
                                <td style="width: 155px">
                                    <asp:CheckBox ID="SpecialOffers" runat="server" Text="Special Offers"/>
                                    <br />
                                    <br />
                                </td>
                                <td>
                                    <asp:CheckBox ID="Family" runat="server" Text="Family"/>
                                    <br />
                                    <br />
                                </td>                            
                            </tr>
                        </table>
    I am having Table called Interest and has only one column named Answer...
    i want the checked values to insert in database..If i select Family, News and Fashion... The inserted data should be in Three rows of answer field..
    I know it is possible only through arrays..but.... not having any idea about this..
    Please...Help.. ..
    Last edited by tlhintoq; Feb 19 '10, 02:39 PM. Reason: [CODE] ...Your code goes between code tags [/CODE]
  • CroCrew
    Recognized Expert Contributor
    • Jan 2008
    • 564

    #2
    Hello sarvanhsr,

    No one says it better then tlhintoq: When you are writing your question, there is a button on the tool bar that wraps the [code] tags around your copy/pasted code. It helps a bunch. Its the button with a '#' on it. More on tags. They're cool. Check'em out.

    Happy Coding,
    CroCrew~

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      I'm sorry but I don't understand your problem.

      Could you please explain how Family, News and Fashion have anything to do with Interest?

      Oh wait, I think I understand now...you have categories: Family, News and Fashion etc and you want to be able to track which categories the user is "interested " in. This doesn't have anything to do with calculating interest.

      Ok...well...you need a button to submit the page to the server. In the button click event you need to loop through the check boxes and determine which ones are selected.

      From there you will be able to write a SQL query that will update your database.

      It is easier to use a CheckBoxList than it is to use individual check boxes...so that you can actually use a loop...but you can do whatever you want.

      Check out the articles:

      Make sure that you use Parameters with you SQL Command to avoid possible injection attacks.

      -Frinny

      Comment

      • semomaniz
        Recognized Expert New Member
        • Oct 2007
        • 210

        #4
        If i understand your question right here is what i would do.use checkbox list and use a for loop to extract the checked item and then create a string of checked items separated with delimiter (",") and then write a code to insert in the database.

        Comment

        Working...