I just downloaded and installed C sharp for 2010

The connection to the database via the DataAdapter and then to the DataSet does not work. I am trying to tie the db to the textboxs on the form and then navigate the db with buttons like next prev etc.

System.Data.SqlClient.SqlConnection con;

DataSet ds1;

private void Form1XXXLoad(object sender, EventArgs e)

con = new System.Data.SqlClient.SqlConnection();
con.ConnectionString = "Data Source=.\\SQLEXPRESS;
AttachDBFilename=C:\\MyWorkers.mdf;Integrated Security=True;Connect
Timeout=30;User Instance=True";


MessageBox.Show("Database Open");


MessageBox.Show("Database Closed");


string sql = "SELECT * From tblFriends";
da = new System.DataSqlClient.SqlDataAdapter(sql, con);

it just does not work, I am not looking for anyone to fix the code, JUST point out some place on the net I can get a GOOD idea about how to implement my objective. Thanks

6 Years
Discussion Span
Last Post by tanor

The problem is that you are opening your connection, then closing it and disposing of it, then trying to use it with your sqldatareader.
Check the code here. First of all, place the connection inside a "using" block. This ensures the connection is correctly closed and disposed when the using block exits.
You will also see that you don't need to manually call con.Open(). You pass the connection to the datareader which then opens the connection itself to execute your sql query.


Thanks for your answer, I will try it out today. I think some of my situation is the fact I am using a book written for C# 2005 in the 2010 IDE and syntax which is different I think.

This article has been dead for over six months. Start a new discussion instead.
Please be thoughtful and detailed and be sure to adhere to our posting rules.