Link to home
Start Free TrialLog in
Avatar of mlg101
mlg101Flag for United States of America

asked on

Change time zone in Cascading DropDown

I'm populating a cascading ddl using webservice for systemtimezones, then I have cascading ddls, using the webservice, and want to convert the date/time in the last ddl (GetTime),  based on the 1st ddl (GetTimeZone Webmethod).

This line of code, which is close to the bottom of my code below, does not return any rows:
Dim strTimeName As String = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dtrTime("EventStart"), TimeZoneInfo.Local.Id, intZoneId.ToString()).ToString()

Any ideas on how to get it to work in this webservice? I have a similar code working on .aspx page using one ddl and 2 labels but this is different.

Thank you.
<WebMethod()> _
    Public Function GetTimeZone(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()



        Dim tzi As ReadOnlyCollection(Of TimeZoneInfo)
        tzi = TimeZoneInfo.GetSystemTimeZones()



        Dim myTimeZone As New List(Of CascadingDropDownNameValue)
        For Each timeZone As TimeZoneInfo In tzi
            Dim Zone As String = CStr(timeZone.DisplayName)
            Dim ZoneID As String = CStr(timeZone.Id)

            myTimeZone.Add(New CascadingDropDownNameValue(Zone, ZoneID))
            'End While
        Next timeZone
        Return myTimeZone.ToArray
    End Function

<WebMethod()> _
    Public Function GetTime(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()
        Dim strConnection As String = ConfigurationManager.ConnectionStrings("BurgoyneMusicConnectionString").ConnectionString
        Dim sqlConn As SqlConnection = New SqlConnection(strConnection)
        'Dim strTimeQuery As String = "SELECT id, TeacherID, InstrumentID, right(convert(varchar(19), EventStart, 100),6) As Times, Available, DatePart(dw, EventStart), EventStart, EventEnd FROM Events WHERE id IN (SELECT MAX(id) FROM Events WHERE DatePart(dw, EventStart) = @DayOfWeekID and (InstrumentID = @InstrumentID Or InstrumentID = 0) and TeacherID = @TeacherID and Available <> 'False'  Group by right(convert(varchar(19), EventStart, 100),6), DatePart(dw, EventStart) )Order by right(convert(varchar(19), EventStart, 100),6)"

        Dim strTimeQuery As String = "SELECT id, TeacherID, InstrumentID, Available, DatePart(dw, EventStart), EventStart, EventEnd FROM Events WHERE id IN (SELECT MAX(id) FROM Events WHERE DatePart(dw, EventStart) = @DayOfWeekID and (InstrumentID = @InstrumentID Or InstrumentID = 0) and TeacherID = @TeacherID and Available <> 'False'  Group by right(convert(varchar(19), EventStart, 100),6), DatePart(dw, EventStart) )Order by right(convert(varchar(19), EventStart, 100),6)"
        Dim cmdFetchTime As SqlCommand = New SqlCommand(strTimeQuery, sqlConn)

        Dim dtrTime As SqlDataReader
        Dim kvTime As StringDictionary = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)

        Dim kvtime2 As StringDictionary = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)
        Dim intDayID2 As Integer
        If Not kvtime2.ContainsKey("day2") Or Not Int32.TryParse(kvtime2("day2"), intDayID2) Then
            Return Nothing
        End If


        Dim intDayId As Integer
        If Not kvTime.ContainsKey("day2") Or Not Int32.TryParse(kvTime("day2"), intDayId) Then
            Return Nothing
        End If

        Dim kvDays As StringDictionary = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)
        Dim intTeachID As Integer
        If Not kvDays.ContainsKey("Instrument") Or Not Int32.TryParse(kvDays("Instrument"), intTeachID) Then
            Return Nothing
        End If
        Dim kvTeach As StringDictionary = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)
        Dim intInstId As Integer
        If Not kvTeach.ContainsKey("Teacher") Or Not Int32.TryParse(kvTeach("Teacher"), intInstId) Then
            Return Nothing
        End If

