Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
Tutorial RSS
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Charts
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - Database - Using Stored Procedure to Add to Database- ASP.NET & C#
Using Stored Procedure to Add to Database- ASP.NET & C#

ASP.Net 4.0 Tutorials V4
Server Intellect Cloud Hosting

ASP.NET Database Tutorial

This tutorial will show how we can use Stored Procedures instead of explicit SQL statements to add data from a form to a database. C# version.

Using Stored Procedures instead of specific SQL statements is very useful - one Stored Procedure can be referenced many times by many different pages, and changes only need to be made once, for example. Using Stored Procedures is barely more difficult to implement than using SQL statements in code.
First, we need the following assembly reference:

using System.Data.SqlClient;

Try Server Intellect for Windows Server Hosting. Quality and Quantity!

In our Web.config, we declare the connection string:

<appSettings>
<add key="ConnString" value="Data Source=CLIENT-TASK2\SQLEXPRESS;Initial Catalog=BasicDataAccess;Integrated Security=True"/>
</appSettings>

The ASPX page will look something like this:

<form id="form1" runat="server">
<asp:Label ID="Label1" runat="server"></asp:Label><br />
<table>
<tr>
<td style="width: 100px">
Name:</td>
<td style="width: 100px">
<asp:TextBox ID="txtName" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td style="width: 100px">
City:</td>
<td style="width: 100px">
<asp:TextBox ID="txtCity" runat="server"></asp:TextBox></td>
</tr>
<tr>
<td colspan="2">
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Submit" /></td>
</tr>
</table>
</form>

We used over 10 web hosting companies before we found Server Intellect. Their dedicated servers and add-ons were setup swiftly, in less than 24 hours. We were able to confirm our order over the phone. They respond to our inquiries within an hour. Server Intellect's customer support and assistance are the best we've ever experienced.

The Stored Procedure will look something like this:

ALTER PROCEDURE spInsertData
@name varchar(50),
@city varchar (50)
AS
INSERT INTO [tblOne]
(theName, theCity)
VALUES (@name, @city)
RETURN

We chose Server Intellect for its dedicated servers, for our web hosting. They have managed to handle virtually everything for us, from start to finish. And their customer service is stellar.

The code-behind will look something like this:

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;

public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{

}

protected void Button1_Click(object sender, EventArgs e)
{
SqlCommand cmd = new SqlCommand("spInsertData", new SqlConnection(ConfigurationManager.AppSettings["ConnString"]));
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("@name", txtName.Text);
cmd.Parameters.AddWithValue("@city", txtCity.Text);

cmd.Connection.Open();
cmd.ExecuteNonQuery();
cmd.Connection.Close();

Label1.Text = "<b>" + txtName.Text + "</b> has been added to the database using a Stored Procedure.";
}
}

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

Download Project Source - Enter your Email to be emailed a link to download the Full Source Project used in this Tutorial!



100% SPAM FREE! We will never sell or rent your email address!



 
  Developer Resources







Server Intellect Rocks