1

I created a DataBase named charityah containing 5 tables. Their names are listed in a combobox.

When I choose one of them I want to display their content in a DataGridView.

What I tried is: first I linked the DataGridView to this database and tried this code that I found:

SqlConnection connection = new SqlConnection();

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
    string s = comboBox1.Text;

    connection.ConnectionString = @"Data Source=(LocalDB)\v11.0;AttachDbFilename=C:\Users\Downloads\charityah.mdf;Integrated Security=True";

    using (connection)
    {
        connection.Open();
        SqlDataAdapter adapter = new SqlDataAdapter("select * from "+s, connection);
        DataSet ds = new DataSet();
        adapter.Fill(ds, s);
        dataGridView1.DataSource = ds.Tables[0];
        dataGridView1.Refresh();   
    }
}

This method doesn't give me any errors and it finds the tables, but nothing is seen in the DataGridView.

5
  • What does ds.Tables[0].Rows.Count report after the Fill(ds,s) ? Commented May 21, 2013 at 9:55
  • also: I wonder how much of this is related to you disposing (in the event-handler) an object that exists longer than that (as a field). You have a single connection instance, which you Dispose() (via using) every time it is used... it is guaranteed to not work the second time... (Personally, I would fix that by removing the field, and moving the new Connection() into the method, i.e. using(var connection = new SqlConnection(cs)) {...} Commented May 21, 2013 at 9:57
  • ds.Tables[0].Rows.Count gave me the number of rows that exist.. Commented May 21, 2013 at 9:59
  • i didn't understand the last thing you said but i put SqlConnection connection = new SqlConnection(); inside the methode and i used instead of using(connection) connection.open() and connection.close() Commented May 21, 2013 at 10:02
  • you only need to dispose something once; once you've done that it is toast. By using using on a field, you've doomed the object at connection to be useless after the first time through that event handler. You can open/close things lots of times - but disposal is pretty fatal. Commented May 21, 2013 at 10:06

2 Answers 2

1

Since you report (comments) that there are rows, it sounds like the primary problem (connection disposal aside) is an issue with data-binding. A few thoughts leap to mind:

  • is the table in virtual mode?
  • is it adding columns?
  • do the columns already exist?

You might want to try adding:

dataGridView1.VirtualMode = false;
dataGridView1.Columns.Clear();
dataGridView1.AutoGenerateColumns = true;

before the:

dataGridView1.DataSource = ds.Tables[0];

You might also want to check that dataGridView1.DataMember doesn't have a value assigned.

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

1 Comment

ty so much :D this problem made me a headache :P
0

try this, some times nonsense items do create a lot of mess. so try setting autogenerate columns to true. may this starts showing you the results. because as per your comments, it dosent seems there could be any other issue. so just give it a try dataGridView1.AutoGenerateColumns = true;

Comments

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.