?
Solved

C# Get data from SQL Server and display it

Posted on 2004-11-10
3
Medium Priority
?
364 Views
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:

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

0
Comment
Question by:apirnia
  • 2
3 Comments
 
LVL 6

Accepted Solution

by:
ryerras earned 2000 total points
ID: 12551006
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
 
LVL 9

Author Comment

by:apirnia
ID: 12561895
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
 
LVL 6

Expert Comment

by:ryerras
ID: 12561914
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

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
This lesson discusses how to use a Mainform + Subforms in Microsoft Access to find and enter data for payments on orders. The sample data comes from a custom shop that builds and sells movable storage structures that are delivered to your property. …
Suggested Courses
Course of the Month14 days, 5 hours left to enroll

807 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