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
Business Analysis Course
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 6 of 14
 

The output of this example is as shown below:

The example above contains two TextBox controls named tbFirstname and tbLastname. When the values are entered into the two Textbox controls and Submit clicked, the Button Click subroutine is executed.

The Button Click subroutine creates an instance of the SqlCommand class with a parame­tric Select statement. Two instances of the SqlParameter class are created and added to the Parameters collection of the SqlCommand class. For example, the @firstname para­meter is created and added with the following statement:

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

This statement creates a new SqlParameter named @firstname and further adds to the Parameters collection of the SqlCommand class. Passing its name and value creates the parameter. In this case, the value of the parameter is the value of the tbFirstname TextBox control.

Using Parameters with MS Access

 When using the classes from the System.Data.OleDb namespace, you need to create the parameters for MS Access. For example, to retrieve a phone number for an employee from the Employees table of Northwind database, you have to write the SQL statement like: -

Select HomePhone From Employees Where FirstName =? And LastName=?

What is Character?

This represents the parameter instead of using a named parameter like @firstname or @lastname.

Important Note: - (i) Use @ character for SQL SERVER

(ii) Use? Or @ character with MS Access.

But is? Recommended with MS ACCESS

Example 24 illustrates how you can use parametric queries with a Microsoft Access database.

Example 24 OleDbParameterSelectDemo.aspx

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

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

<Script Runat="Server">

Sub Button_Click (s as Object, e As EventArgs)

Dim conEmployee As OleDbConnection

Dim strSelect As String

Dim cmdSelect As OleDbCommand

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

strSelect = "Select HomePhone From Employees Where FirstName=? And LastName=?"

cmdSelect = New OleDbCommand( strSelect, conEmployee )

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

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

conEmployee. Open ()

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

conEmployee. Close ()

End Sub

</Script>

<html>

<body>

<form Runat="Server">

<h2>Employee Phone Retrieval</h2>

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

 

The output of above example is shown below:

       

 

.

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