You must Sign In to post a response.
  • Category: Webservices

    Connection string is not retreiving from Web.Config file

    Hai Friends,
    I am creating a web service and for the connection string, i m keeping in the Configuration file- Web.Config as below:

    <connectionStrings>
    <add connectionString="NBAConnectionString" name="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=NEWBQA;Data Source=NLGTNBASQL09"/>
    </connectionStrings>

    Now I am writing the code to access this connection string in my Service.asmx.cs as shown below:

    using (sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["NBAConnectionString"].ConnectionString.ToString()))
    {
    }

    while execution, its displaying the "object reference doesn't set to instance of objects" message.
    I am trying to consume this service from a Console application. So while keeping the breakpoint, its not accessing any element of the Web.Config file.
    Even i kept few key value pair under the AppSettings, those also not accessible.
    Any suggestion please.
  • #673610
    which line gives you error pawan ?
    as error message suggest, may be some of your oject has NULL value

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #673612
    Hi,


    What I can see in your web.config file is that you assign connectionString to name value and vise verse.

    May be it's just a typo, but it should be something like below:


    <connectionStrings>
    <add name="NBAConnectionString" connectionString ="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=NEWBQA;Data Source=NLGTNBASQL09"/>
    </connectionStrings>

    Hope it'll help you.
    Regards
    Ajatshatru

  • #673613
    Still I am getting the same error message "Object reference doesn't set to instance of object".
    Why its getting the null value?? Why its not able to read the Web.Config file?
    Also its not abel to read the AppSettings key value pair which i mentioned like below:

    <appSettings>
    <add key="LogMode" value="on"/>
    <add key="ErrorLogFile" value="ErrorLog.txt"/>
    </appSettings>

    Any suggestions?

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #673615
    Hi,

    Did you try hardcoding the connection string to see if it is working?
    You can also read MSDN below which describes - "How to: Read Application Settings from the Web.config File"

    http://msdn.microsoft.com/en-us/library/610xe886.aspx

    Hope it'll help you.
    Regards
    Ajatshatru

  • #673618
    Hi,

    Please try the following. This may solve your problem

    Code:

    string cnstr = ConfigurationManager.ConnectionStrings["NBAConnectionString"].ConnectionString;

    web config:

    <connectionStrings>

    <add name="NBAConnectionString" connectionString="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=NEWBQA;Data Source=NLGTNBASQL09" providerName="System.Data.SqlClient"/>

    </connectionStrings>

  • #673621
    Hi,

    Also, It may be the typo issue. Please include the object name for sqlconnection as mentioned below

    using (sqlConnection cn = new SqlConnection(ConfigurationManager.ConnectionStrings["NBAConnectionString"].ConnectionString.ToString())){}

    Asath L

  • #673623
    Hai Asath,
    Yes I already tried with all the above but its not working at all..
    Then I tried to get the connection string using the below snippet:

    ConfigurationManager.ConnectionStrings[0].ConnectionString...

    Its getting the below string:

    data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|aspnetdb.mdf;User Instance=true

    It seems this is the default one which gets generated automatically.
    And not reading the one which I have mentioned there in web.config file.
    Then I thought to see what all the connection strings it's taking then I found that only above one its fetching.
    Any clue?

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com

  • #673631
    Please do not provide any commented line of extra space in the connection string section of the web config file.

    Sometime this may be cause of the error.

    Thanks & Regards
    Anil Kumar Pandey
    Microsoft MVP, DNS MVM

  • #673637
    Hi Pawan,



    <connectionStrings>
    <add name="NBAConnectionString" ConnectionString="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=NEWBQA;Data Source=NLGTNBASQL09"/>
    </connectionStrings>

    In ur code behind,

    using System.configuration;

    SqlConnection con=new SqlConnection(ConfigurationManager.ConnectionStrings["NBAConnectionString"].ConnectionString);





    Hope this will help you

    Regards,
    SonyShiva
    Never lose hope..You never know what tomorrow will bring

  • #673651
    Hi pawan,


    please follow the setting.
    It's working. for example

    <add name="NBAConnectionString" connectionString="Data Source=HOME-2A9269EF8E\SQLEXPRESS;Initial Catalog=Alkeep; Integrated Security=True;"
    providerName="System.Data.SqlClient" />


    and it code behind cs you used as following.

    SqlConnection con=new SqlConnection();
    con.ConnectionString=ConfigurationManager.ConnectionStrings["NBAConnectionString"].ConnectionString;


    Hope Now you will gloom out from your error.

    Regards
    Krishna Kant

  • #673665
    It seems connection string name may be repeated in web.config and check otherwise change connection string name like this and add provider name also in connection string


    <connectionStrings>
    <add name="constr" connectionString="Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=NEWBQA;Data Source=NLGTNBASQL09" providerName="System.Data.SqlClient"/>
    </connectionStrings>


    Get it in C# code like this

    SqlConnection sqlcon = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString);


    Put breakpoint verify it the above connection value is return

    Regards
    N.Ravindran
    Your Hard work never fails

  • #673868
    hi,

    web config file:

    <appSettings>
    <add key="constr" value="Data Source=WTSSQL\WTSSQL;Initial Catalog=REG;User ID=sa;Password=Webtek123"/>
    </appSettings>



    code behind:

    using System.Data.SqlClient;

    string str=ConfigurationManager .AppSettings ["constr"].ToString ();
    SqlConnection ocon = new SqlConnection(str);
    thanks,
    raja..

  • #673912
    How to display horizontal scroll bat

  • #673923
    Hi Lilian,



    If you hav any doubts post as a seperate post not as a reply to other posts.
    So that all the members can try to help you.

    Regards,
    SonyShiva
    Never lose hope..You never know what tomorrow will bring

  • #674242
    Hai Guys,
    Thanks for the response.
    The issue was that I created a Web Service using the Visual Studio 2008 (.Net 3.5v) and then I tried to consume it by using the Console application. Actually in the .Net 3.5, we don't have the option to add the Web Reference. So i added it as the Service Reference. It means that it takes as the WCF service and added the end points to my console application app.config file.
    Now when I try to get the settings from the Config file, it won't be able to read it.
    Now I deleted the console application and added the console application by selecting the .Net 2.0 framework. I got the Add web reference option and added it.
    Its working fine now.
    Thanks a lot for all your effort of providing the probable answers.

    Regards,
    Pawan Awasthi(DNS MVM)
    +91 8123489140 (whatsApp), +60 14365 1476(Malaysia)
    pawansoftit@gmail.com


  • This thread is locked for new responses. Please post your comments and questions as a separate thread.
    If required, refer to the URL of this page in your new post.