473,387 Members | 1,574 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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???????
Jan 14 '09 #1
11 2838
liawcv
33
<asp:CheckBoxList> 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... : )
Jan 14 '09 #2
Curtis Rutland
3,256 Expert 2GB
Also, is this a Windows Forms app, or an ASP.NET website?
Jan 14 '09 #3
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)
Jan 16 '09 #4
liawcv
33
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... : )
Jan 16 '09 #5
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....
Jan 17 '09 #6
liawcv
33
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.

Expand|Select|Wrap|Line Numbers
  1. using System.Data.OleDb;
  2. ...
  3. protected void btnInsert_Click(object sender, EventArgs e)
  4. {
  5.    // 1. Get the selected items and format as comma-separated string
  6.    ArrayList list = new ArrayList();
  7.    foreach (ListItem i in cblTestItem.Items)
  8.    {
  9.       if (i.Selected)
  10.          list.Add(i.Value);
  11.    }
  12.    string[] arr = (string[])list.ToArray(typeof(string));
  13.    string test_item = string.Join(",", arr);
  14.  
  15.    // 2. Insert into database
  16.    string cs = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=|DataDirectory|Sample.mdb";
  17.    string sql = "INSERT INTO TEST (TEST_ITEM) VALUES (?)";
  18.    OleDbConnection con = new OleDbConnection(cs);
  19.    OleDbCommand cmd = new OleDbCommand(sql, con);
  20.    cmd.Parameters.AddWithValue("", test_item);
  21.    con.Open();
  22.    cmd.ExecuteNonQuery();
  23.    con.Close();
  24. }
  25.  
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... : )
Jan 18 '09 #7
thankyou the code and explanation.

can you pls tel me the code for SQL Server???
Jan 19 '09 #8
liawcv
33
SQL Server? Generally... : )

Expand|Select|Wrap|Line Numbers
  1. // using System.Data.OleDb; <-- replace this with
  2. using System.Data.SqlClient;
  3. ...
  4. // string cs = "Provider=Microsoft..."; <-- replace this with
  5. string cs = "...";
  6. // where ... is connection string for SQL Server
  7.  
I think you may have to do some basic study on database access on your own. Cheer... : )
Jan 19 '09 #9
Curtis Rutland
3,256 Expert 2GB
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:
Jan 19 '09 #10
im sorry the previous reply of mine was incomplete....i know the SQL connection....just 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
Jan 20 '09 #11
liawcv
33
Erm. Get your point... : )
Here is the VB.NET version of manipulating the ArrayList:

Expand|Select|Wrap|Line Numbers
  1. ...
  2. Dim list As New ArrayList
  3. For Each i As ListItem In cblTestItem.Items
  4.    If i.Selected Then
  5.       list.Add(i.Value)
  6.    End If
  7. Next
  8. Dim arr() As String = list.ToArray(GetType(String))
  9. Dim test_item As String = String.Join(",", arr)
  10. ...
Jan 20 '09 #12

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Dennis Johansson | last post by:
I have a CheckBoxList like this one: <asp:CheckBoxList id="chkReport" runat="server" DataValueField="rptID" DataTextField="RptName" Enabled="False" RepeatLayout="Flow"></asp:CheckBoxList> I...
4
by: dm_dal | last post by:
Is there a know issue surrounding the CheckBoxList control and it's viewstate? When my control is created, it's ListItems are checked as needed, but on a postback, they loose their Selected...
3
by: JD | last post by:
Hello, I have a problem with checkboxlist inside Repeater (in ASP.NET page). I am able to create Checkboxlist and bind it (inside Repeater_ItemBound - including setting checked/unchecked)....
3
by: Dune | last post by:
Hi, Is there anyway to get the datavaluefield from a databound checkboxlist using javascript? If not, is there any way to associate a custom attribute with the databound checkboxlist items so...
5
by: Patrick.O.Ige | last post by:
I'm binding a CheckBoxlist below in the ItemDataBound(the CheckBoxList is in a Datalist) By doing "li.Selected = True" i can see all the checkBoxes are selected. But what i want is to be able...
4
by: Patrick.O.Ige | last post by:
I have a CheckBoxList in a DataList and i'm trying to get item Selected after doing a postBack. I have set my CheckBoxlist AutoPostBack="True" Any ideas what 'm doing wrong? It seems not to...
7
by: ThunderMusic | last post by:
Hi, I have a CheckBoxList and I want to add some javascript code to each CheckBox created by this CheckBoxList. I tried iterating through all items of the list, all the controls, do a FindControl,...
0
by: webmaster | last post by:
Hi all, I'm tearing my hair out with this one. I have successfully implemented by own RadioButtonList in order to provide additional functionality and a DIV rather than TABLE-based layout in...
4
by: haresh.amis | last post by:
hello to all, I m using .net 2.0 and i face a problem that is as under Well I have a checkboxlist which i bound in .cs page now I want to count that how many checkboxes ate checked ( In...
2
by: mercea | last post by:
hi all, i have a grid view and i have inserted a checboxlist template with 5checkboxes per row into the gridview. i want the user to be able to select an option (A-E) and that selected option be...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.