1

I have a EntityModel created from ADO.NET that connects to my database. I want to fill a DataGridview. For that I'm following this code:

using System.Data;
using System.Data.SqlServerCe;
using System.Windows.Forms;

namespace WindowsFormsApplication5
{
public partial class Form1 : Form
{
public Form1()
{
    InitializeComponent();
    FillData();
}

void FillData()
{
    // 1
    // Open connection
    using (SqlCeConnection c = new SqlCeConnection(
    Properties.Settings.Default.DataConnectionString))
    {
    c.Open();
    // 2
    // Create new DataAdapter
    using (SqlCeDataAdapter a = new SqlCeDataAdapter(
        "SELECT * FROM Animals", c))
    {
        // 3
        // Use DataAdapter to fill DataTable
        DataTable t = new DataTable();
        a.Fill(t);
        // 4
        // Render data onto the screen
        dataGridView1.DataSource = t;
    }
    }
}
}

This code comes from a Web Page, so I'm trying to adapt it to my ADO.NET model. So, as I'm not sure where the Properties.Settings.Default.DataConnectionStringcomes from, I thought it was the connection string used to connect to my database, so, following my Entity Model, I wrote this to get the connection string:

SqlCeConnection c = new SqlCeConnection(db.Connection.ConnectionString);

Where db is my Entity Model created like this:

private dbEntities db = new dbEntities();

But this db.Connection.ConnectionString returns this: "name= dbEntities", so I changed it to db.Connection.DataSource, that returns this string:

"C:\\Users\\user\\Documents\\Visual Studio 2010\\Projects\\ProjectName\\MySQLProject\\bin\\Debug\\db.sdf" string

But it says that the string format is not adjusted (obviously...). I'm using SQL Server, but I'm not sure how to get that connection :(

The Properties.Settings.Default.DataConnectionString says this:

        Properties.Settings.Default.dbConnectionString  'System.Windows.Forms.PropertyStore' 

does not contain a definition for 'Settings' and no extension method 'Settings' accepting a first argument of type 'System.Windows.Forms.PropertyStore' could be found (are you missing a using directive or an assembly reference?)

1
  • You're not using the full version of SQL Server, you're using compact edition; I've retagged your question to reflect that. Commented Jan 11, 2013 at 9:29

1 Answer 1

1

The message does not mean the connection string is in wrong format! Is says that there are no settings in your project.

Are you sure that you created a respective setting? To edit them, double click the "Properties" entry in your project, then switch to the "Settings" tab. If there's no entry dbConnectionString, create one.

Sign up to request clarification or add additional context in comments.

5 Comments

Actually I have that property on my Settings tab. It says exactly this: Data Source=|DataDirectory|\db.sdf
Well, the message says that Settings is not available. This has nothing to do with the content not being correct.
Ok, I could solve that. You were right. It wasn't pointing to the correct namespace, so I added my project namespace before, and it found it :)
Actually it should work automatically. Did you manipulate the namespaces?
Yes, thanks. Now I have a different mistake, so I'm gonna try to solve it. And if I can't, I will open a new thread, as it's different to this one. Thanks!!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.