Click here to Skip to main content
16,012,110 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi ALl,

I have a dataset with column(0),column(1),column(2) and column(3). I wanted to replace each row on column(3) which has text separated with commas like this "Sales,Reversal" i wanted to replace this with new line..I tried the below code

VB
For Each dr As DataRow In ds.Tables(0).Rows
                Dim i As Integer = 0

                For i = 0 To ds.Tables(0).Rows.Count - 1
                    Replace(ds.Tables(0).DefaultView(i)("Details"), ",", vbCrLf)

                Next
            Next



This does not work ...Please help.Thanks.
Posted
Comments
joshrduncan2012 24-Jul-13 11:01am    
Look at how String.Replace works.
Prasad Khandekar 24-Jul-13 11:17am    
Why not do this in the SELECT query. For SQL server you can use REPLACE function. Your SELECT Query may look similar to one shown below.

SELECT col1, col2, REPLACE(col3, ',', CHAR(13) + CHAR(10)) AS col3 FROM some_table

Regards,
[no name] 24-Jul-13 11:17am    
"This does not work", sure it does. The problem is is that you are simply throwing the result away. As indicated by joshduncan2012 Replace returns a new string.

1 solution

Try this:
VB
Public Sub MyReplace(ByRef ds As DataSet)
    Dim str As String = ","
    Dim mtr As String = vbCrLf

    For Each datarow As DataRow In ds.Tables(0).Rows
        For Each datacol As DataColumn In ds.Tables(0).Columns
            datarow(datacol) = Replace(datarow(datacol).ToString, str, mtr)
        Next
    Next
End Sub
 
Share this answer
 
Comments
vidkaat 24-Jul-13 14:06pm    
Thank u so much
Kuthuparakkal 25-Jul-13 1:48am    
You're welcome!!!

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