c# - Null Error Reading Connection String -


i getting null reference exception when trying connect database via connectionctrings["mydb"].connectionstring.

this in using statement , same code i've used in many other projects, keeps telling me null reference , don't know why.

the connection string named named correctly in web.config in same project wouldn't expect there permissions issues.

what have missed?

edit: have seen suggested answers, these solved putting string in web.config connection string is.

code: connectionstring in web.config

<connectionstrings>     <add name="mydb" connectionstring="data source=192....; initial catalog=projectdb; integrated security=false; user id=user; password=password;" providername="system.data.sqlclient" /> </connectionstrings> 

function access db

public static company retrievecompany(int id) {     var cmp = new company();     try     {         using (var con = new sqlconnection(configurationmanager.connectionstrings["mydb"].connectionstring))         {             con.open();             using (var cmd = new sqlcommand("retreiveemailproc", con))             {                 cmd.commandtype = commandtype.storedprocedure;                 cmd.parameters.addwithvalue("@companyid", id);                  sqldatareader sdr = cmd.executereader();                 while (sdr.read())                 { // code omitted }             }                     }     }     catch(exception ex)     {      }      return cmp; } 

i think related using configurationmanager instead of webconfigurationmanager. possible code resides in folder different web.config , configurationmanager can't handle inheritance problem.


Comments

Popular posts from this blog

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -