Click here to Skip to main content
15,889,808 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm new to WebAPI but have done MVC in the past. I created 2 projects WebServiceAlpha that houses the "backend" and EF etc, and WebServiceBeta that "uses" the Alpha.
In WebServiceBeta, I'm trying to create a data access layer so I created a class called DAL and it's very simple. I have the SqlConnection, but any attempt to code anything else results in "The name j does not exist in the current context".
All I'm trying to do is to connect to the database.

What I have tried:

namespace WebServiceBeta
{    
    public class DAL
    {
        SqlConnection j = new SqlConnection(@"data source=***;initial catalog=CRUDDB;User id=***;password=***;MultipleActiveResultSets=True");
        j.  // The name 'j.' does not exist in the current context
    }
}


On the controller; however, the connection works fine:
public ActionResult Index( FormCollection frm )
 {
     SqlConnection j = new SqlConnection(@"data source=**;initial catalog=CRUDDB;User id=**;password=**;MultipleActiveResultSets=True")
     //Enumerable<mvcEmployeeModel> empList;
     j.Open();  // THIS WORKS FINE
     if ( j.State==ConnectionState.Open)
     {
         ViewBag.CnState = "Yea - I'm open!";
     }


Is there something else I'm missing? I can't imagine it being a "using" issue, I've added System.Data.SqlClient and System.Data.
Posted
Updated 28-Sep-18 10:33am

1 solution

Your trying to write code at the class level instead of in methods in the class. That's not going to work.

In your controller, you're putting the code in the method Index. That's not true in your DAL class.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900