Accessing SQL Server on ASP.NET Web Application using SqlDataSource Web Control, Part 1: SELECT Data

This entry is part 1 of 2 in the series Accessing SQL Server on ASP.NET using SqlDataSource

On this post, you will see how to develop an ASP.NET web application that query data from SQL Server 2005 and display the result on a GridView object using SqlDataSource web control so you don’t have to write any code!

Step-by-step to SELECT data

  1. Drag a SqlDataSource object from Toolbox to a Web Form.
    SqlDataSource
  2. On SqlDataSource1 object, click Configure Data Source.
    Configure Data Source
  3. On Choose Your Data Connection, click New Connection.
    Create a New Connection
  4. On Add Connection, select Data Source as Microsoft SQL Server (SqlClient). Then, type the Server name of the SQL Server and select a database. On this example, the server is BKKSQL001\INSTANCE01 and Northwind is the database.
    Add Connection
  5. On Save the Connection String to the Application Configuration File, click Next to continue.
    Save the Connection String
  6. On Configure the Select Statement, select Specify columns from table or view and select Products table. Then, check ProductID, ProductName and UnitPrice columns. Click Next.
    Configure the Select Statement
  7. On Test Query, you can click on Test Query button to verify the query result. Then, click Finish.
    Test Query
  8. Drag a GridView object from Toolbox to the Web Form. Then, choose the Data Source to the one that you have just created.
    Set Data Source for GridView Object
  9. Build and run the project. You will see the result as the figure below.
    SELECT Data - Query Result on Web Page
Series NavigationAccessing SQL Server on ASP.NET Web Application using SqlDataSource Web Control, Part 2: UPDATE Data >>

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.