0

To initialize the connection string, it needs to be a static string. Why that happens?

using System.Data.SqlClient;

namespace CDemoLib
{
    public class Connecting
    {
        static string conString = @"connection string here";

Also when I am trying to open a connection using the conn.Open()am getting an error saying that

The name conn.Open() does not exist in the current context

Why that error occurs ?

using System.Data.SqlClient;

namespace CDemoLib
{
    public class Connecting
    {
         static string conString = @"connection string here";

    SqlConnection conn = new SqlConnection(conString);
    conn.Open();
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Christos Michael
  • 1,003
  • 2
  • 10
  • 16
  • 6
    It should be inside a method! – Salah Akbari Aug 04 '18 at 17:44
  • @S.Akbari, I did that and I want to call it to a different namespace and class in order to open a connection to the db. However, after creating the new object, when am trying to call the method that opens the sql connection, again am seeing the "The name conn.Open() does not exist in the current context" error message – Christos Michael Aug 04 '18 at 17:48
  • 2
    In your code above you are calling `conn.Open();` inside the body of the *class*. That is syntactically wrong and you will get the *compile* time error you mentioned above. You need to call this inside of a *method*. – Igor Aug 04 '18 at 18:45
  • @Igor I did that and I want to call it to a different namespace and class in order to open a connection to the db. However, after creating the new object, when am trying to call the method that opens the sql connection, again am seeing the "The name conn.Open() does not exist in the current context" error – Christos Michael Aug 04 '18 at 19:08
  • **No you didn't**. Look at your code above that you *posted in your question*. There is no method. – Igor Aug 04 '18 at 19:09
  • All 3 lines are found in `public class Connecting` <= emphasis on **class**. **There is no method in your code above** – Igor Aug 04 '18 at 19:12

1 Answers1

2

I have anwsered this question but i will replay it for you. (Connection String Is not Working Object instance reference is null)

Put your connection string at you web.config as the foloow example:

<add name="MovieDB"
     connectionString="Data Source=LocalDb)\MSSQLLocalDB;Initial Catalog=aspnet- MvcMovie;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|\Movies.mdf"     
     providerName="System.Data.SqlClient"/>

to read it within your code:

System.Configuration.Configuration rootWebConfig =System.Web.Configuration.WebConfigurationManager.OpenWebConfiguration("/MyWebSiteRoot");
    System.Configuration.ConnectionStringSettings connString;
    if (rootWebConfig.ConnectionStrings.ConnectionStrings.Count > 0)
    {
        connString =
            rootWebConfig.ConnectionStrings.ConnectionStrings["MovieDB"];
        if (connString != null)
            Console.WriteLine("MovieDB connection string = \"{0}\"",
                connString.ConnectionString);
        else
            Console.WriteLine("No MovieDB connection string");
    }

The name at your web.config tag 'name'

<add name="MovieDB".....

has to be the same one from your c# code:

connString = rootWebConfig.ConnectionStrings.ConnectionStrings["MovieDB"]

from: https://msdn.microsoft.com/en-us/library/ms178411.aspx

Here a method to open it:

private static void OpenSqlConnection(string connString)
{
    using (SqlConnection connection = new SqlConnection(connString))
    {
        connection.Open();
        Console.WriteLine("ServerVersion: {0}", connection.ServerVersion);
        Console.WriteLine("State: {0}", connection.State);
    }
}

from: https://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlconnection.open(v=vs.110).aspx

Don´t forget to secure your connection string at your web.config. Please, read this: https://msdn.microsoft.com/en-us/library/ms178372.aspx

Sergio Rezende
  • 762
  • 8
  • 25