Click here to Skip to main content
15,886,034 members
Articles / Web Development / HTML

Easy 8 Step HTML To SQL Data Transfer in a ASP.NET Web Form using jQuery, JSON, XML, Web Methods, node() Methods and Extension Methods.

Rate me:
Please Sign up or sign in to vote.
5.00/5 (1 vote)
7 Feb 2013CPOL2 min read 24.1K   497   18  
This article uses 8 steps to create example of how to read and write from HTML to a SQL database in a .NET web application using jQuery, JSON, and XML.
SET ANSI_NULLS ON
GO

SET QUOTED_IDENTIFIER ON
GO

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[_Contacts](
	[ContactID] [bigint] IDENTITY(1,1) NOT NULL,
	[FirstName] [varchar](50) NULL,
	[LastName] [varchar](50) NULL,
	[Company] [varchar](50) NULL,
 CONSTRAINT [PK_Contacts] PRIMARY KEY CLUSTERED 
(
	[ContactID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO


CREATE PROCEDURE [dbo].[_UpdateContact]
	-- Add the parameters for the stored procedure here
	@XML XML
AS 
	SET NOCOUNT ON
        
	DECLARE	@Temp TABLE ( [ContactID] BIGINT ,
						  [FirstName] VARCHAR(50) ,
						  [LastName] VARCHAR(50) ,
						  [Company] VARCHAR(50) )

	INSERT	INTO @Temp
			SELECT	x.value('(ContactID/text())[1]', 'bigint'), x.value('(FirstName/text())[1]', 'varchar(50)'), x.value('(LastName/text())[1]', 'varchar(50)'),
					x.value('(Company/text())[1]', 'varchar(50)')
			FROM	@XML.nodes('//Contact') IDX ( x ) 

	IF EXISTS ( SELECT	*
				FROM	@Temp
				WHERE	[ContactID] IS NULL ) 
		BEGIN
			INSERT	INTO dbo.[_Contacts] ( FirstName, LastName, Company )
					SELECT	FirstName, LastName, Company
					FROM	@Temp 
		END
	ELSE 
		BEGIN 	
			UPDATE	t1
			SET		t1.[FirstName] = ISNULL(t2.[FirstName], t1.[FirstName]), t1.[LastName] = ISNULL(t2.[LastName], t1.[LastName]),
					t1.[Company] = ISNULL(t2.[Company], t1.[Company])
			FROM	_Contacts t1
					JOIN @Temp t2 ON t1.[ContactID] = t2.[ContactID]
		END    

	SELECT	ContactID, FirstName, LastName, Company
	FROM	dbo._Contacts
GO

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior)
United States United States
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions