newtothis newtothis - 6 months ago 38
Vb.net Question

VB.NET ASP.NET Retrieve a list of conutries form database

I am new to ASP.NET and can't seem to find an answer for my problem.

I want to retrieve a list of countries form a table in my database.

I have found a way to do it in C# but i need to do it in Viusal Basic.

private List<Country> PopulateCountry()
{
using (MyDatabaseEntities dc = new MyDatabaseEntities())
{
return dc.Countries.OrderBy(a => a.CountryName).ToList();
}
}


This is the code sample from C# which I tried to convert to VB.net, but I keep getting this error message and can't figure out why.

Private Function GetListOfCountries() As List(Of Country)
Using dc As New MyDatabaseEntities
Try
Dim countryList = (From p In dc.Counties Order By p.CountryName
Ascending
Select p)
Return countryList.ToList()
Catch ex As Exception
Return Nothing
End Try
End Using

End Function


On the
Return countryList.ToList()
line i get this error

Value of type 'List(Of Country)' cannot be converted to 'List(Of Country)'.


Error picture

Any help would be much appreciated. Thanks!

Answer

I believe this function will suit your needs. (your conversion failed due to County instead of Country, where County happens to be a valid entity type as well.)

Private Function GetListOfCountries() As List(Of Country)
    Using dc As New MyDatabaseEntities
        Try
            Return dc.Countries.OrderBy(Function(a) a.CountryName).ToList()
        Catch ex As Exception
            Return Nothing
        End Try
    End Using

End Function
Comments