checkboxlist problem

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sandeepv
    New Member
    • Jan 2009
    • 8

    checkboxlist problem

    Hi friends,

    i have a checkboxlist with multiple selections connected to the database.
    but it just stores one value.
    i want to store multiple values.
    Can you please help me out???????
  • liawcv
    New Member
    • Jan 2009
    • 33

    #2
    <asp:CheckBoxLi st> control, by default, is already a multi-selection control. I guess, it may be the problem of how you have its data binding or select value... Erm...

    Please post your codes, perhaps part of your codes, so that we can understand your scenario further. Thanks... : )

    Comment

    • Curtis Rutland
      Recognized Expert Specialist
      • Apr 2008
      • 3264

      #3
      Also, is this a Windows Forms app, or an ASP.NET website?

      Comment

      • sandeepv
        New Member
        • Jan 2009
        • 8

        #4
        i have no idea abt the code.... i just know we hav to use loop for it....

        n im doing it in ASP.NET (.aspx & .aspx.vb)

        Comment

        • liawcv
          New Member
          • Jan 2009
          • 33

          #5
          Hi, sandeepv. I think a loop plays a major role in solving your problem. Somehow, different implementations for different scenarios. More information is needed in order for us to understand your issue further... : )

          Give us some hints, such as:

          - Do you place your CheckBoxList within another control such as GridView, FormView, etc?

          - Do your CheckBoxList populates its list items based on a list of hard-coded values or dynamically from a database table?

          - What is your database tables' structure? Do you store your selected values as a comma separated list within a single field? Or do you store your selected values in a separated table?

          - How do you access to database? Manually create your Connection, Command and DataReader objects? Or just by configuring a SqlDataSource control?

          - etc...

          Without some of these details, none of us can start our first step to help you further. Hope to hear from you soon... : )

          Comment

          • sandeepv
            New Member
            • Jan 2009
            • 8

            #6
            thankyou for responding.
            will specify exactly how i was thinking abt my situation....

            Suppose i have a database named test with one attribute.

            i have a plain checkboxlist i.e no gridview etc.

            suppose there are 5 items in the checkboxlist

            my question>if the person clicks on the 4 items in the checkboxlist then i want to save those 4 items in the attribute in the database

            pls help me out....

            Comment

            • liawcv
              New Member
              • Jan 2009
              • 33

              #7
              Suppose you're using MS Access database (named Sample.mdb and located at the App_Data folder of your Website), and the TEST table consists of 2 fields: TEST_ID (PK, auto number) and TEST_ITEM (text).

              Suppose a user is to select some items from the CheckBoxList cblTestItem. If he clicks on the button btnInsert, the selected list items will be formatted as a comma-separated string, and to be inserted into the TEST table's TEST_ITEM field as a new record.

              Code:
              using System.Data.OleDb;
              ...
              protected void btnInsert_Click(object sender, EventArgs e)
              {
                 // 1. Get the selected items and format as comma-separated string
                 ArrayList list = new ArrayList();
                 foreach (ListItem i in cblTestItem.Items)
                 {
                    if (i.Selected)
                       list.Add(i.Value);
                 }
                 string[] arr = (string[])list.ToArray(typeof(string));
                 string test_item = string.Join(",", arr);
              
                 // 2. Insert into database
                 string cs = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Sample.mdb";
                 string sql = "INSERT INTO TEST (TEST_ITEM) VALUES (?)";
                 OleDbConnection con = new OleDbConnection(cs);
                 OleDbCommand cmd = new OleDbCommand(sql, con);
                 cmd.Parameters.AddWithValue("", test_item);
                 con.Open();
                 cmd.ExecuteNonQuery();
                 con.Close();
              }
              The code above is to give you some basic ideas about how the job can be done. Change (connection string, sql, etc) and enhance (try-catch, message, etc) the logic based on your needs... : )

              Comment

              • sandeepv
                New Member
                • Jan 2009
                • 8

                #8
                thankyou the code and explanation.

                can you pls tel me the code for SQL Server???

                Comment

                • liawcv
                  New Member
                  • Jan 2009
                  • 33

                  #9
                  SQL Server? Generally... : )

                  Code:
                  // using System.Data.OleDb; <-- replace this with
                  using System.Data.SqlClient;
                  ...
                  // string cs = "Provider=Microsoft..."; <-- replace this with
                  string cs = "...";
                  // where ... is connection string for SQL Server
                  I think you may have to do some basic study on database access on your own. Cheer... : )

                  Comment

                  • Curtis Rutland
                    Recognized Expert Specialist
                    • Apr 2008
                    • 3264

                    #10
                    We have two articles on using DBs in your .NET programs on this site. I suggest you read them both if you don't know how to use DBs in your program:

                    Comment

                    • sandeepv
                      New Member
                      • Jan 2009
                      • 8

                      #11
                      im sorry the previous reply of mine was incomplete....i know the SQL connection....j ust wanted to know something else...
                      wanted to know the aspx.vb script for the code u have specified.....
                      as list.add() doesnt work in aspx.vb

                      thankyou

                      Comment

                      • liawcv
                        New Member
                        • Jan 2009
                        • 33

                        #12
                        Erm. Get your point... : )
                        Here is the VB.NET version of manipulating the ArrayList:

                        Code:
                        ...
                        Dim list As New ArrayList
                        For Each i As ListItem In cblTestItem.Items
                           If i.Selected Then
                              list.Add(i.Value)
                           End If
                        Next
                        Dim arr() As String = list.ToArray(GetType(String))
                        Dim test_item As String = String.Join(",", arr)
                        ...

                        Comment

                        Working...