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
 

Code:
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        
sqlConnection1.Open();

need data adapter and data set


// close connection
        sqlConnection1.Close();



    }
bmanmike39Asked:
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.

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

Open in new window

0

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
svcoderCommented:
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 
			sqlConnection1.Open();
			// Fill the DataSet
			ad.Fill( ds );
		}
			// Populate the GridView
			GridView1.DataSource = ds;
			Gridview1.DataBind();
 
		}

Open in new window

0
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.