Click here to Skip to main content
15,886,919 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
Hii...

I am working in windows Application and using sql 2005.I have a table called "Details" with having two column named,"No","ServiceName". While I am saving Data from front-end the datas of ServiceName will store in database,separating by comma.Like

No ServiceName
1 Pen,Pencil,Ruler

So i want,when i will retrieve data in a grid,i want to see single item in a single column of a grid.
Like,

No Name
1 Pen
1 Pencil
1 Ruler

So i donot understand how to do this.please help me to solve this.
Posted

create this function
SQL
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
GO
CREATE FUNCTION [dbo].[ParseValues]
(@String varchar(8000), @Delimiter varchar(max) )
RETURNS @RESULTS TABLE (ID int identity(1,1), Val varchar(max))
AS
BEGIN
    DECLARE @Value varchar(max)
    WHILE @String is not null
    BEGIN
        SELECT @Value=CASE WHEN PATINDEX('%'+@Delimiter+'%',@String) >0 THEN LEFT(
          @String,PATINDEX('%'+@Delimiter+'%',@String)-1) ELSE @String END,
          @String=CASE WHEN PATINDEX('%'+@Delimiter+'%',@String) >0 THEN SUBSTRING(
          @String,PATINDEX('%'+@Delimiter+'%',@String)+LEN(@Delimiter),LEN(@String)) ELSE NULL END
        INSERT INTO @RESULTS (Val)
        SELECT @Value
    END
RETURN
END

Now, use this query
SQL
select No,b.val as servicename 
from yourtable
cross apply (dbo.PasrseValues(servicename,',')) as b
Where b.val <> ''

Happy coding!
:)
 
Share this answer
 
Comments
Shivani Dash 20-May-13 6:57am    
Thanks Aarti.... :)
Aarti Meswania 20-May-13 6:59am    
Welcome!
Glad to help you! :)
after getting data in datareader you have to split it using split function
VB
Dim items() As String
Dim i As Integer
dim str as String
str = dr(0) 'your datareader
items = str.Split(",")
For Each item As String In items
    DataGridView1.Item(1, i).value = items(i) 'give your column index and row index
Next

and you havent taged in which language you are working vb.net or C#

for C#
C#
string[] items = null;
String str;
int i = 0;
str = dr[0];
items = str().Split(",");
foreach (string item in items) {
	DataGridView1.Item(1, i).value = items[i];
}
 
Share this answer
 
Comments
Shivani Dash 20-May-13 6:57am    
Thanx a lot for Helping...
Basmeh Awad 20-May-13 7:15am    
welcome...is this what you are searching for

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900