Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CS0103 C# The name 'Json' does not exist in the current context

Well,

I have written JsonResult funtion. But i get the following error:

CS0103 C# The name 'Json' does not exist in the current context

I cant find solution to it... plzz help?

public JsonResult DoUserExist(string Emailaddress)
{
    bool ch = false;
    string connectionString = ConfigurationManager.ConnectionStrings["FreelanceDBCS"].ConnectionString;

    using (SqlConnection con = new SqlConnection(connectionString))
    {
        SqlCommand cmd = new SqlCommand("GetCities", con);
        cmd.CommandType = System.Data.CommandType.StoredProcedure;
        con.Open();
        SqlDataReader rdr = cmd.ExecuteReader();


        if (rdr != null)
        {
            ch = true;
        }
    }
    return Json(ch,JsonRequestBehavior.AllowGet);

}
like image 458
Ahmed Sazar Avatar asked Jul 08 '16 17:07

Ahmed Sazar


People also ask

How do I fix error code CS0103?

CS0103 is caused when you are using a name for a variable or method that does not exist within the context that you are using it. In order to fix the CS0103 error you will need to correct the name of the variable or method from where it is declared or referenced.

What is error CS0103?

An attempt was made to use a name that does not exist in the class, namespace, or scope. Check the spelling of the name and check your using directives and assembly references to make sure that the name that you are trying to use is available.

How do I fix cs0234?

To fix this error, simply update the example . csproj file to use a previous System. CommandLine version. Note that this package is only used to parse the options for the example.


2 Answers

I got it !

I have forgotten to derive my class from Controller class

public class User: Controller
{

}
like image 189
Ahmed Sazar Avatar answered Sep 17 '22 11:09

Ahmed Sazar


Page must be derived from Controller class to Json () and JsonResult() part by using Microsoft.AspNetCore.Mvc;

If you are using Razor page model then you can face this issue. I got it after too much time spending.

You can also change your page derived from Controller instead of pagemodel like this public class User: Controller

like image 21
rishabh kumawat Avatar answered Sep 18 '22 11:09

rishabh kumawat