Link to home
Start Free TrialLog in
Avatar of apirnia
apirniaFlag for United States of America

asked on

C# Get data from SQL Server and display it

I want to know How I can make an aspx  page with one button "Click Me" and as soon as user clicks on it, I will get data from my database and displays in on the browser in a table and all in C#.

I already have dome tons of readings but I cant put all the parts together.

So far I have added this to my web.config:

<appSettings>
<add key="dbconnection" value="server=local;database=MyTesting;uid=testx;password=testy;" />
</appSettings>

On my codebehind I have the following namespaces:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.Configuration;   //For Web Config
using System.Data.SqlClient;   //Do I need this one??????


and my Query is:

sSQL = "Select * from Client" ;

ASKER CERTIFIED SOLUTION
Avatar of ryerras
ryerras

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of apirnia

ASKER

Thank you, that worked great. One question I have seen some people do the following:

dgEmployees.DataSource = myCommand.ExecuteReader(CommandBehavior.CloseConnection);

In your example you did it like this:

dgEmployees.DataSource = myCommand.ExecuteReader();

What is this part  exactly do? "CommandBehavior.CloseConnection"

If I do that whould I still need "myConnection.Close();" ??

Thanks
Avatar of ryerras
ryerras

apirinia,
  CommandBehavior.CloseConnection is optional . This tells the reader to close the  Connection object when the DataReader object is closed. If you do not use, you should close Database connection after you close the DataReader. Just an extra feature to avoid a pitfall. remember open databae connections are some of most the common mistakes in web applications