Link to home
Start Free TrialLog in
Avatar of Annette Wilson, MSIS
Annette Wilson, MSISFlag for United States of America

asked on

App.Config Connection String - Null Exception

Hi!

I have the following settings for my connection string which is set up in App.Config.  I keep getting an error, Null Exception and cannot figure out why.  

Any help will be greatly appreciated.

<appSettings >
    <add key="DBConn" value="Data Source=Server; Initial Catalog=Database; user ID=User; 
                                       password=Password Integrated Security=True"/>
  </appSettings>
  <connectionStrings >
    <add name="DBConn" providerName="System.Data.SqlClient" connectionString="Data Source=Server; Initial Catalog=Database; user ID=User; 
                                       password=Password Integrated Security=True"/> 
  </connectionStrings> 

Open in new window



public List<Parts> GetPartsDetailFromPTNum(string PartNum)
        {
           
            List<Parts> partsList = new List<Parts>();
            var strConnection = ConfigurationManager.ConnectionStrings["DBConn"].ConnectionString;


            SqlConnection myConn = new SqlConnection(strConnection);

            myConn.Open();
            string commandString = "GetECRPartsData";

            SqlCommand myCommand = new SqlCommand(commandString, myConn);
            myCommand.CommandType = CommandType.StoredProcedure;
            myCommand.Parameters.Add("@PartNum", System.Data.SqlDbType.VarChar);
            myCommand.Parameters["@PartNum"].Value =PartNum ;
            
            //SqlDataReader myReader = myCommand.ExecuteReader();
            using (IDataReader myReader = myCommand.ExecuteReader()) 
            
            
            while (myReader.Read())
            
            {
                
                partsList.Add(new Parts() 

            { 
                ID = (myReader.IsDBNull(myReader.GetOrdinal("ID")) ? (string)null : myReader.GetString(myReader.GetOrdinal("ID"))),
                DESCRIPTION = (myReader.IsDBNull(myReader.GetOrdinal("DESCRIPTION")) ? (string)null : myReader.GetString(myReader.GetOrdinal("DESCRIPTION"))),
                ENGINEERING_MSTR = (myReader.IsDBNull(myReader.GetOrdinal("ENGINEERING_MSTR")) ? (string)null : myReader.GetString(myReader.GetOrdinal("ENGINEERING_MSTR"))),
                COMMODITY_CODE = (myReader.IsDBNull(myReader.GetOrdinal("COMMODITY_CODE")) ? (string)null : myReader.GetString(myReader.GetOrdinal("COMMODITY_CODE"))),
                FABRICATED = (myReader.IsDBNull(myReader.GetOrdinal("FABRICATED")) ? (string)null : myReader.GetString(myReader.GetOrdinal("FABRICATED"))),
                PURCHASED = (myReader.IsDBNull(myReader.GetOrdinal("PURCHASED")) ? (string)null : myReader.GetString(myReader.GetOrdinal("PURCHASED"))),
                DRAWING_ID = (myReader.IsDBNull(myReader.GetOrdinal("DRAWING_ID")) ? (string)null : myReader.GetString(myReader.GetOrdinal("DRAWING_ID"))),
                DRAWING_REV_NO = (myReader.IsDBNull(myReader.GetOrdinal("DRAWING_REV_NO")) ? (string)null : myReader.GetString(myReader.GetOrdinal("DRAWING_REV_NO"))),
                //QTY_AVAILABLE_ISS = (myReader.IsDBNull(myReader.GetOrdinal("QTY_AVAILABLE_ISS")) ? (string)null : myReader.GetString(myReader.GetOrdinal("QTY_AVAILABLE_ISS"))),
                USER_1 = (myReader.IsDBNull(myReader.GetOrdinal("USER_1")) ? (string)null : myReader.GetString(myReader.GetOrdinal("USER_1"))),
                HTS_CODE = (myReader.IsDBNull(myReader.GetOrdinal("HTS_CODE")) ? (string)null : myReader.GetString(myReader.GetOrdinal("HTS_CODE"))),
                REVISION_ID = (myReader.IsDBNull(myReader.GetOrdinal("REVISION_ID")) ? (string)null : myReader.GetString(myReader.GetOrdinal("REVISION_ID"))),
                //QTY_ON_HAND = (myReader.IsDBNull(myReader.GetOrdinal("QTY_ON_HAND")) ? (string)null : myReader.GetString(myReader.GetOrdinal("QTY_ON_HAND"))),
                USER_2 = (myReader.IsDBNull(myReader.GetOrdinal("USER_2")) ? (string)null : myReader.GetString(myReader.GetOrdinal("USER_2"))),
                USER_6 = (myReader.IsDBNull(myReader.GetOrdinal("USER_6")) ? (string)null : myReader.GetString(myReader.GetOrdinal("USER_6"))),
             
               
            

            });
                        

            }
            return partsList;

            myConn.Close();

            }

