-1

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();
6
  • 6
    It should be inside a method! Commented Aug 4, 2018 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 Commented Aug 4, 2018 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. Commented Aug 4, 2018 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 Commented Aug 4, 2018 at 19:08
  • No you didn't. Look at your code above that you posted in your question. There is no method. Commented Aug 4, 2018 at 19:09

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

14 Comments

Put your connection string at you web.config as the foloow example: In which tag, sytstem.web, system.webServer or system.codedom ?\
Also, i've tried to put it under system.web -> httpModules, and am getting the error The conncection string attribute is not allowed. Same for providerName
inside <configuration > tag look for <connectionStrings> tag and if you are not seeing it just add it. Please let me know with you get it done! learn.microsoft.com/en-us/aspnet/mvc/overview/getting-started/…
I added connectionStrings, however System.Configuration.Configuration does not exists
so try this : SqlConnection con = new SqlConnection( WebConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString); Namespace: using System.Web.Configuration;
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.