fetch data from a Sql server database

T

Tony Johansson

Hello!

This is just for learing how all fits together.
I have this event handler button1_Click below

The sql clause for supplierTableAdapter is
SELECT SupplierID, CompanyName
FROM Suppliers

I have also a combobox where the DataSource is set to suppliersBindingSource
and DisplayMember to CompanyName and ValueMember to SupplierID

So when I click button1 all CompanyName is listed in the combobox which is
was I was expected. So this works good.

Now to my question: If I comment out these two rows
suppliersTableAdapter.Connection = dataConnection;
dataConnection.Open();
in the event handler and
run again it works just as good as it did before.
So how can it works when I don't have these two rows.


private void button1_Click(object sender, EventArgs e)
{
SqlConnection dataConnection = new SqlConnection();
dataConnection.ConnectionString = "Integrated Security=true;" +
"Initial Catalog=Northwind;" +
"Data Source=hempc\\SQLExpress";
suppliersTableAdapter.Connection = dataConnection;
dataConnection.Open();
suppliersTableAdapter.Fill(northwindDataSet.Suppliers);
}

//Tony
 
A

Alberto Poblacion

Tony Johansson said:
Now to my question: If I comment out these two rows
suppliersTableAdapter.Connection = dataConnection;
dataConnection.Open();
in the event handler and
run again it works just as good as it did before.
So how can it works when I don't have these two rows.

If you don't explicitly set the Connection, the TableAdapter uses the
original connection string that was saved in the Application Settings when
you used the IDE to create the TableAdapter.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top