-
Jul 23rd, 2021, 10:26 PM
#1
Thread Starter
Hyperactive Member
Not Getting Data From WebAPI
hi.
i am working on xamarin and created a web api for data transfer but not getting any data in the xamarin form. The web api is showing data in postman on local server but in xamarin the response is showing empty json. The same database is uploaded to the server and the data is present in it too
My Web API
Code:
Public MyConConnection As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("SqlConnectionString").ToString())
<Route("GetConsumerDetailsJSonInList")>
Public Function GetConsumerJSonData(ByVal xCustNo As String) As List(Of ConsumerForLogin)
Dim employeeData As List(Of ConsumerForLogin) = New List(Of ConsumerForLogin)()
Dim obj As ConsumerForLogin()
'employeeData.Add({1, "sdfshfkhsdkhf", "kjhkdfsdf", "sdfjsdkfsf", "sdfjskjfksf"})
'employeeData.Add(New ConsumerForLogin {ConsumerNo="U010001", MyId = 1, Name = "Rohit Singh", Address = "kdflsdjklfdf", MobileNo = "12345" });
Dim mcmd As String = "SELECT * FROM ConsumerMaster WHERE CustNo = @mCNo "
Dim da_Temp As SqlDataAdapter = New SqlDataAdapter(mcmd, MyConConnection)
da_Temp.SelectCommand.Parameters.AddWithValue("@mCNo", xCustNo)
Dim ds_Temp As DataSet = New DataSet()
Dim dTab As DataTable = New DataTable()
da_Temp.Fill(ds_Temp, "ConsumerMaster")
dTab = ds_Temp.Tables("ConsumerMaster")
Dim iRowCount As Integer = dTab.Rows.Count - 1
For i As Integer = 0 To iRowCount
Dim irow As DataRow = dTab.Rows(i)
Dim xmyId As Integer = Convert.ToUInt16(irow("myID").ToString())
employeeData.Add(New ConsumerForLogin() With {
.MyId = irow!MyID,
.Name = irow!Name,
.Address = irow!Address,
.CustNo = irow!CustNo,
.MobileNo = IIf(IsDBNull(irow!MobileNo), " ", irow!MobileNo)
})
Next
Return employeeData
End Function
Code:
namespace UMCWaterBilling.Models
{
public class ConsumerInformation
{
public int MyId { get; set; }
public string CustNo { get; set; }
public string Name { get; set; }
public string Address { get; set; }
public string MobileNo { get; set; }
}
}
my Xamarin Code
Code:
var httpClient = new HttpClient();
var response = await httpClient.GetStringAsync(AppSettings.ApiUrl + "Consumer/GetConsumerDetailsJSonInList?xCustNo='" + txtCustNo.Text + "'");
var login = JsonConvert.DeserializeObject<List<ConsumerInformation>>(response);
ConsumerDetails.ItemsSource = login;
HTML Code:
<StackLayout>
<ListView x:Name="ConsumerDetails">
<ListView.ItemTemplate>
<DataTemplate>
<StackLayout>
<Label Text="{Binding Name}"></Label>
<Label Text="{Binding Address}"></Label>
<Label Text="{Binding CustNo}"></Label>
<Label Text="{Binding MyID}"></Label>
<Label Text="{Binding MobileNo}"></Label>
</StackLayout>
</DataTemplate>
</ListView.ItemTemplate>
</ListView>
</StackLayout>
pls. guide as to what am i doing wrong. thanks
Last edited by kuldevbhasin; Jul 23rd, 2021 at 10:47 PM.
The only time you run out of chances is when you stop taking them.
The mind is like a parachute.
It doesn’t work unless it’s open.
-
Jul 24th, 2021, 02:09 AM
#2
Re: Not Getting Data From WebAPI
I doubt that you should have single quotes in your URL when you call GetStringAsync.
-
Jul 24th, 2021, 05:52 AM
#3
Thread Starter
Hyperactive Member
Re: Not Getting Data From WebAPI
hi thanks for pointing out the silly mistake i was doing
for simplifying the things i changed the code in web api to (just to try out as i was getting an error) :
Code:
Dim employeeData As List(Of ConsumerForLogin) = New List(Of ConsumerForLogin)()
'employeeData.Add({1, "sdfshfkhsdkhf", "kjhkdfsdf", "sdfjsdkfsf", "sdfjskjfksf"})
employeeData.Add(New ConsumerForLogin() With {
.MyId = 1,
.Name = "Consumer Name",
.Address = "Address ",
.CustNo = "CustNo",
.MobileNo = "MobileNo"
})
Return employeeData
and in the Xamarin to :
Code:
var httpClient = new HttpClient();
var response = await httpClient.GetStringAsync(AppSettings.ApiUrl + "Consumer/GetConsumerDetailsJSonInList?xCustNo=" + txtCustNo.Text);
var login = JsonConvert.DeserializeObject<List<ConsumerInformation>>(response);
but its giving me an error in line :
Code:
var response = await httpClient.GetStringAsync(AppSettings.ApiUrl + "Consumer/GetConsumerDetailsJSonInList?xCustNo=" + txtCustNo.Text);
the error i am getting is :
Code:
System.InvalidCastException: 'Specified cast is not valid.'
puzzled as to why i am getting this error even after simplifying the code
The only time you run out of chances is when you stop taking them.
The mind is like a parachute.
It doesn’t work unless it’s open.
-
Jul 24th, 2021, 06:45 AM
#4
Re: Not Getting Data From WebAPI
Are you absolutely sure it's on that line? What does the stack trace say?
-
Jul 24th, 2021, 07:27 AM
#5
Thread Starter
Hyperactive Member
Re: Not Getting Data From WebAPI
i put a break point and after this line it gives me the error :
Your app has entered a break state, but there is no code to show because all threads were executing external code (typically system or framework code).
The only time you run out of chances is when you stop taking them.
The mind is like a parachute.
It doesn’t work unless it’s open.
-
Jul 24th, 2021, 07:45 AM
#6
Thread Starter
Hyperactive Member
Re: Not Getting Data From WebAPI
jmcilhinney u r right its not giving the error on that line. it is showing the data what i changed more is the sequence in the WebAPI
i changed it from :
Code:
employeeData.Add(New ConsumerForLogin() With {
.MyId = 1,
.Name = "Consumer Name",
.Address = "Address ",
.CustNo = "CustNo",
.MobileNo = "MobileNo"
})
to :
Code:
employeeData.Add(New ConsumerForLogin() With {
.MyId = 1,
.CustNo = "CustNo",
.Name = "Consumer Name",
.Address = "Address ",
.MobileNo = "MobileNo"
})
does the sequence too has to be the same ?
it is showing the data in debug but giving the error at the end of the function
The only time you run out of chances is when you stop taking them.
The mind is like a parachute.
It doesn’t work unless it’s open.
-
Jul 24th, 2021, 08:43 AM
#7
Thread Starter
Hyperactive Member
Re: Not Getting Data From WebAPI
hi. thankx a lot for the help.
i got it working.
1st i think the sequence should be same and secondly the problem was somewhere showing it in the listview
took the data in variables and its working fine.
thanks a lot for your guidance.
The only time you run out of chances is when you stop taking them.
The mind is like a parachute.
It doesn’t work unless it’s open.
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|