Code Won't Work in ASP.NET VB.NET

Austin Althouse 26 Reputation points
2021-02-16T18:55:33+00:00

Hello, I am making a VB.NET ASP.NET page to replace a vb.net windows forms app. I am trying to insert local weather via the national weather service's api. The code is:

Private Sub WeatherInfo()
Using WB As New WebClient
Try
WB.Headers.Add("User-Agent", "AWNHD Weather Broadcast Engine/v1.0 (http://www.austinlandweather.com; aalthouse@austinlandweather.com")
Dim ObservationData As String = WB.DownloadString("https://api.weather.gov/stations/K3AU/observations/latest?require_qc=false")

            Dim weather As WeatherData = JsonConvert.DeserializeObject(Of WeatherData)(ObservationData)

            Dim o As JObject = JObject.Parse(ObservationData)

            Dim IconLink As String

            Console.WriteLine($"{o("properties")("station")}")
            IconLink = $"{o("properties")("icon")}"
            MainCityConditionsLabel.Text = ($"{o("properties")("textDescription")}")

            Dim bytes() As Byte = WB.DownloadData(IconLink)
            Dim ImgStream As New MemoryStream(bytes)

            MainCityWeatherIcon.ImageUrl = IconLink

This is in a try catch code. The code works but for some reason, the label and the image box which are supposed to contain the information and icon remain blank? Why would that be?

Thanks!

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,500 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. XuDong Peng-MSFT 10,741 Reputation points Microsoft Vendor
    2021-02-18T05:49:23.507+00:00

    Hi @Austin Althouse ,

    According to the code you provided, I created a simple demo and test these code, but I can not get the same issue.

    I only got a 403 exception in this line of code Dim bytes() As Byte = WB.DownloadData(IconLink). I think this is because of the user authentication problem, so I moved part of the code position to change the execution order so that the exception will not affect the final result, and finally these code works fine.

    Something like this (in WebForm):

    <div>  
          <asp:Label Text="text" ID="MainCityConditionsLabel" runat="server" />  
          <asp:Image ImageUrl="imageurl" ID="MainCityWeatherIcon" runat="server" />  
    </div>  
    
    IconLink = $"{o("properties")("icon")}"   
    MainCityWeatherIcon.ImageUrl = IconLink  
    MainCityConditionsLabel.Text = $"{o("properties")("textDescription")}"  
    Dim bytes() As Byte = WB.DownloadData(IconLink)  
    Dim ImgStream As New MemoryStream(bytes)  
    

    Result:
    data content:69391-data.png
    Page display:69308-show.png

    If possible, could you provide more details?

    Best regards,
    Xudong Peng


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.