Net-informations.com

First ASP.NET Database Program

The following ASP.NET database program based on the "pubs" database. If you dont have the pubs database on your SQL server please follow the url.

http://www.codeproject.com/KB/database/InstallingNorthwindAndPub.aspx

After you setup the pubs database on your SQL server , you need to make a connectionstring in your we.config file.

In order to making a connection string you have to provide the following information.

  1. SqlServer Name
  2. Database Name
  3. Username
  4. Password

After collecting above information , you have to create web.config file like shown below , or copy and paste the below

content in your web.config file and fill the appropriate parameter values.

Here we are going to connect the database and count the number of rows in the publishers table.

The sql command is : SELECT COUNT(*) FROM publishers

In order to retrieve the connection string value from web.config file , we code like the following :

VB.Net

C#

After we retrieve the connection string , we will create a command object for execute the sql and read the result using SqlDataReader.

VB.Net

C#

Finally we retrieve the result in a string and show it in a Label control.

VB.Net

C#

Default.aspx



Click the following links to see full source code

C# Source Code
VB.NET Source Code
default.aspx.cs
default.aspx.vb





net-informations.com (C) 2019    Founded by raps mk
All Rights Reserved. All other trademarks are property of their respective owners.
SiteMap  | Terms  | About