Here, I will bind static data to a grid view for a demo but you may bind data to a grid view dynamically.

Step 1: The Table

Create the table as in the following:

  1. CREATE TABLE tblCustomer(
  2. Id int NOT NULL,
  3. name varchar(50) NOT NULL,
  4. gender varchar(6) NOT NULL,
  5. email varchar(30) NOT NULL,
  6. country varchar(30) NOT NULL,
  7. city varchar(30) NOT NULL
  8. )

Step 2: Stored Procedure

Create the Stored Procedure as in the following:

  1. CREATE procedure Pr_SaveCustomer
  2. @CustomerXml xml
  3. as
  4. BEGIN TRANSACTION
  5. BEGIN TRY
  6. DECLARE @index int
  7. EXEC sp_xml_preparedocument @index OUTPUT, @CustomerXml;
  8. insert into tblCustomer
  9. (Id,
  10. name,
  11. gender,
  12. email,
  13. country,
  14. city)
  15. select Id,Name,Gender,Email,Country,City
  16. FROM OPENXML (@index, '/DocumentElement/Table1',2)
  17. WITH (
  18. Id int,
  19. Name varchar(50),
  20. Gender varchar(6),
  21. Email varchar(30),
  22. Country varchar(30),
  23. City varchar(30)
  24. );
  25. EXEC sp_xml_removedocument @index
  26. COMMIT TRANSACTION
  27. END TRY
  28. BEGIN CATCH
  29. ROLLBACK TRANSACTION
  30. END CATCH

Step 3: UI Design

Create the UI Design as in the following:

  1. <body>
  2. <form id="form1" runat="server">
  3. <div>
  4. <table border="1">
  5. <tr>
  6. <td>
  7. <asp:GridView ID="grdBulk" runat="server" AutoGenerateColumns="False" CellPadding="3"
  8. BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
  9. CellSpacing="2">
  10. <FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
  11. <HeaderStyle BackColor="#A55129" Font-Bold="True" ForeColor="White" />
  12. <PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
  13. <RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
  14. <SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
  15. <SortedAscendingCellStyle BackColor="#FFF1D4" />
  16. <SortedAscendingHeaderStyle BackColor="#B95C30" />
  17. <SortedDescendingCellStyle BackColor="#F1E5CE" />
  18. <SortedDescendingHeaderStyle BackColor="#93451F" />
  19. <Columns>
  20. <asp:BoundField DataField="Id" HeaderText="Id" />
  21. <asp:BoundField DataField="Name" HeaderText="Name" />
  22. <asp:BoundField DataField="Gender" HeaderText="Gender" />
  23. <asp:BoundField DataField="Email" HeaderText="Email" />
  24. <asp:BoundField DataField="Country" HeaderText="Country" />
  25. <asp:BoundField DataField="City" HeaderText="City" />
  26. </Columns>
  27. </asp:GridView>
  28. </td>
  29. </tr>
  30. <tr>
  31. <td align="center">
  32. <asp:Button ID="btnSubmit" runat="server" Text="Submit" OnClick="btnSubmit_Click"
  33. Style="background: #fff; border: 1px solid #000; text-shadow: 0px 0px 0px #000;
  34. text-align: center" />
  35. </td>
  36. </tr>
  37. </table>
  38. </div>
  39. </form>
  40. </body>

Check that the UI design looks as in the following:

gridview

Step 4: Code Behind

Create the Code Behind as in the following:

  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.IO;//
  10. namespace Demo
  11. {
  12. public partial class BulkSave : System.Web.UI.Page
  13. {
  14. protected void Page_Load(object sender, EventArgs e)
  15. {
  16. if (!IsPostBack)
  17. {
  18. BindCustomer();
  19. }
  20. }
  21. private void BindCustomer()
  22. {
  23. DataTable dt = new DataTable();
  24. dt.Columns.Add(new DataColumn("Id", typeof(int)));
  25. dt.Columns.Add(new DataColumn("Name", typeof(string)));
  26. dt.Columns.Add(new DataColumn("Gender", typeof(string)));
  27. dt.Columns.Add(new DataColumn("Email", typeof(string)));
  28. dt.Columns.Add(new DataColumn("Country", typeof(string)));
  29. dt.Columns.Add(new DataColumn("City", typeof(string)));
  30. dt.Rows.Add(1, "Rajesh", "Male", "[email protected]", "India", "Hyderabad");
  31. dt.Rows.Add(2, "Vijay", "Male", "[email protected]", "India", "Bengluru");
  32. dt.Rows.Add(3, "Rekha", "Female", "[email protected]", "India", "Pune");
  33. dt.Rows.Add(4, "Kiran", "Female", "[email protected]", "India", "Mumbai");
  34. dt.Rows.Add(5, "Suraj", "Male", "[email protected]", "India", "Chennai");
  35. grdBulk.DataSource = dt;
  36. ViewState["vsCustomer"] = dt;
  37. grdBulk.DataBind();
  38. }
  39. protected void btnSubmit_Click(object sender, EventArgs e)
  40. {
  41. if (grdBulk.Rows.Count > 0)
  42. {
  43. DataTable dt = ViewState["vsCustomer"] as DataTable;
  44. if (dt != null)
  45. {
  46. if (dt.Rows.Count > 0)
  47. {
  48. string CustomerXML = DatatableToXml(dt);
  49. if (CustomerXML != null)
  50. {
  51. using (SqlConnection con = new SqlConnection("Data Source=.;Trusted_Connection=true;Database=test"))
  52. {
  53. using (SqlCommand cmd = new SqlCommand())
  54. {
  55. cmd.Connection = con;
  56. cmd.CommandType = CommandType.StoredProcedure;
  57. cmd.CommandText = "Pr_SaveCustomer";
  58. cmd.Parameters.Add("@CustomerXml", SqlDbType.Xml, -1).Value = CustomerXML;
  59. con.Open();
  60. int i = cmd.ExecuteNonQuery();
  61. con.Close();
  62. if (i > 0)
  63. {
  64. ScriptManager.RegisterStartupScript(this, this.GetType(), "alert", "alert('Record Saved Successfully')", true);
  65. }
  66. }
  67. }
  68. }
  69. }
  70. }
  71. }
  72. }
  73. public string DatatableToXml(DataTable dt)
  74. {
  75. MemoryStream ms = new MemoryStream();
  76. dt.WriteXml(ms, true);
  77. ms.Seek(0, SeekOrigin.Begin);
  78. StreamReader sr = new StreamReader(ms);
  79. string strXML;
  80. strXML = sr.ReadToEnd();
  81. return (strXML);
  82. }
  83. }
  84. }
Check for data in the tblCustomer table in the database.

output

Yes, the bulk records are inserted into the table.

I hope you like this article and understood how to insert bulk data into SQL Server using OPENXML in ASP.NET.