Search code examples
c#sqlasp.net.netasp.net-mvc

The named 'CommandType' does not exist in the current context


Can someone help me understand why I'm getting that error here?

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Threading.Tasks;
using System.Data.SqlClient;
using System.Configuration;

namespace JsPractice.Controllers
{
    public class SolutionController : Controller
    {

        public ActionResult Index ( )
        {

            return View();
        }

        [HttpPost]
        public ActionResult CreateNew ( int problem_id, string solver, string solution_code, string test_code )
        {
            // Going to move this to controller later .. 
            using ( SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalJsPracticeDb"].ConnectionString) )
            {
                using ( SqlCommand cmd = new SqlCommand("AddSolution", con) )
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@problem_id", problem_id);
                    cmd.Parameters.AddWithValue("@solver", solver);
                    cmd.Parameters.AddWithValue("@solution_code", solution_code);
                    cmd.Parameters.AddWithValue("@test_code", test_code);
                    con.Open();
                    cmd.ExecuteNonQuery();
                }

            }
            return View();
        }

    }
}

According to the documentation https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.commandtype(v=vs.110).aspx I don't see what I'm doing wrong, as I've included System.Data.SqlClient.


Solution

  • You have missed to include the System.Data namespace. So adding the following, you will solve your problem.

    using System.Data;