1

I'm new to ASP.NET. I have done Json parsing when returning the data in WebAPI controller like this, `

public JsonResult getResult()
        {
            string dataconfig = Convert.ToString(ConfigurationManager.ConnectionStrings["Onviodb"]);
            SqlConnection cnn = new SqlConnection(dataconfig);
            SqlCommand cmd = null;
            String sqlQuery = null;
            SqlDataReader dataReader = null;

            var list = new List<onvioAPI>();
            try
            {
                cnn.Open();
                sqlQuery = "Select * from tbl_Projects";
                cmd = new SqlCommand(sqlQuery, cnn);
                dataReader = cmd.ExecuteReader();
                while (dataReader.Read())
                {
                    onvioAPI od = new onvioAPI();
                    //od.id = Int32.Parse(dr["ID"].ToString());
                    od.aname = Convert.ToString(dataReader["Agency Contact Name"]);
                    od.vname = Convert.ToString(dataReader["Vendor Name"]);
                    od.pcity = Convert.ToString(dataReader["Project City"]);
                    od.pstate = Convert.ToString(dataReader["Project State"]);
                    od.vcity = Convert.ToString(dataReader["Vendor City"]);
                    list.Add(od);

                }

                cnn.Close();
            }
            catch (Exception ex)
            {
                Console.WriteLine("Can not open connection ! " + ex);
            }

            return Json(list,JsonRequestBehavior.AllowGet);
        }

`

But there is an error showing like The best overloaded method match for Json(List,JsonSerializerSettings) has some invalid aruguments. What should I do ? Thanks in advance

0

2 Answers 2

1

It happened because there are two JsonResult types: one from System.Web.Mvc namespace(with enum JsonRequestBehavior) and one from System.Web.Http.Results(JsonResult<T>) and I think you mixed up them. Solution is to do this:

using System.Web.Http;
//using System.Web.Mvc;

    public IHttpActionResult getResult() 
    {
        //your code..
        var list = new List<onvioAPI>();
        //your code..
        return Json(list);
    }
Sign up to request clarification or add additional context in comments.

Comments

0

Try this,

List<onvioAPI> list = new List<onvioAPI>();

This should work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.