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" ;

LVL 9
apirniaAsked:
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.

ryerrasCommented:
The simplest of simplest example is to use datagrid

html portion
---------------------------------------------------------------------------------------
<%@ Page language="c#" Codebehind="WebForm2.aspx.cs" AutoEventWireup="false" Inherits="WebApplication1.WebForm2" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
      <HEAD>
      </HEAD>
      <body ms_positioning="GridLayout">
            <form id="Form1" runat="server">
                  <asp:datagrid id="dgEmployees" Runat="server" EnableViewState="False"></asp:datagrid>
                  <asp:Button id="Button1" style="Z-INDEX: 101; LEFT: 232px; POSITION: absolute; TOP: 184px" runat="server"
                        Text="Button"></asp:Button></form>
      </body>
</HTML>


code behind
----------------------------------------------------------------------------------------
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Configuration;
using System.Data.SqlClient;
using System.Web.UI.WebControls;


namespace WebApplication1
{
      /// <summary>
      /// Summary description for WebForm2.
      /// </summary>
      public class WebForm2 : System.Web.UI.Page
      {
            protected System.Web.UI.WebControls.DataGrid dgEmployees;
            protected System.Web.UI.WebControls.Button Button1;
      
            private void Page_Load(object sender, System.EventArgs e)
            {
                  
            }

            #region Web Form Designer generated code
            override protected void OnInit(EventArgs e)
            {
                  //
                  // CODEGEN: This call is required by the ASP.NET Web Form Designer.
                  //
                  InitializeComponent();
                  base.OnInit(e);
            }
            
            /// <summary>
            /// Required method for Designer support - do not modify
            /// the contents of this method with the code editor.
            /// </summary>
            private void InitializeComponent()
            {    
                  this.Button1.Click += new System.EventHandler(this.Button1_Click);
                  this.Load += new System.EventHandler(this.Page_Load);

            }
            #endregion

            private void Button1_Click(object sender, System.EventArgs e)
            {
                  SqlConnection myConnection = new SqlConnection(ConfigurationSettings.AppSettings["connectionString"]);
                  SqlCommand myCommand = new SqlCommand("Select * from Employee", myConnection);
                  // opens the connection to the database
                  myConnection.Open();
                  // set the data source
                  dgEmployees.DataSource = myCommand.ExecuteReader();
                  // bind the data source to the datagrid
                  dgEmployees.DataBind();
                  // close the connection to the database since we are done using it
                  myConnection.Close();

            }
      }
}
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
apirniaAuthor Commented:
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
0
ryerrasCommented:
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
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
ASP.NET

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.