user5648283
user5648283

Reputation: 6203

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.

Upvotes: 8

Views: 11834

Answers (5)

estinamir
estinamir

Reputation: 503

Kindly replace this line:

cmd.CommandType = CommandType.StoredProcedure;

With this:

cmd.CommandType = System.Data.CommandType.StoredProcedure;

Upvotes: 0

Sanjit Paudel
Sanjit Paudel

Reputation: 1

Add using System.Data on the top of page or just click on commandtype - there you can see icon like a bulb, click on it and add using System.Data;

Upvotes: -1

Namdev Karande
Namdev Karande

Reputation: 81

you have missed the namespace. add below line.

using System.Data;

it will solve your problem.

Upvotes: 1

Imad
Imad

Reputation: 7490

For these kind of errors, right click on the error word and say 'Resolve'. All the errors will be if defining assembly is referenced in your project.

Upvotes: 1

Christos
Christos

Reputation: 53958

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

using System.Data;

Upvotes: 15

Related Questions