Open in new window

Avatar of Sreedhar Vengala
Sreedhar Vengala
Flag of Australia image

Can you post inner text of your exception.
There might be some issue in your app.config file. If there is any error(syntax error or anything) in app.Config file and then it will not load config file and you will get null reference. So I would suggest please check your config file.
Avatar of Carl Tawn
Getting a NullException at which point?
Avatar of Annette Wilson, MSIS

ASKER

I have attached the error screen.

Thank you for your help.
ConnectionStringError.JPG
Ok, assuming you have included a reference to the System.Configuration assembly, and have an using statement for the System.Configuration namespace, then there is nothing fundamentally wrong with your code.

Where in your config file is the connectionStrings section defined? Relative to the top of the document i mean - it might help if you can post the whole config file (with any sensitive information redacted of course).
This is my app.config file contents:

<?xml version="1.0" encoding="utf-8"?>
<configuration>
  
    <system.serviceModel>
      <behaviors>
        <serviceBehaviors>
          <behavior name="debug">
            <serviceDebug includeExceptionDetailInFaults ="true"/>
          </behavior>
        </serviceBehaviors>
       
      </behaviors>
        <bindings />
        <client />
      <services>
        <service name="ECN_ECRWCFService" behaviorConfiguration ="debug"/>
      </services>
    </system.serviceModel>
  
  <connectionStrings >
    <add name="DBConn" providerName="System.Data.SqlClient" connectionString="Data Source=Server; Initial Catalog=ONTIC; user ID=username; password=password"/> 
  </connectionStrings>
  <appSettings >
    <add key="DBConn" value="Data Source=server; Initial Catalog=ONTIC; user ID=userid; password=password"/>
  </appSettings>
</configuration>

Open in new window



There server name has a '\' in it. Does that make a difference?
the password has an '!' in it.  does that make a difference
if the backslash is a problem, do you think I should put this in an external settings file?
If so, How would I write the code?
I originally had the connection string defined in the webservice but had to move it because the production server has a backslash in the name and it was causing an error.

This is for SharePoint.  should I configure the configuration connection string differently?
You'd normally need to escape the backslash by using 2 slashes. Are youable to read anything from your config, or is it just the connection strings?
It looks like it's just the connection string.
Dear

Your App Config file you have :
 <connectionStrings >
    <add name="DBConn" providerName="System.Data.SqlClient" connectionString="Data Source=Server; Initial Catalog=ONTIC; user ID=username; password=password"/> 
  </connectionStrings>
  <appSettings >
    <add key="DBConn" value="Data Source=server; Initial Catalog=ONTIC; user ID=userid; password=password"/>
  </appSettings>

Open in new window


You are using DBConn in two sections connectionStrings and appSettings,  rename your connectionString name to DBConn1 and do the same in your code and try again.

Good Luck
Ammar,
I tried renaming and it did not work.  I changed the connection string name in the config and where it is referenced in the service.  It still did not work.
It wouldn't make any difference, because it is perfectly valid to have two entries with the same name in different sections of the config.

You need to go back to basics, and start by checking if you can read anything from the config at all.... it may be that it isn't hooked up properly for some reason, so isn't seeing the config file, rather than just not seeing the connectionStrings section.
ASKER CERTIFIED SOLUTION
Avatar of Ammar Gaffar
Ammar Gaffar
Flag of United Arab Emirates image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I check the event viewer and it appears that I needed to set up Alternate Access Mapping.  I will let you know the results shortly.