C# Get data from SQL Server and display it

Posted on 2004-11-10
Last Modified: 2011-10-03
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:

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

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

Question by:apirnia
    LVL 6

    Accepted Solution

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

    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.
                /// <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);


                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
                      // set the data source
                      dgEmployees.DataSource = myCommand.ExecuteReader();
                      // bind the data source to the datagrid
                      // close the connection to the database since we are done using it

    LVL 9

    Author Comment

    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();" ??

    LVL 6

    Expert Comment

      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

    Featured Post

    Better Security Awareness With Threat Intelligence

    See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

    Join & Write a Comment

    I have developed many web applications with asp & and to add and use a dropdownlist was always a very simple task, but with the new, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
    In .NET 2.0, Microsoft introduced the Web Site.  This was the default way to create a web Project in Visual Studio 2005.  In Visual Studio 2008, the Web Application has been restored as the default web Project in Visual Studio/.NET 3.x The Web Si…
    Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.
    In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor ( If you're interested in additional methods for monitoring bandwidt…

    729 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    18 Experts available now in Live!

    Get 1:1 Help Now