Click here to Skip to main content
15,886,689 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hi I wish to know how to define a connection string and use it successfully in my App. The Idea is to define once and use as required.
I followed the advice here :

How to get Connection String from App.Config in C#[^]

with little success. when i run my test app, it runs ok but when i ask it to insert sole values into the test database I created for the occasion i have an error message:

" Object reference is not defined to an instance of the object " at the level of the statement
var connectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;


What should I do to get this situation sorted out please ?

What I have tried:

1. First I added this code in the config.app file:

<connectionStrings>
    <add name= "con" 
         providerName= "System.Data.SqlClient" 
         connectionString="Data Source=.\sqlexpress;Initial Catalog=practice;Integrated Security=True" />
    </connectionStrings>


2. Next I Added a reference to System.Configuration

3. The click event of my button control has the code :

var connectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
            
            string ins = @"insert into employee(name, address, phonenum, profession) values(@nam, @add, @phonen, @job)";

                SqlCommand cmd = new SqlCommand(ins, con);
                SqlParameter NM = new SqlParameter();
                NM.ParameterName = "nam";
                NM.Value = nametxt.Text;

                SqlParameter AD = new SqlParameter();
                AD.ParameterName = "add";
                AD.Value = addresstxt.Text;

                SqlParameter PN = new SqlParameter();
                PN.ParameterName= "phonen";
                PN.Value = telephonetxt.Text;

                SqlParameter PF = new SqlParameter();
                PF.ParameterName = "job";
                PF.Value = professiontxt.Text;

                cmd.Parameters.Add(AD);
                cmd.Parameters.Add(PN);
                cmd.Parameters.Add(NM);
                cmd.Parameters.Add(PF);
                con.Open();
                cmd.ExecuteNonQuery();
                MessageBox.Show("Values Entered", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
                con.Close();
Posted
Updated 8-Mar-17 3:32am
Comments
F-ES Sitecore 6-Mar-17 5:40am    
Make sure you're updating the correct app.config file, especially if you have multiple projects in your solution.
Nganku Junior 6-Mar-17 7:09am    
I have a single project in my Solution
Karthik_Mahalingam 6-Mar-17 5:41am    
have tested your code, it works fine..
you might be missing something
PeejayAdams 6-Mar-17 5:56am    
Yes, that looks fine to me, too. Make sure your <connectionstrings> segment is within the <configuration> tab and as F-ES suggests make sure you're dealing with the correct app.config file.
Nganku Junior 6-Mar-17 6:47am    
Comments duly noted. I'll go through it again. Thanks !

1 solution

Hey Guys, I found the little thing that was lacking:
This actually did it:

private void btn_enter_Click(object sender, EventArgs e)
{
var connectionString = ConfigurationManager.ConnectionStrings["con"].ConnectionString;
 
SqlConnection cn= new SqlConnection(connectionString); //This is all I had to add
string ins = @"insert into employee(name) values(@nam)";

SqlCommand cmd = new SqlCommand(ins, cn);//Brought in a few modifications 
SqlParameter NM = new SqlParameter();
NM.ParameterName = "nam";
NM.Value = txtboxname.Text;
cmd.Parameters.Add(NM);
cn.Open();
cmd.ExecuteNonQuery();
MessageBox.Show("Values Entered", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
cn.Close();
 
Share this answer
 

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