Hi ,
I am newbie in C#. I created a database by right clicking on the project in solution explorer and by selecting add new item then selecting database. Database name is SAMP and it has a table called Account. can somebody tell me how to connect to this database and display the contents of Account table in a gridview control through coding. Not in Design. I am trying this for a long time but could not. Pls Help me. I am using VS2008 and SqlExpress 2005.

Recommended Answers

All 4 Replies

You first have to connect to your database, use something like :
SqlConnection con = new SqlConnection("<connectionstring>");

I tried Like this

String connstring = "Data Source =.\\SQLEXPRESS;Intial Catalog = SAMP; Integrated Security = True";

but its not working.

My Program is

string connstring = "Data Source = .\\SQLEXPRESS;
Initial Catalog = SAMP; Integrated Security = True";
SqlConnection conn = new SqlConnection(connstring);
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
SqlDataAdapter myadapter = new SqlDataAdapter(comm);
DataSet myset = new DataSet();
comm.CommandText = "Select * From Account ";
myadapter.Fill(myset, "Account");
dataGridView1.DataSource = myset;
dataGridView1.DataMember = "Account";

Seems that you issue a sql command before the connection is open.
Put conn.Open() earlier in your program.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.