Open your Visual Studio 2010. Create an Empty Website under Visual C#. Provide a suitable name to your application such as "validation_demo".
Step 2
In Solution Explorer right-click the project and select Add New Item (or use Ctrl + Shift + A) the select Web Form (validation_demo.aspx). Again get back to Add New Item then select SQL Server Database. (Put your database inside App_Data_Folder).
Step 3
I think everyone has installed the Ajaxtoolkit inside the Toolbox in VS10. If not then go to my tutorial “How to add
AjaxToolkit in VS10” and add it, we need that later.
DATABASE CHAMBER
Step 4
In Server Explorer, under your database (Database.mdf) do something to table then select Add New Table.
Table -> tbl_data
Don't forget to set Is Identity == True for “ID”.
Figure 1 Database
Step 5
Create a Stored Procedure by going to Database.mdf -> Stored Procedure -> Add New Stored Procedure.
Stored Procedure – sp_insert:
Figure 2 Stored Procedure
DESIGN CHAMBER
Step 6
Now get back to your validation.aspx page, where we will design our application. Here is the design code:
Validation.aspx
Figure 3 Design Chamber
CODE CHAMBER
Step 7
In Solution Explorer under validation.aspx the validation.aspx.cs file is there. We will try our code in this file.
Figure 4 Namespace
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data.SqlClient;
- using System.Data;
- public partial class _Default: System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e)
- {
- }
- protected void Button1_Click(object sender, EventArgs e)
- {
- SqlConnection con = new SqlConnection(@
- "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
- SqlCommand cmd = new SqlCommand("sp_insert", con);
- cmd.CommandType = CommandType.StoredProcedure;
- cmd.Parameters.AddWithValue("fname", TextBox1.Text);
- cmd.Parameters.AddWithValue("lname", TextBox2.Text);
- cmd.Parameters.AddWithValue("email", TextBox3.Text);
- cmd.Parameters.AddWithValue("phoneno", TextBox4.Text);
- cmd.Parameters.AddWithValue("location", TextBox5.Text);
- con.Open();
- int i = cmd.ExecuteNonQuery();
- con.Close();
- }
- }
I have attached the entire source code so that you can understand better. I hope you like this. Thank you.
Have a nice day!