how to split the record from db

i need to split the records from db and show it on the page in the calendar_selectionchanged event ,
if the subjects columns contains records like this way ,

For EG
2#10#103,3#11#110,4#12#120
this should be displayed like this , i mean seperating # and (kama),  (after kama it should be entered in next line  )
                                             
For 2 you get 10 of 103---------->2#10#103
for 3 you get 11 of 110----------->3#11#110
for 4 you get 12 of 120------------>4#12#120

my Table contains like this ,
name          subjects
James        2#10#103,3#11#110,4#12#120
Nelson        4#12#123,5#11#120,6#2#133
romeo          5#6#123,8#1#100

this loop manages to get the name from the db, but i find difficcult to split  the values and display as such ,which  i mentioned above ..
 While (myDataReader.Read())
               If (myDataReader.Item("real") = 1 And myDataReader.Item("illusion") = 1) Then
                temp &= "<br>name :" & myDataReader.Item("name") & ""
            ElseIf (myDataReader.Item("real") = 0 And myDataReader.Item("illusion") = 1) Then
                temp &= "<br>name :" & myDataReader.Item("name") & ""
            End If


Public Sub Calendar1_SelectionChanged(ByVal sender As Object, ByVal e As EventArgs)
        Dim myConnection As SqlConnection
        Dim myCommand As SqlCommand
        Dim myDataReader As SqlDataReader
        myConnection = New SqlConnection(ConfigurationSettings.AppSettings("str"))
        myCommand = New SqlCommand("design", myConnection)
        myCommand.CommandType = CommandType.StoredProcedure
        Dim smalldt As New SqlParameter("@smalldt", SqlDbType.DateTime, 8)
        smalldt.Value = Calendar1.SelectedDate
        myCommand.Parameters.Add(smalldt)
        myConnection.Open()
        myDataReader = myCommand.ExecuteReader()
        Dim temp As String = ""
         While (myDataReader.Read())
               If (myDataReader.Item("real") = 1 And myDataReader.Item("illusion") = 1) Then
                temp &= "<br>name :" & myDataReader.Item("name") & ""
            ElseIf (myDataReader.Item("real") = 0 And myDataReader.Item("illusion") = 1) Then
                temp &= "<br>name :" & myDataReader.Item("name") & ""
            End If
        End While
        myDataReader.Close()
        myConnection.Close()
        Label1.Text = temp
    End Sub

Open in new window

LVL 18
Rajar AhmedConsultantAsked:
Who is Participating?
 
JPJ78Connect With a Mentor Commented:
Try this....
         While (myDataReader.Read())
               If (myDataReader.Item("real") = 1 And myDataReader.Item("illusion") = 1) Then
                temp &= "<br>name :" & myDataReader.Item("name") & "<br/>" & FormatSubject(myDataReader.Item("subject"))
            ElseIf (myDataReader.Item("real") = 0 And myDataReader.Item("illusion") = 1) Then
                temp &= "<br>name :" & myDataReader.Item("name") & "<br/>" & FormatSubject(myDataReader.Item("subject"))
            End If
        End While
 
    Private Function FormatSubject(ByVal subject As String) As String
        Dim text As String = ""
        Dim subjects() As String = subject.Split(",")
        For Each subj As String In subjects
            Dim vals() As String = subj.Split("#")
            If vals.Length > 2 Then
                text += "for " + vals(0).ToString() + " you get " + vals(1).ToString() + " of " + vals(2).ToString() + "<br/>"
            End If
        Next
        Return text
    End Function

Open in new window

0
 
JPJ78Commented:
I'm not completly sure what you want.
Maybe you could use the Replace function.
temp.Replace(",", "<br/">) 'This will replace all , with a <br/> to ge a linebreak in HTML.
alt.
myDataReader.Item("name").ToString().Replace(",", "<br/">)

or use the split function
Dim tempArr() as String = temp.Split(",")
alt.
Dim tempArr() as String = myDataReader.Item("name").ToString().Split(",")
0
 
Rajar AhmedConsultantAuthor Commented:
well, one of my field(subjects) in db stored the records in this format ,

12#23#200,5#4#200

this represents there are two characters between the integers one is # and other is ,(kama)

 i need to seperate these  two characters and display on the page .

rightnow the code  displays only this,
 name : michael

but i want to bring the subjects fields also in the below format

if michel has subject fields like this  2#10#103,3#11#110,4#12#120

then in the calendar _selectionchanged
OUTPUT
name:michael    
for 2 you get 10 of 103
 for 3 you get 11 of 110
for 4 you get 12 of 120

You can see this above output ,
 that # are replaced by the string (you get) and (of) are
,(kama) are replaced by the <br>
 
0
Cloud Class® Course: Microsoft Exchange Server

The MCTS: Microsoft Exchange Server 2010 certification validates your skills in supporting the maintenance and administration of the Exchange servers in an enterprise environment. Learn everything you need to know with this course.

 
Rajar AhmedConsultantAuthor Commented:
You can see this above output ,
 that # are replaced by the string (you get) and (of) and (not are as it was in previous comment)
,(kama) are replaced by the <br>
0
 
Rajar AhmedConsultantAuthor Commented:
it worked , can u please explain the function  ,

Wats this do . i mean subjects()  --??
Dim subjects() As String = subject.Split(",") <---

0
 
Rajar AhmedConsultantAuthor Commented:
Thanks mate :) .Dats a great function ..
0
 
JPJ78Commented:
Dim subjects() As String  'Declares the variable subjects as an array of strings.
subject.Split(",") 'Splits the subject string into multiple strings at the places where "," occurs.

    Private Function FormatSubject(ByVal subject As String) As String
        Dim text As String = ""
        Dim subjects() As String = subject.Split(",")  'Splits the string up into an string array. The separator i ","
        For Each subj As String In subjects 'Loops through all parts of the string array
            Dim vals() As String = subj.Split("#") 'Splits the current part into a new string array
            If vals.Length > 2 Then 'Just a check so we won't get an error if the string we splitted didn't contain more than a value
                text += "for " + vals(0).ToString() + " you get " + vals(1).ToString() + " of " + vals(2).ToString() + "<br/>"
            End If
        Next
        Return text
    End Function


I hope this explains the function.
0
 
Rajar AhmedConsultantAuthor Commented:
thanks again ..:)...
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.