Hi all...
I am not able to form a logic -- Please help --
I have a checklistbox - With a list of names (total 56 names)...
Now, I want that - If I select 5 users , then total 5 records/rows must be saved in the database...
Eg - If I select A,B,C,D,E - Then in database - 5 rows must be inserted in table A with values -
A
B
C
D
E
-----------
How do I form this loop ??

Francis SusaimichaelRank #466 · 5.4K reputationPosted Jan 31, 2016, 2:55 PM EST
foreach(ListItem lstItem in chkBoxList.Items)
{
// check the list item is selected
if(lstItem.Selected)
{
// To do your insert logic here
}
}
Riddhi ValechaRank #794 · 2.9K reputationPosted Feb 1, 2016, 4:27 AM EST
Francis SusaimichaelRank #466 · 5.4K reputationPosted Feb 1, 2016, 4:36 AM EST
I guess, I have answered your first question. The question you have asked now, is not relevant with your first question. So you need to create a new thread for your question. The reason is if any one refer this thread in future he get confused because of the subject you gave and questions you asked. :)
Riddhi ValechaRank #794 · 2.9K reputationPosted Feb 2, 2016, 3:13 AM EST
checklistbox1.datavaluefield="ID"
foreach(listitems lst in checklistbox1.items)
{
if(lst.selected == true)
{
// Insert -1
}
}
Likewise, I have selected 1,2,3 values ( DataValueFileds), So I want 3 rows - 1,2,3 in database.
This loop is not iterating on button click event.
Francis SusaimichaelRank #466 · 5.4K reputationPosted Feb 2, 2016, 5:50 AM EST
Have you debugged the application by keeping break points? If possible share your code.
Riddhi ValechaRank #794 · 2.9K reputationPosted Feb 2, 2016, 8:22 PM EST
{
chkboxEmpNames.DataSource = Datatable;
chkboxEmpNames.DataTextField = "EmpName";
chkboxEmpNames.DataValueField = "ID";
chkboxEmpNames.DataBind();
}
--
private void INsertdata()
{
foreach (ListItem lstItem in chkboxEmpNames.Items
{
if (lstItem.Selected == true)
{
Insert into EmpTable (EmpID) values( convert.toint32(chkboxEmpNames.SelectedValue));
}
}
}
---------
In "If-Condition" - I get next value , but in "convert.toint32(chkboxEmpNames.SelectedValue)", the value is not moving.
Francis SusaimichaelRank #466 · 5.4K reputationPosted Feb 2, 2016, 9:20 PM EST
You need to refer the current item. Not the entire Checkbox List. Try with this code:
private void INsertdata()
{
foreach (ListItem lstItem in chkboxEmpNames.Items
{
if (lstItem.Selected == true)
{
Insert into EmpTable (EmpID) values( convert.toint32(lstItem.Value));
}
}
}