Click here to Skip to main content
15,121,549 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I created an app.config file and put the datasource in there incase it changes in the near future and i keep getting the null reference exception was unhandled.

use the new keyword as an object instance

but i have already imported the using system.configuration

and yet still. i have tried to catch but it seems not to work

C#
public partial class LoginForm : Form
    {
        //Main mnFrm;
        SqlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConn"].ToString());//the exception is thrown on this line when I run the program
        
        SqlDataReader sdr;
        SqlCommand sqlCmd = new SqlCommand();

        public LoginForm(Main mnFrm)
        {
            //this.mnFrm = mnFrm;
            InitializeComponent();
        }


I would appreciate the help guys thanks in advance
Posted
Comments
MuhammadUSman1 26-Jun-13 1:48am
   
I added solution please check it.

   
v2
Comments
JoelonJada 26-Jun-13 8:08am
   
Thank you very much for that article. I will ask questions later.
C#
qlConnection sqlConn = new SqlConnection(ConfigurationManager.ConnectionStrings["DbConn"].ToString());//the exception is thrown on this line when I run the program


in this line you write Key "DbConn" dose not exist in your config file are you write wrong spell there.
   
Comments
JoelonJada 26-Jun-13 1:57am
   
that is the variablename for the connectionstring in the app.config file for which ever database it is to connect to in future
MuhammadUSman1 26-Jun-13 2:01am
   
Please post your config file code here, or you make sure that the spell you using in Code "DbConn" is same in your config file
JoelonJada 26-Jun-13 4:37am
   
<configuration>
<connectionstrings>
<add name="DbConn" providername="System.Data.SqlClient" connectionstring=" Data Source=popsy-pc; Initial Catalog=GEMSystem; Integrated Security=True; ">

MuhammadUSman1 26-Jun-13 6:42am
   
What is this?
JoelonJada 26-Jun-13 7:47am
   
Sorry I can't seem to figure out the tag for posting codes here...if you do please show me how
MuhammadUSman1 27-Jun-13 0:23am
   
If problem didn't solved yet. then please Past code of your config file. with dummy data.
Do you have a connection called "DbConn" in the ConnectionStrings section of your config file?
   
Comments
JoelonJada 26-Jun-13 1:44am
   
yes please i do
CHill60 26-Jun-13 8:56am
   
If you need further clarification from the OP please use the "Have a question or Comment?" link, don't post questions as Solutions
_Damian S_ 26-Jun-13 19:45pm
   
It wasn't a search for clarification... I was pointing the OP in the likely direction of his error... I prefer to guide people towards the answer rather than simply handing it to them on a plate.
You can add a settingsfile to your solution and set the connectionstring there.
Then you can acces the connectionstring from the settingfile.

C#
using (SqlConnection sett = new SqlConnection(Settings.Default.Setting))
          {
            sett.open();
           //your code here;
          }
   
v2
Comments
JoelonJada 26-Jun-13 1:46am
   
Should i state this sett ing file as a global variable. Because i get the message the setting does not exist in the current context
Tom Wauters 26-Jun-13 2:51am
   
If you specifie it with a name, type as connectionString,scope: Application, and value as the connectionstring you want to use, you can just acces it as setting.Default."theNameofYourConnectionString"

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900