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
  • 704,970
  • 168
  • 1,303
  • 1,425
Christos Michael
  • 953
  • 2
  • 8
  • 15
  • 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

Just Fair
  • 647
  • 7
  • 21
  • Put your connection string at you web.config as the foloow example: In which tag, sytstem.web, system.webServer or system.codedom ?\ – Christos Michael Aug 04 '18 at 19:03
  • 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 – Christos Michael Aug 04 '18 at 19:10
  • inside tag look for tag and if you are not seeing it just add it. Please let me know with you get it done! https://docs.microsoft.com/en-us/aspnet/mvc/overview/getting-started/introduction/creating-a-connection-string – Just Fair Aug 04 '18 at 19:14
  • I added connectionStrings, however System.Configuration.Configuration does not exists – Christos Michael Aug 04 '18 at 19:17
  • so try this : SqlConnection con = new SqlConnection( WebConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString); Namespace: using System.Web.Configuration; – Just Fair Aug 04 '18 at 19:21
  • still WebConfigurationManager, does not exists – Christos Michael Aug 04 '18 at 19:23
  • within your c# code are you using the Namespace: using System.Web.Configuration? – Just Fair Aug 04 '18 at 19:25
  • How are you doind so far ? – Just Fair Aug 04 '18 at 19:32
  • using System.Web.Configuration; does not exist in the namespace System.Web. – Christos Michael Aug 05 '18 at 19:00
  • Please, go to the Reference of your project in Solution Explorer, right click and choose from the list: Assemblies > Framework the System.Web namespace. Check it and hit Ok and let me know if this resolve your problem. – Just Fair Aug 05 '18 at 19:34
  • I don't see Assemblies option when I right click on Reference of my project – Christos Michael Aug 07 '18 at 17:48
  • I've found the System.Web under References, however there is no option to check it or something. – Christos Michael Aug 07 '18 at 17:51
  • Which version of Visual Studio are you using ? – Just Fair Aug 08 '18 at 18:06
  • Well in this case could be a problem in adding references in your VS 2017 edition. PLease see this: https://stackoverflow.com/questions/45007007/i-cant-add-reference-in-visual-studio-2017. Let me know if this solves your problem adding references into your porject. – Just Fair Aug 10 '18 at 00:36