0

I have a problem about the connection strings at web.config

below is the connection string on my web.config

<connectionStrings>
    <add name="ConnectionString" connectionString="Data Source=something\SQLEXPRESS; Initial Catalog=myDB; User Id=username; Password=password;" />
  </connectionStrings>

that value I stored in a static class and which recorded perfectly

but when the process at the class of my data access layer class

executing the method it got the error

Object reference not set to an instance of an object.

when executing the

SqlConnection con = null;
con.ConnectionString = GetConnectionString();

the ConnectionString Method

public string GetConnectionString()
        {

                string connectionString = Config_Key.ConnectionString;

                return connectionString;

        }

it returns the exact value of connection string from web.config

I debugged those already

dunno whats wrong about it

  • Why do you expect `SqlConnection con = null; con.ConnectionString = ...` to work? – Scott Chamberlain Apr 17 '17 at 14:16
  • You set `con = null` and then proceed to try and access a property on that `null` object. You see the problem there? – Nkosi Apr 17 '17 at 14:16
  • yeah sure, forgive my retardness thx mate – This lowly newb Apr 17 '17 at 14:20
  • Create a connection first: `IDbConnection con = new SqlConnection(GetConnectionString())`. Then open it: `con.Open();`. Then use it to create an SQL command: `IDbCommand cmd = con.CreateCommand();`. Set your query: `cmd.CommandText = "SELECT * FROM Customers";`. Then execute the command: `IDataReader reader = cmd.ExecuteReader();`. Read and process the data. And don't forget to close the connection when you're done with it. – Anatoliy Pidlubnyy Apr 17 '17 at 14:35

0 Answers0