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

The above page contains two TextBox controls named tbFirstname and tbLastname. When you enter values into the two Textbox controls and click Submit, the Button Click subroutine is executed. hype

The Button Click subroutine creates an instance of the OleDbCommand class with a parame­tric Select statement. Two instances of the OleDbParameter class are created and added to the Parameters collection of the OleDbCommand 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 OleDbParameter named @firstname and adds it to the Parameters collection of the OleDbCommand 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.

Inserting Values in a Table [INSERT Command]

 We can use INSERT command for inserting the new records in the table. The syntax of this command is: -

INSERT [into] table_name (column1, column2…) VALUES (value1, value2…)

If we try to write this command for our employees’ table, we will write it as:

INSERT into employees (EmployeeID,FirstName,LastName) VALUES (102, 'John', ‘Greg’ )

The important steps needed to execute the INSERT command is: -

1. Create and open a database connection.

2. Create a database command that represents the SQL Insert statement to execute.

3. Execute the command with the ExecuteNonQuery () method.

The following example demonstrates the INSERT command

Example25 SqlInsertDemo.aspx

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

<%

Dim conNorthwind As SqlConnection

Dim strInsert As String

Dim cmdInsert As SqlCommand

conNorthwind = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;database=Northwind" )

strInsert = "Insert books ( bookName,Author) Values ('Express AspNet', 'John' )"

cmdInsert = New SqlCommand( strInsert, conNorthwind )

conNorthwind. Open ()

cmdInsert. ExecuteNonQuery ()

conNorthwind. Close ()

%>

New book Added!

 

The output of this example is shown below:

The first line in the above example imports the necessary namespaces to use the OleDb classes.

Next, a connection to an OleDb database is initialized.

Then, a variable named strInsert that has a SQL Insert statement as its value is created .The statement inserts a new record into a table named Books in database.

In the statement that follows, an instance of the OleDbCommand class is created. This class is initialized with two parameters: the command to execute and the connection to use for executing the command.

Finally, the command is executed by calling the ExecuteNonQuery () method of the OleDbCommand class. This method sends the SQL command to the database server, and the database server executes the command.

Here we have used ExecuteNonQuery () rather than ExecuteReader () to execute the command. ExecuteNonQuery () method is used because you are not returning any records from the database.

Parametric Insert

Practically Parametric Insert is used in the form of a web i.e. the parameters are passed from outside. The following example demonstrates it.

Example 27 SqlParameterInsert.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 strInsert As String

Dim cmdInsert As SqlCommand

conNorthwind = New SqlConnection( "Server=localhost;UID=sa;PWD=secret;database=Northwind" )

strInsert = "Insert Books ( bookName, Author ) Values ( @bkName, @bkAuthor )"

cmdInsert = New SqlCommand( strInsert, conNorthwind )

cmdInsert.Parameters.Add( "@bkName", txtbookName.Text )

cmdInsert.Parameters.Add( "@bkAuthor",txtAuthor.Text)

conNorthwind. Open ()

cmdINsert. ExecuteNonQuery ()

ConNorthwind. Close ()

End Sub

</Script>

<html>

<body>

<form Runat="Server">

<h3>Add New Book Form</h3>

<b>Book's Name:</b>

<br>

<asp:TextBox

ID="txtbookName"

Runat="Server" />

<p>

<b>Author:</b>

<br>

<asp:TextBox

ID="txtAuthor"

Runat="Server" />

<p>

<asp:Button

Text="Add Books!"

OnClick="Button_Click"

Runat="Server" />

</form>

</body>

</html>

 

       
 

 

 

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