ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing ExpertRating - Online Certification and Employment Testing

ExpertRating Home
ExpertRating Benefits
Recommend ExpertRating
Suggest More Tests
Privacy Policy
FAQ
Login
 
ASP Dot Net Tutorial - Index
ASP Dot Net Tutorial - Microsofts ASP.NET
ASP Dot Net Tutorial - Begining Web Forms
ASP Dot Net Tutorial - User Controls
ASP Dot Net Tutorial - ADO .NET
ASP Dot Net Tutorial - DataBinding to WebControls
ASP Dot Net Tutorial - DataList Controls
ASP Dot Net Tutorial - DataGrid Control
ASP Dot Net Tutorial - Advance Practical Examples on DataGrid
ASP Dot Net Tutorial - Working with DataSets
ASP Dot Net Tutorial - Working with XML
ASP Dot Net Tutorial - Forms Based Authentication
ASP Dot Net Tutorial - Windows Based Authentication
     

 
Online Personal Trainer Certification
Test Search by Job Title
Find a Personal Trainer
Job Resources
Free Self Tests and Quizzes
Find a Lawyer
Project Management Certification
Six Sigma Certification
Six Sigma Black Belt Certification
Six Sigma Green Belt Certification
Lean Management Certification
First Aid Certification
CPR Certification
Yoga Certification
Aerobics Certification
Pilates Certification
SEO Certification
Online Photography Course & Certification
Online Business Writing Course & Certification
Baby Sitting Course & Certification
Time Manangement Certification
Health Club Management Certification
Selling Skills Certification
Forensic Skills Certification
Green Living Course  free!
Master Trainer Certification
Total Quality Management Certification
Kaizen Certification
Creative Writing Certification
  All Online Courses
   

   
   
   
   
   
   

   
  Home > Courses, Tutorials & eBooks > ASP.NET Tutorial > ADO.NET
 

ASP.NET Tutorial - ADO.NET

                                                                                                               Page 5 of 14
 

 

Example 22 OleDbExecuteScalarCount.aspx

<%@ Import Namespace="System.Data.OleDb" %>

<Script Runat="Server">

Sub Page_Load

Dim conNorthwind As OleDbConnection

Dim cmdSelectCount As OleDbCommand

conNorthwind = New OleDbConnection( "PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA Source=c:\Employees.mdb")

conNorthwind. Open ()

cmdSelectCount = New OleDbCommand( "Select Count(*) From Authors", conNorthwind )

lblEmp. Text = cmdSelectCount. ExecuteScalar ()

conNorthwind. Close ()

End Sub

</Script>

<html>

<body>

There are

<asp:Label

ID="lblEmp"

Runat="Server" />

Employees in this Table

</body>

</html>

 

The output is shown below:

Queries Using Parameters

Queries using parameters are performed to make ADO.NET more efficient. A parameter is a value that’s either passed in or out of a query. Using parameters helps you to keep your information straight and makes your queries comprehensible. For example you need to retrieve all employees from a table who works in department no. 10.In this case the SQL query will be: -

strSql=”Select * from employees where deptno=10”

This will give a fixed result and the scope will be limited to department number 10. If we want that each record is changed by passing outside parameters the query will be as: -

strSql=Select * from employees where deptno= ”& tbDeptno.text

This will serve the purpose of building a dynamic query, but an unrecognized one. What if you have three different textboxes?

Having them would become difficult for a developer, who is trying to read the code to know as to which box contains which information.

How ever, a more efficient method is to use parameters.

Let us replace previous query with a parametric one: -

strSql= “Select * from employees where deptno= @deptNo”

here @deptNo represents the parameter.

Using Parameters with Microsoft SQL Server

When you execute a SQL statement with the SqlCommand class, parameters are represented in the statement like:

Select HomePhone From Employees Where FirstName=@firstname And LastName=@lastname

In this statement, @firstname and @lastname represent parameters.

Example23 SqlParameterSelectDemo.aspx

<%@ Import Namespace="System.Data" %>

<%@ Import Namespace="System.Data.SqlClient" %>

<Script Runat="Server">

Sub Button_Click (s as Object, e As EventArgs)

Dim conNorthwind As SqlConnection

Dim strSelect As String

Dim cmdSelect As SqlCommand

conNorthwind = New SqlConnection( "server=localhost;UID=sa;PWD=secret;Database=Northwind" )

strSelect = "Select HomePhone From Employees Where FirstName=@firstname And LastName=@lastname"

cmdSelect = New SqlCommand( strSelect, conNorthwind )

cmdSelect.Parameters.Add( "@firstname", tbFirstname.Text )

cmdSelect.Parameters.Add( "@lastname", tbLastname.Text )

conNorthwind. Open ()

lblHomePhone. Text = "<b>Home Phone Is</b>&nbsp;" & cmdSelect. ExecuteScalar ()

conNorthwind. Close ()

End Sub

</Script>

<html>

<body>

<form Runat="Server">

<h3>Employee Phone Retrieval</h3>

<b>First Name:</b>

<br>

<asp:TextBox

ID="tbFirstname"

Runat="Server" />

<p>

<b>Last Name:</b>

<br>

<asp:TextBox

ID="tbLastname"

Runat="Server" />

<p>

<asp:Button

Text="Submit"

OnClick="Button_Click"

Runat="Server" />

<p>

<asp:Label ID="lblHomePhone" Runat="Server" />

</form>

</body>

</html>

 

       
 

 

.

 
     
Home  |  About Us  |  Privacy Policy  |  Site Map  |  FAQs  |  Contact Us
 
© ExpertRating 2006. All Rights Reserved.