multiple records from a listbox / checkbox list / radio button list.
Many people asked me to help them to code for different scenarios. Most of the people find difficulty to save multiple data into database while selecting a listbox.
That's possible by implementing the following method. For example in an application there are several Skills to be selected. The user can select any number of Skills from the list box.
We can add them into a collection called objColl(its generic list) as follows.
(Here Skills is the list that we passed to update in database)
List objColl = new List();
for (int intCount = 0; intCount < lstSkills.Items.Count; intCount++)
SkillInfo objSkills = new SkillInfo();
if (lstSkills.Items[intCount].Selected == true)
objSkills.SkillID = Convert.ToInt32(lstSkills.Items[intCount].Value);
The next thing we can do is, we can append a comma to each Skillid
IEnumerator ISkills = Skills.GetEnumerator();
In back-end just have a split method to split the data by comma. And it can be updated to the database.
Same thing can apply for checkbox list & radio button list.
CREATE function [dbo].[fn_Split](
@Skills nvarchar (1000),
@Delimiter nvarchar (10)
returns @ValueTable table ([SkillID] INT, [ProgramInfoId] INT)
declare @NextString nvarchar(4000)
declare @Pos int
declare @NextPos int
declare @CommaCheck nvarchar(1)
set @NextString = ''
set @CommaCheck = right(@Skills,1)
set @Skills = @Skills + @Delimiter
set @Pos = charindex(@Delimiter,@Skills)
set @NextPos = 1
while (@pos <> 0)
set @NextString = substring(@Skills,1,@Pos - 1)
insert into @ValueTable ( [SkillID], [CustomerId]) Values (@CustomerId, @NextString)
set @Skills = substring(@Skills,@pos +1,len(@Skills))
set @NextPos = @Pos
set @pos = charindex(@Delimiter,@Skills)