.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
Sign In
Win Surprise Gifts!!!

Top 5 Contributors of the Month
Gaurav Pal

Home >> Articles >> ASP.NET >> Post New Resource Bookmark and Share   

 Subscribe to Articles

multiple records from a listbox / checkbox list / radio button list

Posted By:nishithraj       Posted Date: November 27, 2009    Points: 25    Category: ASP.NET    URL: http://www.dotnetspark.com  

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.

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();
while (ISkills.MoveNext())

(Here Skills is the list that we passed to update in database)
In back-end just have a split method to split the data by comma. And it can be updated to the database.

CREATE function [dbo].[fn_Split](
@Skills nvarchar (1000),
@CustomerId int,
@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)


Same thing can apply for checkbox list & radio button list.
 Subscribe to Articles


Further Readings:


No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend