Rigel Kent Carbonel
Rigel Kent Carbonel

Reputation: 440

NullReferenceException Error Database data to table html

i have this statement that highlighted after i debug the program. i don't know how to fix this can someone help me please.

string constr = ConfigurationManager.ConnectionStrings["Server=LUIGEL-PC\\SQLExpress;Database=StudentInfo;Trusted_Connection=Yes"].ConnectionString;

is there a problem with this line? i'm connecting to a localdb.

Upvotes: 2

Views: 43

Answers (1)

Anik Islam Abhi
Anik Islam Abhi

Reputation: 25352

In this line require key name of this value from web.config

ConfigurationManager.ConnectionStrings["Server=LUIGEL-PC\\SQLExpress;Database=StudentInfo;Trusted_Connection=Yes"].ConnectionString;

As it's not found any key name Server=LUIGEL-PC\\SQLExpress;Database=StudentInfo;Trusted_Connection=Yes in ConnectionStrings node, it's throwing NullReferenceException

Try like this

web.config

<connectionStrings>
  <add 
    name="myConnectionString" 
    connectionString="Data Source=serverName;Initial 
    Catalog=Northwind;Persist Security Info=True;User 
    ID=userName;Password=password"
    providerName="System.Data.SqlClient"/>
</connectionStrings>

C#

string connStr = ConfigurationManager.ConnectionStrings["myConnectionString"].ConnectionString;

Upvotes: 2

Related Questions