How to fill a Gridview control with specific data value

Im trying to understand how to fill a GridView1 control with the values from my query.
I know how to connect to the dB, but dont know how to fill the grid. Can someone show me how to do this?  Below is the code i have.

Thanks for any help

protected void Button1_Click(object sender, EventArgs e)

        SqlConnection sqlConnection1 = new SqlConnection("Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ConDb.mdf;Integrated Security=True;User Instance=True");
        SqlCommand cmd = new SqlCommand();
        Object returnValue;

        cmd.CommandText = "SELECT  nameID, Fname FROM name WHERE (Fname = 'mike')";
        cmd.CommandType = CommandType.Text;
        cmd.Connection = sqlConnection1;

// open connetction        

need data adapter and data set

// close connection

Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Between the open and close of the connection:
SqlDataAdapter data = New SqlDataAdapter(cmd);
DataSet source = New DataSet("name");
GridView1.DataSource = source;

Open in new window


Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Try this
protected void Button1_Click1( object sender, EventArgs e )
			// Create a new instance of the DataSet
			DataSet ds = new DataSet();
			// Create the connection, and data adapter objects and wrap them in a using ()
			using ( SqlConnection sqlConnection1 = new SqlConnection( "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ConDb.mdf;Integrated Security=True;User Instance=True" ) )
			using ( SqlDataAdapter ad = new SqlDataAdapter( "SELECT  nameID, Fname FROM name WHERE (Fname = 'mike')", sqlConnection1 ) )
			// Open the connection 
			// Fill the DataSet
			ad.Fill( ds );
			// Populate the GridView
			GridView1.DataSource = ds;

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.