473,322 Members | 1,494 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,322 software developers and data experts.

Loop datatable and select checkboxlist item based on the datatable field values

50
I am trying to display value of the field ("UserID") for every row exists in datatable to checkboxlist(make the checkboxlist item selected).
I used for loop, but only the field value from last row of RoleUsers table is selected in the checkboxlist.

Here is my code

Expand|Select|Wrap|Line Numbers
  1.    Private Sub DisplayRoleUser()
  2.     Dim conn As SqlConnection
  3.     Dim cmd As SqlCommand 
  4.         Dim drDataReader As SqlDataReader
  5.         Dim dtDataTable As New DataTable
  6.  
  7.  
  8.         conn = New SqlConnection("Data Source=(local);Initial Catalog=BHPTrackingSystem;User ID=sa;Password=Safetec2011")
  9.         conn.Open()
  10.         cmd = New SqlCommand("SELECT UserID ,RoleID FROM RoleUsers WHERE RoleID=@RID ", conn)
  11.         cmd.Parameters.Add("@RID", SqlDbType.Int)
  12.         cmd.Parameters("@RID").Value = lstRole.SelectedValue
  13.  
  14.         drDataReader = cmd.ExecuteReader()
  15.         dtDataTable.Load(drDataReader)
  16.         Dim i As Integer
  17.         If dtDataTable.Rows.Count > 0 Then
  18.             For i = 0 To dtDataTable.Rows.Count - 1
  19.                 chkUser.SelectedValue = dtDataTable.Rows(i).Item("UserID")
  20.             Next
  21.         End If
  22.  
  23.         cmd.Dispose()
  24.         conn.Close()
  25.  
Can you guys please tell me where do i get wrong? Your help is much appreciated. THank you very much !
Aug 18 '11 #1
3 7530
Hi Beemomo,

Please find the below code. I guess it will help you.

DataTable table = new DataTable();
table.Columns.Add("Dosage", typeof(int));
table.Columns.Add("Drug", typeof(string));
table.Columns.Add("Patient", typeof(string));
table.Columns.Add("Date", typeof(DateTime));

table.Rows.Add(25, "Indocin", "David", DateTime.Now);
table.Rows.Add(50, "Enebrel", "Sam", DateTime.Now);
table.Rows.Add(10, "Hydralazine", "Christoff", DateTime.Now);
table.Rows.Add(21, "Combivent", "Janet", DateTime.Now);
table.Rows.Add(100, "Dilantin", "Melanie", DateTime.Now);

CheckBoxList1.RepeatColumns = 1;
CheckBoxList1.DataSource = table;
CheckBoxList1.DataTextField = "Drug";

CheckBoxList1.DataBind();

If you have any concern please let me know.

Thanks,
Snehasis
Aug 19 '11 #2
beemomo
50
Thank you for your reply, Snehasis.
I have been away from workstation during last weekend. Sorry for my late reply.

I got my solution for the above post by replacing the for loop using the code below:

Expand|Select|Wrap|Line Numbers
  1. For i = 0 To dtDataTable.Rows.Count - 1
  2.   chkUser.Items.FindByValue(dtDataTable.Rows(i).Item  ("UserID")).Selected = True
  3. Next 
  4.  
I appreciate your help alot! Thanks a again!

Regards,
Beemomo
Aug 22 '11 #3
You are Most Welcome...:)
Aug 24 '11 #4

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

Similar topics

1
by: Eddie | last post by:
I have a DataTable.Select() statement with a DateTime field that is being compared to a Date: Dim drCal as DataRow() = tblCal.Select("CalendarItemDate ='12/1/2003'", "CalendarItemDate",...
3
by: RockNRoll | last post by:
Greetings, Can I select an item in a listbox based on user textbox entry and a submit button? Can you provide any code examples for what needs to occur when the user clicks submit? Thank you,...
2
by: Marcel Hug | last post by:
Hi NG! With a Inner-Join SQL I get my datas in a DataSet. In the table are the column Entry and Version. Like this: Entry Version 1 1 1 2 1 ...
3
by: sdisalvo | last post by:
I'm looking for a way to filter records in a query (or report) based on a value in a field of an adjacent record. Can this be done easily? I cannot find any info in the help files. Thanks
7
convexcube
by: convexcube | last post by:
To keep a record of training levels for different tasks, I have 18 option groups with 4 options values each: 0 labelled as "None", 1 labelled as "Trainee", 2 labelled as "Competent" and 3 labelled as...
3
by: Freud52 | last post by:
I've developed a test that results in standard scores from 35 to 99. Now, i need to be able, when a score for a specific scale is, let's say, >=75 and <=84, to have a predefined block of text be...
6
by: dbuchanan | last post by:
There are three parts to this 1.) How do I cascade menus? 2.) And, how do I cascade menus with a multi-select CheckBoxList?
2
by: daveL | last post by:
how to uppercase a table columen in datatable select //dt is a Datatable i Need to uppercase control.text and table column data i dont know how to set loginname to upper case if...
4
by: DaveL | last post by:
how to uppercase a table columen in datatable select //dt is a Datatable i Need to uppercase control.text and table column data i dont know how to set loginname to upper case if...
3
by: abhijeetraje | last post by:
I am applying XSLT to WSDL to generate html pages. now, as WSDL is not having comprehensive details, i am adding XML documentation (.NET) as well in XSLT. like this.. <xsl:variable name="ddoc"...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

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.