[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

how to split the record from db

Posted on 2009-02-18
8
Medium Priority
?
168 Views
Last Modified: 2013-11-07
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

0
Comment
Question by:Rajar Ahmed
  • 5
  • 3
8 Comments
 
LVL 10

Expert Comment

by:JPJ78
ID: 23670402
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
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 23670745
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
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 23670772
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Accepted Solution

by:
JPJ78 earned 2000 total points
ID: 23670884
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
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 23671001
it worked , can u please explain the function  ,

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

0
 
LVL 18

Author Closing Comment

by:Rajar Ahmed
ID: 31548244
Thanks mate :) .Dats a great function ..
0
 
LVL 10

Expert Comment

by:JPJ78
ID: 23671269
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
 
LVL 18

Author Comment

by:Rajar Ahmed
ID: 23672512
thanks again ..:)...
0

Featured Post

Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

More often than not, we developers are confronted with a need: a need to make some kind of magic happen via code. Whether it is for a client, for the boss, or for our own personal projects, the need must be satisfied. Most of the time, the Framework…
A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
This video shows how to quickly and easily deploy an email signature for all users in Office 365 and prevent it from being added to replies and forwards. (the resulting signature is applied on the server level in Exchange Online) The email signat…
Loops Section Overview
Suggested Courses

829 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question