Click here to Skip to main content
15,881,882 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I face a problem when i try to connect to sql server db with list view control of xamarin forms.

What I have tried:

xaml code:
XML
<stacklayout padding="5">        
        <listview x:name="MainListBox">            
            <listview.itemtemplate>
                <datatemplate>
                    <viewcell>
                        <stacklayout>

and this is my c# code behind:
C#
public MainPage()
{
    InitializeComponent();
    _Load();
}
private async void _Load()
{
    var httpclient = new HttpClient();
    var response = await httpclient.GetStringAsync("https://localhost:22348/api/customer");
    var login = JsonConvert.DeserializeObject<list<customer>>(response);
    MainListBox.ItemsSource = login;
}

By run the app I got this error(in android mode):
System.Net.WebException
  Message=failed to connect to localhost/127.0.0.1 (port 22348) after 86400000ms: isConnected failed: ECONNREFUSED (Connection refused)

I got this error (in uwp mode):
System.Net.Http.HttpRequestException
  HResult=0x80072EFD
  Message=An error occurred while sending the request.
  Source=System.Net.Http
  StackTrace:
   at System.Net.Http.HttpClientHandler.<sendasync>d__111.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.Http.HttpClient.<finishsendasyncunbuffered>d__59.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Net.Http.HttpClient.<getstringasynccore>d__27.MoveNext()
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at App5_temp2.MainPage.<login>d__1.MoveNext() in C:\Users\HRS7_PC\source\repos\App5_temp2\App5_temp2\App5_temp2\MainPage.xaml.cs:line 28

Inner Exception 1:
COMException: The text associated with this error code could not be found.

A connection with the server could not be established
Posted
Updated 30-Nov-20 21:52pm
v2

1 solution

The error message in Android mode is quite clear - your code is running on the Android emulator, but the service you are trying to call is not. Specify the correct URL for your service, replacing localhost with the name or IP address of the computer which is hosting the service.

The UWP error suggests that your service is not running, or there is a firewall blocking access.
 
Share this answer
 
Comments
hanymet 1-Dec-20 11:59am    
By change localhost with my local ip (in android mode) the app is works without loading data...
also i try with pc name and the same result (app works without loading data)
But in uwp mode the error still the same.
what i have missed
hanymet 1-Dec-20 18:43pm    
I have found the missing part, That I should host my asp.net web to iis first, So by deploy and host web to iis, the app works well.
Thank you Richard

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900