The following is my SQL Server Data Table:



The script of the table is:

  1. CREATE TABLE [dbo].[Employee](
  2. [ID] [int] IDENTITY(1,1) NOT NULL,
  3. [Name] [varchar](50) NULL,
  4. [Country] [varchar](50) NULL,
  5. [LastLogin] [smalldatetime] NULL,
  6. [JoinedOn] [datetime] NULL
  7. ) ON [PRIMARY]
  8. GO
  9. SET ANSI_PADDING OFF
  10. GO
  11. ALTER TABLE [dbo].[Employee] ADD CONSTRAINT [DF_Employee_LastLogin] DEFAULT (getdate()) FOR [LastLogin]
  12. GO
  13. ALTER TABLE [dbo].[Employee] ADD CONSTRAINT [DF_Employee_JoinedOn] DEFAULT (getdate()) FOR [JoinedOn]
  14. GO
Now expand your DB and select Programmability -> Types -> User-Defined table Types as in the following:




  1. USE [TestDB]
  2. GO
  3. CREATE TYPE [dbo].[EmployeeType] AS TABLE(
  4. [Name] [varchar](50) NULL,
  5. [Country] [varchar](50) NULL,
  6. [LastLogin] [datetime] NULL,
  7. [JoinedOn] [datetime] NULL
  8. )
  9. GO
Now create a new Stored Procedure as in the following:


  1. USE [TestDB]
  2. GO
  3. /****** Object: StoredProcedure [dbo].[ManageEmployee]
  4. Script Date: 03/28/2015 16:58:22 ******/
  5. SET ANSI_NULLS ON
  6. GO
  7. SET QUOTED_IDENTIFIER ON
  8. GO
  9. ALTER PROCEDURE [dbo].[ManageEmployee]
  10. @tblEmployee EmployeeType READONLY
  11. AS
  12. BEGIN
  13. SET NOCOUNT ON;
  14. INSERT INTO Employee(Name, Country, JoinedOn, LastLogin)
  15. SELECT Name, Country, JoinedOn, LastLogin FROM @tblEmployee
  16. END
Now for the application, Here I will read records from a XML file and show the records in a Grid View. In the Grid View I provided a check box option so the user can select records and insert a collection of records into the DB.

The following is My Employee.xml:


  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <Employees>
  3. <Employee>
  4. <Name>Mayank</Name>
  5. <Country>India</Country>
  6. <JoinedOn>2015-01-03</JoinedOn>
  7. <LastLogin>2015-01-03</LastLogin>
  8. </Employee>
  9. <Employee>
  10. <Name>Rakesh</Name>
  11. <Country>USA</Country>
  12. <JoinedOn>2015-01-03</JoinedOn>
  13. <LastLogin>2015-01-03</LastLogin>
  14. </Employee>
  15. <Employee>
  16. <Name>Abhishek</Name>
  17. <Country>France</Country>
  18. <JoinedOn>2015-01-03</JoinedOn>
  19. <LastLogin>2015-01-03</LastLogin>
  20. </Employee>
  21. <Employee>
  22. <Name>Saurabh</Name>
  23. <Country>Dubai</Country>
  24. <JoinedOn>2015-01-03</JoinedOn>
  25. <LastLogin>2015-01-03</LastLogin>
  26. </Employee>
Now my aspx is:
  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  3. <html xmlns="http://www.w3.org/1999/xhtml">
  4. <head runat="server">
  5. <title>Table Value Parameter</title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <asp:GridView ID="GridViewEmployee" runat="server" AutoGenerateColumns="false">
  11. <Columns>
  12. <asp:TemplateField>
  13. <ItemTemplate>
  14. <asp:CheckBox ID="CheckBox1" runat="server" />
  15. </ItemTemplate>
  16. </asp:TemplateField>
  17. <asp:BoundField DataField="Name" HeaderText="Name" ItemStyle-Width="150" />
  18. <asp:BoundField DataField="Country" HeaderText="Country" ItemStyle-Width="150" />
  19. <asp:BoundField DataField="JoinedOn" HeaderText="Joined On" ItemStyle-Width="150" />
  20. <asp:BoundField DataField="LastLogin" HeaderText="Last Login" ItemStyle-Width="150" />
  21. </Columns>
  22. </asp:GridView>
  23. <br />
  24. <asp:Button ID="btninsert" Text="Insert Records" runat="server" OnClick="btninsert_Click" />
  25. </div>
  26. </form>
  27. </body>
  28. </html>
Now my aspx.cs is:
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data;
  8. using System.Data.SqlClient;
  9. using System.Configuration;
  10. public partial class _Default : System.Web.UI.Page
  11. {
  12. protected void Page_Load(object sender, EventArgs e)
  13. {
  14. if (!this.IsPostBack)
  15. {
  16. GetData();
  17. }
  18. }
  19. private void GetData()
  20. {
  21. DataSet ds = new DataSet();
  22. ds.ReadXml(Server.MapPath("~/Employee.xml"));
  23. GridViewEmployee.DataSource = ds.Tables[0];
  24. GridViewEmployee.DataBind();
  25. }
  26. protected void btninsert_Click(object sender, EventArgs e)
  27. {
  28. DataTable dt = new DataTable();
  29. dt.Columns.AddRange(new DataColumn[4]
  30. {
  31. new DataColumn("Name", typeof(string)),
  32. new DataColumn("Country",typeof(string)),
  33. new DataColumn("JoinedOn", typeof(DateTime)),
  34. new DataColumn("LastLogin", typeof(DateTime))
  35. });
  36. foreach (GridViewRow row in GridViewEmployee.Rows)
  37. {
  38. if ((row.FindControl("CheckBox1") as CheckBox).Checked)
  39. {
  40. string name = row.Cells[1].Text;
  41. string country = row.Cells[2].Text;
  42. DateTime joinedDate = DateTime.Parse(row.Cells[3].Text);
  43. DateTime lastLogin = DateTime.Parse(row.Cells[4].Text);
  44. dt.Rows.Add(name, country, joinedDate, lastLogin);
  45. }
  46. }
  47. if (dt.Rows.Count > 0)
  48. {
  49. InsertRecordsToDB(dt);
  50. }
  51. }
  52. protected void InsertRecordsToDB(DataTable dt)
  53. {
  54. using (SqlConnection con = new SqlConnection(@"Server=INDIA\MSSQLServer2k8;database=TestDB;UID=sa; pwd=india;"))
  55. {
  56. using (SqlCommand cmd = new SqlCommand("ManageEmployee"))
  57. {
  58. cmd.CommandType = CommandType.StoredProcedure;
  59. cmd.Connection = con;
  60. cmd.Parameters.AddWithValue("@tblEmployee", dt);
  61. con.Open();
  62. cmd.ExecuteNonQuery();
  63. con.Close();
  64. }
  65. }
  66. }
  67. }
Before running, the following are the records in my data table:



Now run the application. Select Records and click on the Insert button.



Now see the records in the table.