Home » c# » c# – Null response from restshap by asp.net-Exceptionshub

c# – Null response from restshap by asp.net-Exceptionshub

Posted by: admin February 24, 2020 Leave a comment

Questions:

I’ve developed with Visual Studio 2019 a ConsoleApp C# where a make a request with restsharp and work good.

here there is the code that I’ve used for make a request:

using RestSharp;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Data;
using System.Data.SqlClient;



namespace RetailerUrlStatusChecker
{
    public class Program
    {

        static async Task Main(string[] args)
        {
            await ProcessRepositories();

        }

        private static async Task ProcessRepositories()
        {


            var client = new RestClient("https://api-developer.com/products");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept", "application/json");
            request.AddParameter("application/json", "{\"category\": \"all\"}", ParameterType.RequestBody);

            var response2 = await client.ExecuteTaskAsync<List>(request);
            List products= response2.Data; 

            //INSERT DATA INTO DB SQL SERVER
            string connectionString = "Data Source=utente-pc\SQLEXPRESS;Initial Catalog=MYDB;Persist Security Info=True;User ID=user;Password=sa";
            SqlConnection connection = new SqlConnection(connectionString);

            try
            {

                SqlCommand cmd = new SqlCommand("INSERT INTO PRODUCT (code, name, description) VALUES (@code, @name, @description)", connection);
                cmd.Parameters.AddWithValue("@code", products.code);
                cmd.Parameters.AddWithValue("@name", products.name);
                cmd.Parameters.AddWithValue("@description", products.description);

                connection.Open();
                cmd.ExecuteNonQuery();
            }

            catch
            {
                //Label4.Text = "uh oh";
            }

        }
    }


    public class List
    {

        public string code{ get; set; }
        public string name{ get; set; }
        public string description{ get; set; }
    }






}

I need to implement this request in a project web form asp.net, I’ve try to write the same code but it don’t work, I receved a response null.

This is the code about my page asp.net

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using RestSharp;
using System.Data;
using System.Data.SqlClient;
using System.Threading.Tasks;

namespace WebApplication2
{
    public partial class _Default : Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            RegisterAsyncTask(new PageAsyncTask(GetGizmosSvcAsync));
        }

        public async Task GetGizmosSvcAsync()
        {
            var client = new RestClient("https://api-developer.com/products");
            client.Timeout = -1;
            var request = new RestRequest(Method.POST);
            request.AddHeader("Content-Type", "application/json");
            request.AddHeader("Accept", "application/json");
            request.AddParameter("application/json", "{\"category\": \"all\"}", ParameterType.RequestBody);

            var response2 = await client.ExecuteTaskAsync<List>(request);
            List products= response2.Data; 

            //INSERT DATA INTO DB SQL SERVER
            string connectionString = "Data Source=utente-pc\SQLEXPRESS;Initial Catalog=MYDB;Persist Security Info=True;User ID=user;Password=sa";
            SqlConnection connection = new SqlConnection(connectionString);

            try
            {

                SqlCommand cmd = new SqlCommand("INSERT INTO PRODUCT (code, name, description) VALUES (@code, @name, @description)", connection);
                cmd.Parameters.AddWithValue("@code", products.code);
                cmd.Parameters.AddWithValue("@name", products.name);
                cmd.Parameters.AddWithValue("@description", products.description);

                connection.Open();
                cmd.ExecuteNonQuery();
            }

            catch
            {
                //Label4.Text = "uh oh";
            }
        }
    }

    public class List
    {

        public string code{ get; set; }
        public string name{ get; set; }
        public string description{ get; set; }
    }

}

I’ve set Async=”true” in <%@ page title in my page asp.net

I don’t understand why the same code work good on consoleapp c#, but don’t work on asp.net c#

Thanks for any help!

The scheme of response is:

{
   "code": {
      "type": "string"
    },     
    "name": {
      "type": "string"
    },
    "description": {
      "type": "string"
    }

}

UPDATE:

I’ve read in other forum that there is an incompatibility with the last version of restsharp and framework..
I’ve understand that the consoleapp use CORE.NET and webapp use FRAMEWORK.NET, and there are some different with component and NuGet.. So I’ve try to create a webform and install restsharp v106.1.0 and now it work!!!
So all code was ok, the problem was the update restsharp v106.10.1

Thanks to all for help me and I apologize for the trouble.

How to&Answers:

Without knowing what endpoint response structure, its hard to assist you. But based on name (“https://api-developer.com/products“), am assuming it returns collection of prodcuts. So, try modifying var response2 = await client.ExecuteTaskAsync<List>(request); statement to var response2 = await client.ExecuteTaskAsync<System.Collections.Generic.List<List>>(request);

Or based on your next statement List products = response2.Data; am assuming request returns some object which contains Data which is collection of List objects something like below:

public class RootObject()
{
    public System.Collections.Generic.List<List> Data { get; set; }
}

If that is the case, then try modifying var response2 = await client.ExecuteTaskAsync<List>(request); to var response2 = await client.ExecuteTaskAsync<RootObject>(request);

So, your final GetGizmosSvcAsync method looks like below:

public async Task GetGizmosSvcAsync()
{
    var client = new RestClient("https://api-developer.com/products");
    client.Timeout = -1;
    var request = new RestRequest(Method.POST);
    request.AddHeader("Content-Type", "application/json");
    request.AddHeader("Accept", "application/json");
    request.AddParameter("application/json", "{\"category\": \"all\"}", ParameterType.RequestBody);

    var response2 = await client.ExecuteTaskAsync<RootObject>(request);
    var products = response2.Data;  //List products = response2.Data; 

    //INSERT DATA INTO DB SQL SERVER
    string connectionString = "Data Source=utente-pc\SQLEXPRESS;Initial Catalog=MYDB;Persist Security Info=True;User ID=user;Password=sa";
    SqlConnection connection = new SqlConnection(connectionString);

    try
    {

        SqlCommand cmd = new SqlCommand("INSERT INTO PRODUCT (code, name, description) VALUES (@code, @name, @description)", connection);
        connection.Open();

        foreach (var product in products)
        {
            cmd.Parameters.AddWithValue("@code", product.code);
            cmd.Parameters.AddWithValue("@name", product.name);
            cmd.Parameters.AddWithValue("@description", product.description);
            cmd.ExecuteNonQuery();
        }
    }

    catch
    {
        //Label4.Text = "uh oh";
    }
}