cmdFetchTime.Parameters.AddWithValue("@InstrumentID", intTeachID)
        cmdFetchTime.Parameters.AddWithValue("@TeacherID", intInstId)
        cmdFetchTime.Parameters.AddWithValue("@DayofWeekID", intDayID2)
        Dim myTime As New List(Of CascadingDropDownNameValue)

        sqlConn.Open()
        dtrTime = cmdFetchTime.ExecuteReader

 While dtrTime.Read()

            Dim kvZone As StringDictionary = CascadingDropDown.ParseKnownCategoryValuesString(knownCategoryValues)

            Dim intZoneId As Integer

            If Not kvTeach.ContainsKey("Zone") Or Not Int32.TryParse(kvZone("Zone"), intZoneId) Then
                Return Nothing
            End If

Dim strTimeName As String = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dtrTime("EventStart"), TimeZoneInfo.Local.Id, intZoneId.ToString()).ToString()

            Dim strTimeId As String = dtrTime("id").ToString

            myTime.Add(New CascadingDropDownNameValue(strTimeName, strTimeId))
        End While

        Return myTime.ToArray
    End Function

Open in new window

Avatar of Siyamala
Siyamala
Flag of India image

Avatar of Nasir Razzaq
Avatar of mlg101

ASKER

Thanks, but I think I need more than just a link or two to figure this out.

@Siyamala, knowing how the cascading dropdown works was a good article, but I think my VB needs some help to make this work. And I already have the ddl of time zones populated with system time zones.

@codecruiser, I got my code from that link and modified it to what I have now.

from the code below, I think the only thing that needs to be fixed is the "intZoneID.ToString()", where I need to get the value from the time zone list, the same way as codecruiser's link code does on the aspx page.

Dim strTimeName As String = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dtrTime("EventStart"), TimeZoneInfo.Local.Id, intZoneId.ToString()).ToString()


Have you populated the zones DDL? Have you set the ValueMember as well with ZoneIDs? If yes then you can use that ddlzones.SelectedValue here
Avatar of mlg101

ASKER

I have populated the zones ddl via the code below. It is in the web method and not on the aspx page so I can't use the ddlzones.selectedvalue. I'm trying to get the selected value in the web method.
<WebMethod()> _
    Public Function GetTimeZone(ByVal knownCategoryValues As String, ByVal category As String) As CascadingDropDownNameValue()



        Dim tzi As ReadOnlyCollection(Of TimeZoneInfo)
        tzi = TimeZoneInfo.GetSystemTimeZones()



        Dim myTimeZone As New List(Of CascadingDropDownNameValue)
        For Each timeZone As TimeZoneInfo In tzi
            Dim Zone As String = CStr(timeZone.DisplayName)
            Dim ZoneID As String = CStr(timeZone.Id)

            myTimeZone.Add(New CascadingDropDownNameValue(Zone, ZoneID))
            'End While
        Next timeZone
        Return myTimeZone.ToArray
    End Function

Open in new window

You are returning TimeZoneInfo array in the method so it can be used. How do you bind it to the DDL?
Avatar of mlg101

ASKER

Maybe I should just tell you what I want to do. I have 4 cascading ddls. Teacher, Instrument, Date, and Time (In that order). I have a 5th ddl, which is the time zone ddl. I want the Time to change depending on the time zone, but it is not dependent on the choices of the other 4, so it is really not cascading in that sense. I just don't know how to do the "converttimezone" function and have the correct time display in the "Time" ddl.

Maybe I could do it on the .aspx page instead of the .asmx page? But not sure because if I postback after changing the timezone ddl, it resets the 4 cascading ddls.
You have the code that converts the timezone. All you need now is the timezone to convert to. You can get it from the DDL but to convert, you need to timezoneid. So whether you do it in aspx or asmx, you need to get the selected timezone id from the DDL. Or you can get the select timezone (text) and then retrieve its ID.
Avatar of mlg101

ASKER

How do you stop the aspx page from resetting the cascading ddls on postback? You need to postback to get to convert as per the link you gave me
Avatar of mlg101

ASKER

I thought I was getting the timezone id in the asmx page by "intZoneID" below:

Dim strTimeName As String = TimeZoneInfo.ConvertTimeBySystemTimeZoneId(dtrTime("EventStart"), TimeZoneInfo.Local.Id, intZoneID.ToString()).ToString()
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.