The following is the SQL Server table in design mode.
The following is the script for the table.
- CREATE TABLE [dbo].[Employee]
- (
- [ID] [int] IDENTITY(1, 1) NOT NULL,
- [Name] [varchar](50) NULL,
- [Email] [varchar](500) NULL,
- [Country] [varchar](50) NULL
- ) ON [PRIMARY] GO
The following is the .aspx code.
The following is the aspx.cs.
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Web;
- using System.Web.UI;
- using System.Web.UI.WebControls;
- using System.Data;
- using System.Data.SqlClient;
- using System.Configuration;
- using System.Web.Services;
- using System.Web.Script.Services;
- namespace jQueryWebMethodInsertData
- {
- public partial class Default: System.Web.UI.Page
- {
- protected void Page_Load(object sender, EventArgs e) {
- if (!this.IsPostBack)
- {
- BindEmployees();
- }
- }
- private void BindEmployees()
- {
- string constr = ConfigurationManager.ConnectionStrings["RConnection"].ConnectionString;
- using(SqlConnection con = new SqlConnection(constr)) {
- using(SqlCommand cmd = new SqlCommand("SELECT * FROM Employee ORDER BY ID")) {
- using(SqlDataAdapter da = new SqlDataAdapter()) {
- DataTable dt = new DataTable();
- cmd.CommandType = CommandType.Text;
- cmd.Connection = con;
- da.SelectCommand = cmd;
- da.Fill(dt);
- gvEmployee.DataSource = dt;
- gvEmployee.DataBind();
- }
- }
- }
- }
- [WebMethod]
- [ScriptMethod]
- public static void SaveUser(Employee employee)
- {
- string constr = ConfigurationManager.ConnectionStrings["RConnection"].ConnectionString;
- using(SqlConnection con = new SqlConnection(constr)) {
- using(SqlCommand cmd = new SqlCommand("INSERT INTO Employee VALUES(@Name, @Email, @Country)")) {
- cmd.CommandType = CommandType.Text;
- cmd.Parameters.AddWithValue("@Name", employee.Name);
- cmd.Parameters.AddWithValue("@Email", employee.Email);
- cmd.Parameters.AddWithValue("@Country", employee.Country);
- cmd.Connection = con;
- con.Open();
- cmd.ExecuteNonQuery();
- con.Close();
- }
- }
- }
- }
- }
Next, run the application.