Load connection string from app.config

It just says Instance failure

Page 1 of 1

8 Replies - 17438 Views - Last Post: 19 March 2010 - 04:31 AM Rate Topic: -----

#1 Guest_Dylan*


Reputation:

Load connection string from app.config

Posted 17 March 2010 - 02:41 PM

Hello to all. What I am trying to do here is load a connection string from my app.config file and open a connection to a sql database so that I can list all the rows. I have been trying to figure this out for a few days now. I just feel like i have hit a brick wall. Ill show what I have so far.

App.Config

<?xml version='1.0' encoding='utf-8'?>
<configuration>
<connectionStrings>
<clear />
<add name="sqlcon"
providerName="System.Data.SqlClient"
connectionString="Data Source=pei-a1162\\sqlexpress;Initial Catalog=InventAssist;User ID=test;Password=password;" />
</connectionStrings>
</configuration>

List_Records.cs

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace WindowsFormsApplication1
{
public partial class List_Record : Form
{
public List_Record()
{
InitializeComponent();
}

System.Data.SqlClient.SqlConnection con;

private void button1_Click(object sender, EventArgs e)
{
this.Close();
}

private void button2_Click(object sender, EventArgs e)
{
string mycon = ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;
con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = mycon;
con.Open();

//Create new DataAdapter
using (SqlDataAdapter a = new SqlDataAdapter(
"SELECT * FROM Table1", con))
{

// Use DataAdapter to fill DataTable
DataTable t = new DataTable();
a.Fill(t);

// Render data onto the screen
dataGridView1.DataSource = t;
}
con.Close();
}

private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
{

}
}
}


what happens is that every time I run this and I press the button to execute the code the application crashes and con.Open(); is marked and I'm told there was an instance failure. Any help will be greatly appreciated.


-Dylan

Is This A Good Question/Topic? 0

Replies To: Load connection string from app.config

#2 Gonzilla  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 16-March 10

Re: Load connection string from app.config

Posted 17 March 2010 - 02:49 PM

First, please use the code tag, it makes it easier to read the code. Second, could you post a more detailed error message?

This post has been edited by Gonzilla: 17 March 2010 - 02:50 PM

Was This Post Helpful? 0
  • +
  • -

#3 Guest_Dylan*


Reputation:

Re: Load connection string from app.config

Posted 17 March 2010 - 02:51 PM

how do I use a code tag?
Was This Post Helpful? 0

#4 Gonzilla  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 23
  • Joined: 16-March 10

Re: Load connection string from app.config

Posted 17 March 2010 - 02:54 PM

Just look at the bottom of this site :)
Was This Post Helpful? 1
  • +
  • -

#5 redsys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-March 10

Re: Load connection string from app.config

Posted 17 March 2010 - 02:59 PM

Hopefully this does it. When the error box pops up it says InvalidOperationException was unhandled. Underneath that it says Instance Failure. If i continue to click on "View Detail" it doesn't give me much, but it does say that the source was in "System.Data".





App.Config
<?xml version='1.0' encoding='utf-8'?>
<configuration>
  <connectionStrings>
    <clear />
    <add name="sqlcon"
     providerName="System.Data.SqlClient"
     connectionString="Data Source=pei-a1162\\sqlexpress;Initial Catalog=InventAssist;User ID=test;Password=password;" />
  </connectionStrings>
</configuration>



List_Record.cs
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;

namespace WindowsFormsApplication1
{
    public partial class List_Record : Form
    {
        public List_Record()
        {
            InitializeComponent();
        }

        System.Data.SqlClient.SqlConnection con;

        private void button1_Click(object sender, EventArgs e)
        {
            this.Close();
        }
       
        private void button2_Click(object sender, EventArgs e)
        {
            string mycon = ConfigurationManager.ConnectionStrings["sqlcon"].ConnectionString;
            con = new System.Data.SqlClient.SqlConnection();
            con.ConnectionString = mycon;
            con.Open();

                 //Create new DataAdapter
            using (SqlDataAdapter a = new SqlDataAdapter(
                "SELECT * FROM Table1", con))
            {
                
                // Use DataAdapter to fill DataTable
                DataTable t = new DataTable();
                a.Fill(t);

                // Render data onto the screen
                dataGridView1.DataSource = t;
            }
            con.Close();
        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#6 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Load connection string from app.config

Posted 17 March 2010 - 03:14 PM

In your connection string replace Data Source=pei-a1162\\sqlexpress with Data Source=pei-a1162\sqlexpress
Was This Post Helpful? 0
  • +
  • -

#7 redsys  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 17-March 10

Re: Load connection string from app.config

Posted 17 March 2010 - 04:11 PM

View PostPsychoCoder, on 17 March 2010 - 02:14 PM, said:

In your connection string replace Data Source=pei-a1162\\sqlexpress with Data Source=pei-a1162\sqlexpress

Thank you so much! Iv been beating my head against a wall for several days now........thank you thank you thank you!
Was This Post Helpful? 0
  • +
  • -

#8 PsychoCoder  Icon User is offline

  • Google.Sucks.Init(true);
  • member icon

Reputation: 1641
  • View blog
  • Posts: 19,853
  • Joined: 26-July 07

Re: Load connection string from app.config

Posted 17 March 2010 - 04:25 PM

No problem, glad I could help :)
Was This Post Helpful? 0
  • +
  • -

#9 Guest_ESSOH ELLAH NDEDI LUMUMBA*


Reputation:

Re: Load connection string from app.config

Posted 19 March 2010 - 04:31 AM

Dylan, Add the System.Configuration.dll in the reference of the projects

NDEDI ESSOH
Was This Post Helpful? 0

Page 1 of 1