Requirements

I will be using a UserControl to develop this application as in the following:

Procedure

  1. Database
  2. User Interface Design
  3. Program

SQL Server Database

  1. We need to use 2 Tables,
  2. One table as Temporary use,
  3. Another table for Permanent.

Database Table Structure

  1. use ASP_Complete_Practice
  2. GO
  3. -- Table structure for Temporary use
  4. create table tbl_Temp_Gridview4
  5. (
  6. ID int identity primary key,
  7. Name varchar(50),
  8. Mobile bigint,
  9. EmailID varchar(100),
  10. Address varchar(100)
  11. )
  12. --- Table for Permanent Use
  13. create table tbl_Gridview4
  14. (
  15. Regid varchar(50),
  16. ID int identity primary key,
  17. Name varchar(50),
  18. Mobile bigint,
  19. EmailID varchar(100),
  20. Address varchar(100)
  21. )

Design Of The Application Form

Design Of The Application Form

How to Design the Form

1. Drag a GridView and design it.

2. Select and click on Auto Format from the GridView tasks.

Select and Click on Auto Format

3. Select a Scheme from the left side such as Professional and click Apply and OK.

Click Apply and OK

4. Click on Edit Column on the GridView Task.

Edit Column on the Gridview Task

5. Add some bound fields to the GridView.

Add Some Bound Fields

6. Add a property to the bound fields such as Header Field, Data Field name and then convert the bound fields to the Template Fields.

Add Property to the bound

7. Uncheck the CheckBox from the left side (Auto-generate fields).

CheckBox

8. Add an Extra Template Field to the GridView and name it Add New and click on OK.

Add a Extra Template

9. Click on Edit Templates from GridView Tasks.

 Edit Templates from Gridview Task

10. Select a template from the GridView Tasks and design it.

Task and Design
11. Add a TextBox to the Footer Template.

Footer Template

12. Add a TextBox to all the templates like this except the Add New Template.

Except the Add New Template

13. Add a button to the FooterTemplate of the Add new Template.

FooterTemplate

14. Add a property to the button at the Add New Template, remember here the CommandName property is very important.

remember the CommandName

15. Add a new template to the GridView and give it a name.

Add a new Template

16. Add a TemplateField and provide an Edit / Delete name to the Header text.

TemplateField
17. Add two buttons to the ItemTemplate and two buttons to the EditItemTemplate of this new template (Edit / Delete Template).

new Template

18. Add a CommandName as Edit to the GridView Edit button.

Edit button

19. Add a CommandName as Delete to the GridView Delete button.

Delete button

20. Add a CommanName as Update to the GridView Update Button.
Update Button

21. Add a CommandName as Cancel to the GridView Cancel Button.

Add CommandName

22. Click on End Template Editing at the GridView Task.

Gridview Task
23. Select GridView Property by right-clicking on the GridView.

Right Clicking on the Gridview

24. Set this property as ShowFooter to True, ShowHeaderWhenEmpty to True.

ShowHeaderWhenEmpty

Design the GridView for EmptyDataTemplate

25. Click again on the Edit Template at the GridView Tasks.

Edit Template

26. Select EmptyDataTemplate from the GridView Tasks.

EmptyDataTemplate

27. Add 4 TextBoxes and 1 button to the EmptyDataTemplate.

Button

28. Provide the property to the Button at the EmptyDatatEmplate such as the CommandName as AddNew1, (Remember here the CommandName is very Important) ID as btnAddGrid1 name and others.

Property to the Button
Complete Design of the GridView Until Now (Source View)

  1. <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
  2. GridLines="None" AutoGenerateColumns="False" ShowFooter="True"
  3. ShowHeaderWhenEmpty="True" onrowcommand="GridView1_RowCommand"
  4. AllowPaging="True" DataKeyNames="ID"
  5. onpageindexchanging="GridView1_PageIndexChanging"
  6. onrowdeleting="GridView1_RowDeleting" PageSize="4" Width="873px"
  7. onrowediting="GridView1_RowEditing"
  8. onrowcancelingedit="GridView1_RowCancelingEdit"
  9. onrowupdating="GridView1_RowUpdating">
  10. <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
  11. <Columns>
  12. <asp:TemplateField HeaderText="ID">
  13. <%--<EditItemTemplate>
  14. <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("ID") %>'></asp:TextBox>
  15. </EditItemTemplate>--%>
  16. <ItemTemplate>
  17. <asp:Label ID="Label5" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
  18. </ItemTemplate>
  19. </asp:TemplateField>
  20. <asp:TemplateField HeaderText="Name">
  21. <EditItemTemplate>
  22. <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
  23. </EditItemTemplate>
  24. <FooterTemplate>
  25. <asp:TextBox ID="TextBox5" runat="server" CssClass="SmallTextBox"></asp:TextBox>
  26. </FooterTemplate>
  27. <ItemTemplate>
  28. <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
  29. </ItemTemplate>
  30. <ControlStyle Width="100px" />
  31. <FooterStyle Width="100px" />
  32. <HeaderStyle Width="100px" />
  33. <ItemStyle Width="100px" />
  34. </asp:TemplateField>
  35. <asp:TemplateField HeaderText="Mobile">
  36. <EditItemTemplate>
  37. <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Mobile") %>'></asp:TextBox>
  38. </EditItemTemplate>
  39. <FooterTemplate>
  40. <asp:TextBox ID="TextBox6" runat="server" CssClass="SmallTextBox"></asp:TextBox>
  41. </FooterTemplate>
  42. <ItemTemplate>
  43. <asp:Label ID="Label2" runat="server" Text='<%# Bind("Mobile") %>'></asp:Label>
  44. </ItemTemplate>
  45. <ControlStyle Width="100px" />
  46. <FooterStyle Width="100px" />
  47. <HeaderStyle Width="100px" />
  48. <ItemStyle Width="100px" />
  49. </asp:TemplateField>
  50. <asp:TemplateField HeaderText="EmailID">
  51. <EditItemTemplate>
  52. <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("EmailID") %>'></asp:TextBox>
  53. </EditItemTemplate>
  54. <FooterTemplate>
  55. <asp:TextBox ID="TextBox7" runat="server" Width="170px"></asp:TextBox>
  56. </FooterTemplate>
  57. <ItemTemplate>
  58. <asp:Label ID="Label3" runat="server" Text='<%# Bind("EmailID") %>'></asp:Label>
  59. </ItemTemplate>
  60. <ControlStyle Width="180px" />
  61. <FooterStyle Width="180px" />
  62. <HeaderStyle Width="180px" />
  63. <ItemStyle Width="180px" />
  64. </asp:TemplateField>
  65. <asp:TemplateField HeaderText="Address">
  66. <EditItemTemplate>
  67. <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Address") %>'></asp:TextBox>
  68. </EditItemTemplate>
  69. <FooterTemplate>
  70. <asp:TextBox ID="TextBox8" runat="server" CssClass="SmallTextBox" Width="150px"></asp:TextBox>
  71. </FooterTemplate>
  72. <ItemTemplate>
  73. <asp:Label ID="Label4" runat="server" Text='<%# Bind("Address") %>'></asp:Label>
  74. </ItemTemplate>
  75. <ControlStyle Width="150px" />
  76. <FooterStyle Width="150px" />
  77. <HeaderStyle Width="150px" />
  78. <ItemStyle Width="150px" />
  79. </asp:TemplateField>
  80. <asp:TemplateField HeaderText="Add New">
  81. <FooterTemplate>
  82. <asp:Button ID="btnAddGrid2" runat="server" CommandName="AddNew2"
  83. CssClass="SmallButton" Text="Add" />
  84. </FooterTemplate>
  85. <ControlStyle Width="70px" />
  86. <FooterStyle Width="70px" />
  87. <HeaderStyle Width="70px" />
  88. <ItemStyle Width="70px" />
  89. </asp:TemplateField>
  90. <asp:TemplateField HeaderText="Edit / Delete ">
  91. <EditItemTemplate>
  92. <asp:Button ID="btnGridUpdate" runat="server" CssClass="SmallButton"
  93. Text="Update" CommandName="Update" />
  94. <asp:Button ID="btnGridCancel" runat="server" CssClass="SmallButton"
  95. Text="Cancel" CommandName="Cancel" />
  96. </EditItemTemplate>
  97. <ItemTemplate>
  98. <asp:Button ID="btnGridEdit" runat="server" CssClass="SmallButton"
  99. Text="Edit" CommandName="Edit" />
  100. <asp:Button ID="btnGridDelete" runat="server" CssClass="SmallButton"
  101. Text="Delete" CommandName="Delete" OnClientClick="return confirm('Are You Sure You Want To Delete ?')" />
  102. </ItemTemplate>
  103. <HeaderStyle Width="130px" />
  104. </asp:TemplateField>
  105. </Columns>
  106. <EditRowStyle BackColor="#999999" />
  107. <EmptyDataTemplate>
  108. <asp:TextBox ID="TextBox9" runat="server" CssClass="SmallTextBox"></asp:TextBox>
  109. <asp:TextBox ID="TextBox10" runat="server" CssClass="SmallTextBox"></asp:TextBox>
  110. <asp:TextBox ID="TextBox11" runat="server" CssClass="SmallTextBox"
  111. Width="170px"></asp:TextBox>
  112. <asp:TextBox ID="TextBox12" runat="server" CssClass="SmallTextBox"
  113. Width="150px"></asp:TextBox>
  114. <asp:Button ID="btnAddGrid1" runat="server" CommandName="AddNew1"
  115. CssClass="SmallButton" Text="Add New" />
  116. </EmptyDataTemplate>
  117. <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  118. <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  119. <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
  120. <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
  121. <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
  122. <SortedAscendingCellStyle BackColor="#E9E7E2" />
  123. <SortedAscendingHeaderStyle BackColor="#506C8C" />
  124. <SortedDescendingCellStyle BackColor="#FFFDF8" />
  125. <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
  126. </asp:GridView>

Programming Starts Here

Place the following code in the Gridview_EmptyDataTemplate_Control_4.ascx.cs file.

29. Add these namespaces:

  1. using System.Data;
  2. using System.Data.SqlClient;
  3. using System.Drawing;
  4. using System.Configuration;

30. The following are the database connection and some variable declarations.

  1. public partial class Tech_Test_Controls_Gridview_Control_4 : System.Web.UI.UserControl
  2. {
  3. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CONN"].ConnectionString.ToString());
  4. //
  5. int i;
  6. // declar Five variables here
  7. int ID;
  8. string Name, EmailID, Address;
  9. long Mobile;
  10. //
  11. DataTable Dt = new DataTable();
  12. }

31. First of all, we need to check if the Registration Number already exists or not.

Click on the TextBox or in the TextChangedEvent of the TextBox for the Registration Number.

Registration Number

  1. protected void txtRegidtNo_TextChanged(object sender, EventArgs e)
  2. {
  3. // this method is used to check if the Registration number already exist or not
  4. CheckRegistrationNo();
  5. }
  6. private void CheckRegistrationNo()
  7. {
  8. // this method is used to check if the Registration number already exist or not
  9. try
  10. {
  11. string Query = "select Regid from tbl_Gridview4 where Regid=@Regid";
  12. //
  13. SqlCommand cmd = new SqlCommand(Query, con);
  14. cmd.Parameters.AddWithValue("@Regid", txtRegidtNo.Text);
  15. con.Open();
  16. SqlDataReader dr = cmd.ExecuteReader();
  17. if (dr.Read())
  18. {
  19. lblMessage1.Visible = true;
  20. lblMessage1.ForeColor = Color.Red;
  21. lblMessage1.Text = "This Registration Number is Already Exist, Please try Another ...";
  22. //
  23. dr.Close();
  24. con.Close();
  25. }
  26. else
  27. {
  28. dr.Close();
  29. con.Close();
  30. //
  31. lblMessage1.Visible = false;
  32. }
  33. }
  34. catch (Exception Exc)
  35. {
  36. lblMessage1.Visible = true;
  37. lblMessage2.ForeColor = Color.Red;
  38. lblMessage1.Text = " Application Error : " + Exc.Message;
  39. }
  40. finally
  41. {
  42. //
  43. }
  44. }

32. Retrieving the data from the temporary table to load the GridView.

  1. protected void Page_Load(object sender, EventArgs e)
  2. {
  3. if (!IsPostBack)
  4. {
  5. //Loading the Gridview
  6. LoadGridView();
  7. }
  8. }
  9. private void LoadGridView()
  10. {
  11. // This Method will load the Gridview with Initial Data from the Temporary Table
  12. try
  13. {
  14. string Query = "select * from tbl_Temp_Gridview4";
  15. //
  16. SqlCommand cmd = new SqlCommand(Query, con);
  17. SqlDataAdapter da = new SqlDataAdapter();
  18. da.SelectCommand = cmd;
  19. //
  20. con.Open();
  21. da.Fill(Dt);
  22. con.Close();
  23. //
  24. GridView1.DataSource = Dt;
  25. GridView1.DataBind();
  26. }
  27. catch (Exception Exc)
  28. {
  29. lblMessage2.Visible = true;
  30. lblMessage2.ForeColor = Color.Red;
  31. lblMessage2.Text = " Application Error : " + Exc.Message;
  32. }
  33. finally
  34. {
  35. //
  36. }
  37. }

33. Run the project and watch the output.

Run the Project

34. Inserting the data with the EmptyDataTemplate.

35. Select GridvView, right-click and select Property, then select Event and click on the GridviewRowCommand event.

GridviewRowCommand Event

  1. protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
  2. {
  3. try
  4. {
  5. if (txtRegidtNo.Text == "")
  6. {
  7. lblMessage1.Visible = true;
  8. lblMessage1.ForeColor = Color.Red;
  9. lblMessage1.Text = "Please Enter The Registration Number First";
  10. }
  11. else
  12. {
  13. lblMessage1.Visible = false;
  14. //
  15. //check registration number
  16. CheckRegistrationNo();
  17. GridViewRow row = (GridViewRow)(e.CommandSource as Button).NamingContainer;
  18. // this method will call first when the Gridview is Empty and the EmptyDataTemplate is Called here
  19. if (e.CommandName == "AddNew1")
  20. {
  21. // finding the value from the TextBox inside the EmptyDataTempalte
  22. Name = (row.FindControl("TextBox9") as TextBox).Text;
  23. Mobile = Convert.ToInt64((row.FindControl("TextBox10") as TextBox).Text);
  24. EmailID = (row.FindControl("TextBox11") as TextBox).Text;
  25. Address = (row.FindControl("TextBox12") as TextBox).Text;
  26. //
  27. string Query = "insert into tbl_Temp_Gridview4 (Name,Mobile,EmailID,Address) values(@Name,@Mobile,@EmailID,@Address)";
  28. //
  29. SqlCommand cmd = new SqlCommand(Query, con);
  30. //
  31. cmd.Parameters.AddWithValue("@Name", Name);
  32. cmd.Parameters.AddWithValue("@Mobile", Mobile);
  33. cmd.Parameters.AddWithValue("@EmailID", EmailID);
  34. cmd.Parameters.AddWithValue("@Address", Address);
  35. //
  36. con.Open();
  37. i = cmd.ExecuteNonQuery();
  38. con.Close();
  39. //
  40. if (i > 0)
  41. {
  42. //loading the Gridview
  43. LoadGridView();
  44. //
  45. lblMessage2.Visible = true;
  46. lblMessage2.ForeColor = Color.Green;
  47. lblMessage2.Text = "One Record Add Sucessfully";
  48. }
  49. }
  50. // this method will called in FooterTemplate after one record is added to the Gridview
  51. if (e.CommandName == "AddNew2")
  52. {
  53. // finding the value from the TextBox inside the EmptyDataTempalte
  54. Name = (row.FindControl("TextBox5") as TextBox).Text;
  55. Mobile = Convert.ToInt64((row.FindControl("TextBox6") as TextBox).Text);
  56. EmailID = (row.FindControl("TextBox7") as TextBox).Text;
  57. Address = (row.FindControl("TextBox8") as TextBox).Text;
  58. //
  59. string Query = "insert into tbl_Temp_Gridview4 (Name,Mobile,EmailID,Address) values(@Name,@Mobile,@EmailID,@Address)";
  60. //
  61. SqlCommand cmd = new SqlCommand(Query, con);
  62. //
  63. cmd.Parameters.AddWithValue("@Name", Name);
  64. cmd.Parameters.AddWithValue("@Mobile", Mobile);
  65. cmd.Parameters.AddWithValue("@EmailID", EmailID);
  66. cmd.Parameters.AddWithValue("@Address", Address);
  67. //
  68. con.Open();
  69. i = cmd.ExecuteNonQuery();
  70. con.Close();
  71. //
  72. if (i > 0)
  73. {
  74. //loading the Gridview
  75. LoadGridView();
  76. //
  77. lblMessage2.Visible = true;
  78. lblMessage2.ForeColor = Color.Green;
  79. lblMessage2.Text = "One Record Add Sucessfully";
  80. }
  81. }
  82. }
  83. }
  84. catch (Exception Exc)
  85. {
  86. lblMessage2.Visible = true;
  87. lblMessage2.ForeColor = Color.Red;
  88. lblMessage2.Text = " Application Error : " + Exc.Message;
  89. }
  90. finally
  91. {
  92. //
  93. }
  94. }

36. Run the application and here is the output.

Output

37. If the GridView is exceeding the page, then set it's a Column property.

38. Go to GridView, right-click on Edit Columns.

Edit Columns

39. As an example.

Example

40. Display the output after changing the width.

output after changing the Width

41. Click on the GridviewPageIndexChanging event.

GridviewPageIndexChanging

  1. protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
  2. {
  3. // Doing Paging here
  4. GridView1.PageIndex = e.NewPageIndex;
  5. //Calling tne Function to load the Gridview
  6. LoadGridView();
  7. }

42. Set the GridView Property Allow Paging to True and PageSize to 4.

True and PageSize

43. Run the application and display the output.

Run The Application and watch the Output

44. Deleting the rows from the GridView.

45. Before that provide the DataKeyNames to the GridView as ID.

DataKeyNames

Provide the code in the Source View of the GridView for Confirmation Message (OnClientClick="return confirm('Are You Sure You Want To Delete ?')").

  1. <ItemTemplate>
  2. <asp:Button ID="btnGridDelete" runat="server" CssClass="SmallButton"
  3. Text="Delete" CommandName="Delete" OnClientClick="return confirm('Are You Sure You Want To Delete ?')" />
  4. </ItemTemplate>

46. Select GridView RowDeleting event.

Select Gridview

47. Provide the following code in the GridView RowDeleting event.

  1. protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
  2. {
  3. if (con.State == ConnectionState.Open)
  4. {
  5. con.Close();
  6. }
  7. try
  8. {
  9. //retrieving the DataKey name here
  10. ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
  11. string Query = "delete from tbl_Temp_Gridview4 where ID=@ID";
  12. //
  13. SqlCommand cmd = new SqlCommand(Query, con);
  14. cmd.Parameters.AddWithValue("@ID", ID);
  15. //
  16. con.Open();
  17. i = cmd.ExecuteNonQuery();
  18. if (i > 0)
  19. {
  20. //Load the Gridview
  21. LoadGridView();
  22. }
  23. con.Close();
  24. }
  25. catch (Exception Exc)
  26. {
  27. lblMessage2.Visible = true;
  28. lblMessage2.ForeColor = Color.Red;
  29. lblMessage2.Text = " Application Error : " + Exc.Message;
  30. }
  31. finally
  32. {
  33. //
  34. }
  35. }

48. Run the application and try to delete a row from the GridView.

Row from the Gridview

49. Edit and update The GridView row.

50. Click on the RowEditing event of the GridView.

RowEditing Event
51. Provide the code here:

  1. protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  2. {
  3. //this line of code will open the Gridview in the Edit mode
  4. GridView1.EditIndex = e.NewEditIndex;
  5. }

52. Cancelling the edit.

53. Click on the RowCancelingEdit event.

RowCancelingEdit
54. Provide the code.

  1. protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  2. {
  3. //this code will Cancel the row edit
  4. GridView1.EditIndex = -1;
  5. //Loading the Gridview again
  6. LoadGridView();
  7. }

55. Run the application and dispaly the output after the Cancel Event has been handled.

Run the application

56. Updating the GridView row.

Gridview row

57. Provide the following code.

  1. protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  2. {
  3. try
  4. {
  5. //retrieving the DataKey name here
  6. ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
  7. // ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
  8. Name = (GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox).Text;
  9. Mobile = Convert.ToInt64((GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox).Text);
  10. EmailID = (GridView1.Rows[e.RowIndex].FindControl("TextBox3") as TextBox).Text;
  11. Address = (GridView1.Rows[e.RowIndex].FindControl("TextBox4") as TextBox).Text;
  12. string Query = "update tbl_Temp_Gridview4 set Name=@Name, Mobile=@Mobile, EmailID=@EmailID, Address=@Address where ID=@ID ";
  13. //
  14. SqlCommand cmd = new SqlCommand(Query, con);
  15. //
  16. cmd.Parameters.AddWithValue("@Name", Name);
  17. cmd.Parameters.AddWithValue("@Mobile", Mobile);
  18. cmd.Parameters.AddWithValue("@EmailID", EmailID);
  19. cmd.Parameters.AddWithValue("@Address", Address);
  20. cmd.Parameters.AddWithValue("@ID", ID);
  21. //
  22. con.Open();
  23. i = cmd.ExecuteNonQuery();
  24. con.Close();
  25. //
  26. if (i > 0)
  27. {
  28. // Closing the Editmode
  29. GridView1.EditIndex = -1;
  30. //
  31. //loading the Gridview
  32. LoadGridView();
  33. //
  34. lblMessage2.Visible = true;
  35. lblMessage2.ForeColor = Color.Green;
  36. lblMessage2.Text = "One Record Updated Sucessfully";
  37. }
  38. }
  39. catch (Exception Exc)
  40. {
  41. lblMessage2.Visible = true;
  42. lblMessage2.ForeColor = Color.Red;
  43. lblMessage2.Text = " Application Error : " + Exc.Message;
  44. }
  45. finally
  46. {
  47. //
  48. }
  49. }

58. Then save all this GridView data along with Registration number to the permanent table.

59. Click on the Save All button at the form.

Click on the Save

60. Before that, add validation to the TextBox, so that it will not be empty and provide the Validation Group to the Save All Button.

  1. <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
  2. ControlToValidate="txtRegidtNo" ErrorMessage="* Enter Registration Number"
  3. ForeColor="Red" ValidationGroup="abc">* Enter Registration Number</asp:RequiredFieldValidator>

61. After clicking on the Save All Button, if the TextBox is Empty, you will get an error as in the following.

Clicking on the Sava All Button

62. After completing the validation, provide the code to save all the GridView data to the database.

63. After saving all the GridView data to the database permanent table, delete all the data from the first or temporary table.

64. Click on the Save All Button and write this code.

  1. protected void btnSaveAll_Click(object sender, EventArgs e)
  2. {
  3. // fanally saving all data to the Main table in the database
  4. try
  5. {
  6. string RgNo;
  7. //
  8. if (con.State == ConnectionState.Open)
  9. {
  10. con.Close();
  11. }
  12. if (txtRegidtNo.Text != "")
  13. {
  14. RgNo = txtRegidtNo.Text;
  15. // this will find all the dat from the Gridview row
  16. foreach (GridViewRow row in GridView1.Rows)
  17. {
  18. Name = (row.FindControl("Label1") as Label).Text;
  19. Mobile = Convert.ToInt64((row.FindControl("Label2") as Label).Text);
  20. EmailID = (row.FindControl("Label3") as Label).Text;
  21. Address = (row.FindControl("Label4") as Label).Text;
  22. //
  23. string Query = "insert into tbl_Gridview4 (Regid,Name,Mobile,EmailID,Address) values(@Regid,@Name,@Mobile,@EmailID,@Address)";
  24. //
  25. SqlCommand cmd = new SqlCommand(Query, con);
  26. //
  27. cmd.Parameters.AddWithValue("@Regid", RgNo);
  28. cmd.Parameters.AddWithValue("@Name", Name);
  29. cmd.Parameters.AddWithValue("@Mobile", Mobile);
  30. cmd.Parameters.AddWithValue("@EmailID", EmailID);
  31. cmd.Parameters.AddWithValue("@Address", Address);
  32. //
  33. con.Open();
  34. i = cmd.ExecuteNonQuery();
  35. con.Close();
  36. //
  37. if (i > 0)
  38. {
  39. //once all data is saved, data from the temporary table need to deleted
  40. if (con.State == ConnectionState.Open)
  41. {
  42. con.Close();
  43. }
  44. string Query2 = "truncate table tbl_Temp_Gridview4 ";
  45. //
  46. SqlCommand cmd2 = new SqlCommand(Query2, con);
  47. //
  48. con.Open();
  49. int B = cmd2.ExecuteNonQuery();
  50. con.Close();
  51. //
  52. //
  53. lblMessage2.Visible = true;
  54. lblMessage2.ForeColor = Color.Green;
  55. lblMessage2.Text = "All Data Saved Sucessfully";
  56. //Loading the gridview again
  57. LoadGridView();
  58. //clearing the Gridview
  59. txtRegidtNo.Text = "";
  60. }
  61. }
  62. }
  63. }
  64. catch (Exception Exc)
  65. {
  66. lblMessage2.Visible = true;
  67. lblMessage2.ForeColor = Color.Red;
  68. lblMessage2.Text = " Application Error : " + Exc.Message;
  69. }
  70. finally
  71. {
  72. //
  73. }
  74. }

65. Watch the output again.

Watch The Output

I am using the complete source of the design here again.

  1. <%@ Control Language="C#" AutoEventWireup="true" CodeFile="Gridview_EmptyDataTemplate_Control_4.ascx.cs" Inherits="Tech_Test_Controls_Gridview_Control_4" %>
  2. <style type="text/css">
  3. .Clear
  4. {
  5. clear:both;
  6. }
  7. .logButton
  8. {
  9. width: 100px;
  10. height: 30px;
  11. background-color: #FB5F67;
  12. font-family: Arial, Helvetica, sans-serif; font-size: 13px; font-weight: bold; font-style: normal;
  13. font-variant: normal; text-transform: capitalize; color: #FFFFFF;
  14. }
  15. a:hover {color:#FF00FF;}
  16. a:link {color:blue;}
  17. a:visited {color:green;}
  18. a:active {color:yellow;}
  19. input[type="submit"]:hover {
  20. background:#0000FF;
  21. }
  22. .LoginBox
  23. {
  24. margin: 10px;
  25. height: 270px;
  26. background-color: #FB5F67;
  27. }
  28. .Heading
  29. {
  30. margin: 10px;
  31. height: 15px;
  32. background-color: #FFFFFF;
  33. font-family: Arial, Helvetica, sans-serif; font-size: 12px; font-weight: bold; font-style: normal;
  34. font-variant: normal; text-transform: capitalize; color: #6C6B6B;
  35. text-align: center;
  36. padding: 4px;
  37. }
  38. .InnerLogin
  39. {
  40. margin: 10px;
  41. margin-right: 5px;
  42. width: 320px;
  43. height: 250px;
  44. float: left;
  45. background-color: White;
  46. }
  47. .NewUserBox
  48. {
  49. margin: 10px;
  50. margin-left: 5px;
  51. width: 330px;
  52. height: 250px;
  53. float: left;
  54. background-color: White;
  55. }
  56. .ClickButton
  57. {
  58. width: 100px;
  59. height: 30px;
  60. background-color: #339933;
  61. font-family: Arial, Helvetica, sans-serif; font-size: 13px; font-weight: bold; font-style: normal;
  62. font-variant: normal; text-transform: capitalize; color: #FFFFFF;
  63. }
  64. .ContentInside
  65. {
  66. margin: 10px;
  67. }
  68. .TextBoxSize
  69. {
  70. width: 200px;
  71. }
  72. .SmallTextBox
  73. {
  74. width: 100px;
  75. }
  76. .DropDownSize
  77. {
  78. width: 255px;
  79. height: 23px;
  80. }
  81. .ContentMiddle
  82. {
  83. margin: 0px auto;
  84. height: auto;
  85. width: 500px;
  86. }
  87. .LeftHeading
  88. {
  89. font-family: Arial, Helvetica, sans-serif;
  90. font-size: 14px;
  91. font-weight: bold;
  92. font-style: normal;
  93. font-variant: normal;
  94. text-transform: capitalize;
  95. color: #CC3300;
  96. text-align: left;
  97. }
  98. .SubHeadingLeft
  99. {
  100. margin: 10px;
  101. height: 15px;
  102. padding: 4px;
  103. background-color: #999999;
  104. font-family: Arial, Helvetica, sans-serif;
  105. font-size: 12px;
  106. font-weight: bold;
  107. font-style: normal;
  108. font-variant: normal;
  109. text-transform: capitalize;
  110. color: #FFFFFF;
  111. text-align: left;
  112. }
  113. .SmallButton
  114. {
  115. width: 60px;
  116. height: 30px;
  117. background-color: #666633;
  118. font-family: Arial, Helvetica, sans-serif; font-size: 13px; font-weight: bold; font-style: normal;
  119. font-variant: normal; text-transform: capitalize; color: #FFFFFF;
  120. }
  121. .style1
  122. {
  123. width: 177px;
  124. }
  125. .style2
  126. {
  127. width: 5px;
  128. color: #ff0000;
  129. }
  130. .style3
  131. {
  132. width: 32px;
  133. }
  134. .style4
  135. {
  136. width: 120px;
  137. }
  138. .style5
  139. {
  140. width: 4px;
  141. }
  142. .style6
  143. {
  144. width: 4px;
  145. }
  146. </style>
  147. <div class="Heading">
  148. Working with EmptyDataTemplate, EditItemTemplate and others in GridView</div>
  149. <div class="ContentInside">
  150. <asp:ScriptManagerProxy ID="ScriptManagerProxy1" runat="server">
  151. </asp:ScriptManagerProxy>
  152. </div>
  153. <div class="ContentInside">
  154. <asp:UpdatePanel ID="UpdatePanel1" runat="server" UpdateMode="Conditional">
  155. <ContentTemplate>
  156. <table style="width:100%;">
  157. <tr>
  158. <td class="style3">
  159. </td>
  160. <td class="style1">
  161. </td>
  162. <td class="style6">
  163. </td>
  164. <td class="style2">
  165. </td>
  166. <td>
  167. </td>
  168. <td>
  169. </td>
  170. <td>
  171. </td>
  172. </tr>
  173. <tr>
  174. <td class="style3">
  175. </td>
  176. <td class="style1">
  177. Enter Registration Number</td>
  178. <td class="style6">
  179. :</td>
  180. <td class="style2">
  181. *</td>
  182. <td>
  183. <asp:TextBox ID="txtRegidtNo" runat="server" CssClass="TextBoxSize"
  184. AutoPostBack="True" ontextchanged="txtRegidtNo_TextChanged"></asp:TextBox>
  185. </td>
  186. <td>
  187. </td>
  188. <td>
  189. </td>
  190. </tr>
  191. <tr>
  192. <td class="style3">
  193. </td>
  194. <td class="style1">
  195. </td>
  196. <td class="style6">
  197. </td>
  198. <td class="style2">
  199. </td>
  200. <td>
  201. <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server"
  202. ControlToValidate="txtRegidtNo" ErrorMessage="* Enter Registration Number"
  203. ForeColor="Red" ValidationGroup="abc">* Enter Registration Number</asp:RequiredFieldValidator>
  204. </td>
  205. <td>
  206. </td>
  207. <td>
  208. </td>
  209. </tr>
  210. <tr>
  211. <td class="style3">
  212. </td>
  213. <td class="style1">
  214. </td>
  215. <td class="style6">
  216. </td>
  217. <td class="style2">
  218. </td>
  219. <td>
  220. <asp:Label ID="lblMessage1" runat="server" Text="Message" Visible="False"></asp:Label>
  221. </td>
  222. <td>
  223. </td>
  224. <td>
  225. </td>
  226. </tr>
  227. </table>
  228. </ContentTemplate>
  229. </asp:UpdatePanel>
  230. </div>
  231. <div class="ContentInside">
  232. <table style="width:100%;">
  233. <tr>
  234. <td>
  235. </td>
  236. <td>
  237. Add Items To Below</td>
  238. <td>
  239. </td>
  240. <td>
  241. </td>
  242. </tr>
  243. <tr>
  244. <td>
  245. </td>
  246. <td>
  247. <asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
  248. GridLines="None" AutoGenerateColumns="False" ShowFooter="True"
  249. ShowHeaderWhenEmpty="True" onrowcommand="GridView1_RowCommand"
  250. AllowPaging="True" DataKeyNames="ID"
  251. onpageindexchanging="GridView1_PageIndexChanging"
  252. onrowdeleting="GridView1_RowDeleting" PageSize="4" Width="873px"
  253. onrowediting="GridView1_RowEditing"
  254. onrowcancelingedit="GridView1_RowCancelingEdit"
  255. onrowupdating="GridView1_RowUpdating">
  256. <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
  257. <Columns>
  258. <asp:TemplateField HeaderText="ID">
  259. <%--<EditItemTemplate>
  260. <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("ID") %>'></asp:TextBox>
  261. </EditItemTemplate>--%>
  262. <ItemTemplate>
  263. <asp:Label ID="Label5" runat="server" Text='<%# Bind("ID") %>'></asp:Label>
  264. </ItemTemplate>
  265. </asp:TemplateField>
  266. <asp:TemplateField HeaderText="Name">
  267. <EditItemTemplate>
  268. <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Name") %>'></asp:TextBox>
  269. </EditItemTemplate>
  270. <FooterTemplate>
  271. <asp:TextBox ID="TextBox5" runat="server" CssClass="SmallTextBox"></asp:TextBox>
  272. </FooterTemplate>
  273. <ItemTemplate>
  274. <asp:Label ID="Label1" runat="server" Text='<%# Bind("Name") %>'></asp:Label>
  275. </ItemTemplate>
  276. <ControlStyle Width="100px" />
  277. <FooterStyle Width="100px" />
  278. <HeaderStyle Width="100px" />
  279. <ItemStyle Width="100px" />
  280. </asp:TemplateField>
  281. <asp:TemplateField HeaderText="Mobile">
  282. <EditItemTemplate>
  283. <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Mobile") %>'></asp:TextBox>
  284. </EditItemTemplate>
  285. <FooterTemplate>
  286. <asp:TextBox ID="TextBox6" runat="server" CssClass="SmallTextBox"></asp:TextBox>
  287. </FooterTemplate>
  288. <ItemTemplate>
  289. <asp:Label ID="Label2" runat="server" Text='<%# Bind("Mobile") %>'></asp:Label>
  290. </ItemTemplate>
  291. <ControlStyle Width="100px" />
  292. <FooterStyle Width="100px" />
  293. <HeaderStyle Width="100px" />
  294. <ItemStyle Width="100px" />
  295. </asp:TemplateField>
  296. <asp:TemplateField HeaderText="EmailID">
  297. <EditItemTemplate>
  298. <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("EmailID") %>'></asp:TextBox>
  299. </EditItemTemplate>
  300. <FooterTemplate>
  301. <asp:TextBox ID="TextBox7" runat="server" Width="170px"></asp:TextBox>
  302. </FooterTemplate>
  303. <ItemTemplate>
  304. <asp:Label ID="Label3" runat="server" Text='<%# Bind("EmailID") %>'></asp:Label>
  305. </ItemTemplate>
  306. <ControlStyle Width="180px" />
  307. <FooterStyle Width="180px" />
  308. <HeaderStyle Width="180px" />
  309. <ItemStyle Width="180px" />
  310. </asp:TemplateField>
  311. <asp:TemplateField HeaderText="Address">
  312. <EditItemTemplate>
  313. <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Address") %>'></asp:TextBox>
  314. </EditItemTemplate>
  315. <FooterTemplate>
  316. <asp:TextBox ID="TextBox8" runat="server" CssClass="SmallTextBox" Width="150px"></asp:TextBox>
  317. </FooterTemplate>
  318. <ItemTemplate>
  319. <asp:Label ID="Label4" runat="server" Text='<%# Bind("Address") %>'></asp:Label>
  320. </ItemTemplate>
  321. <ControlStyle Width="150px" />
  322. <FooterStyle Width="150px" />
  323. <HeaderStyle Width="150px" />
  324. <ItemStyle Width="150px" />
  325. </asp:TemplateField>
  326. <asp:TemplateField HeaderText="Add New">
  327. <FooterTemplate>
  328. <asp:Button ID="btnAddGrid2" runat="server" CommandName="AddNew2"
  329. CssClass="SmallButton" Text="Add" />
  330. </FooterTemplate>
  331. <ControlStyle Width="70px" />
  332. <FooterStyle Width="70px" />
  333. <HeaderStyle Width="70px" />
  334. <ItemStyle Width="70px" />
  335. </asp:TemplateField>
  336. <asp:TemplateField HeaderText="Edit / Delete ">
  337. <EditItemTemplate>
  338. <asp:Button ID="btnGridUpdate" runat="server" CssClass="SmallButton"
  339. Text="Update" CommandName="Update" />
  340. <asp:Button ID="btnGridCancel" runat="server" CssClass="SmallButton"
  341. Text="Cancel" CommandName="Cancel" />
  342. </EditItemTemplate>
  343. <ItemTemplate>
  344. <asp:Button ID="btnGridEdit" runat="server" CssClass="SmallButton"
  345. Text="Edit" CommandName="Edit" />
  346. <asp:Button ID="btnGridDelete" runat="server" CssClass="SmallButton"
  347. Text="Delete" CommandName="Delete" OnClientClick="return confirm('Are You Sure You Want To Delete ?')" />
  348. </ItemTemplate>
  349. <HeaderStyle Width="130px" />
  350. </asp:TemplateField>
  351. </Columns>
  352. <EditRowStyle BackColor="#999999" />
  353. <EmptyDataTemplate>
  354. <asp:TextBox ID="TextBox9" runat="server" CssClass="SmallTextBox"></asp:TextBox>
  355. <asp:TextBox ID="TextBox10" runat="server" CssClass="SmallTextBox"></asp:TextBox>
  356. <asp:TextBox ID="TextBox11" runat="server" CssClass="SmallTextBox"
  357. Width="170px"></asp:TextBox>
  358. <asp:TextBox ID="TextBox12" runat="server" CssClass="SmallTextBox"
  359. Width="150px"></asp:TextBox>
  360. <asp:Button ID="btnAddGrid1" runat="server" CommandName="AddNew1"
  361. CssClass="SmallButton" Text="Add New" />
  362. </EmptyDataTemplate>
  363. <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  364. <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
  365. <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
  366. <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
  367. <SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
  368. <SortedAscendingCellStyle BackColor="#E9E7E2" />
  369. <SortedAscendingHeaderStyle BackColor="#506C8C" />
  370. <SortedDescendingCellStyle BackColor="#FFFDF8" />
  371. <SortedDescendingHeaderStyle BackColor="#6F8DAE" />
  372. </asp:GridView>
  373. </td>
  374. <td>
  375. </td>
  376. <td>
  377. </td>
  378. </tr>
  379. <tr>
  380. <td>
  381. </td>
  382. <td>
  383. <asp:Label ID="lblMessage2" runat="server" Text="Message" Visible="False"></asp:Label>
  384. </td>
  385. <td>
  386. </td>
  387. <td>
  388. </td>
  389. </tr>
  390. </table>
  391. <table style="width: 100%;">
  392. <tr>
  393. <td class="style3">
  394. </td>
  395. <td class="style4">
  396. <asp:Button ID="btnSaveAll" runat="server" CssClass="ClickButton"
  397. Text="Save All" onclick="btnSaveAll_Click" ValidationGroup="abc" />
  398. </td>
  399. <td>
  400. </td>
  401. </tr>
  402. <tr>
  403. <td class="style3">
  404. </td>
  405. <td class="style4">
  406. </td>
  407. <td class="style5">
  408. </td>
  409. <td>
  410. <asp:Label ID="lblMessage3" runat="server" Text="Message" Visible="False"></asp:Label>
  411. </td>
  412. <td>
  413. </td>
  414. </tr>
  415. </table>
  416. </div>
  417. <div>
  418. </div>
  419. <div>
  420. </div>

I am using all the programming code here again:

  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.Drawing;
  10. using System.Configuration;
  11. public partial class Tech_Test_Controls_Gridview_Control_4 : System.Web.UI.UserControl
  12. {
  13. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["CONN"].ConnectionString.ToString());
  14. //
  15. int i;
  16. // declar Five variables here
  17. int ID;
  18. string Name, EmailID, Address;
  19. long Mobile;
  20. //
  21. DataTable Dt = new DataTable();
  22. protected void Page_Load(object sender, EventArgs e)
  23. {
  24. if (!IsPostBack)
  25. {
  26. //Loading the Gridview
  27. LoadGridView();
  28. }
  29. }
  30. private void LoadGridView()
  31. {
  32. // This Method will load the Gridview with Initial Data from the Temporary Table
  33. try
  34. {
  35. string Query = "select * from tbl_Temp_Gridview4";
  36. //
  37. SqlCommand cmd = new SqlCommand(Query, con);
  38. SqlDataAdapter da = new SqlDataAdapter();
  39. da.SelectCommand = cmd;
  40. //
  41. con.Open();
  42. da.Fill(Dt);
  43. con.Close();
  44. //
  45. GridView1.DataSource = Dt;
  46. GridView1.DataBind();
  47. }
  48. catch (Exception Exc)
  49. {
  50. lblMessage2.Visible = true;
  51. lblMessage2.ForeColor = Color.Red;
  52. lblMessage2.Text = " Application Error : " + Exc.Message;
  53. }
  54. finally
  55. {
  56. //
  57. }
  58. }
  59. protected void txtRegidtNo_TextChanged(object sender, EventArgs e)
  60. {
  61. // this method is used to check if the Registration number already exist or not
  62. CheckRegistrationNo();
  63. }
  64. private void CheckRegistrationNo()
  65. {
  66. // this method is used to check if the Registration number already exist or not
  67. try
  68. {
  69. string Query = "select Regid from tbl_Gridview4 where Regid=@Regid";
  70. //
  71. SqlCommand cmd = new SqlCommand(Query, con);
  72. cmd.Parameters.AddWithValue("@Regid", txtRegidtNo.Text);
  73. con.Open();
  74. SqlDataReader dr = cmd.ExecuteReader();
  75. if (dr.Read())
  76. {
  77. lblMessage1.Visible = true;
  78. lblMessage1.ForeColor = Color.Red;
  79. lblMessage1.Text = "This Registration Number is Already Exist, Please try Another ...";
  80. //
  81. dr.Close();
  82. con.Close();
  83. }
  84. else
  85. {
  86. dr.Close();
  87. con.Close();
  88. //
  89. lblMessage1.Visible = false;
  90. }
  91. }
  92. catch (Exception Exc)
  93. {
  94. lblMessage1.Visible = true;
  95. lblMessage2.ForeColor = Color.Red;
  96. lblMessage1.Text = " Application Error : " + Exc.Message;
  97. }
  98. finally
  99. {
  100. //
  101. }
  102. }
  103. protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
  104. {
  105. try
  106. {
  107. if (txtRegidtNo.Text == "")
  108. {
  109. lblMessage1.Visible = true;
  110. lblMessage1.ForeColor = Color.Red;
  111. lblMessage1.Text = "Please Enter The Registration Number First";
  112. }
  113. else
  114. {
  115. lblMessage1.Visible = false;
  116. //
  117. //check registration number
  118. CheckRegistrationNo();
  119. GridViewRow row = (GridViewRow)(e.CommandSource as Button).NamingContainer;
  120. // this method will call first when the Gridview is Empty and the EmptyDataTemplate is Called here
  121. if (e.CommandName == "AddNew1")
  122. {
  123. // finding the value from the TextBox inside the EmptyDataTempalte
  124. Name = (row.FindControl("TextBox9") as TextBox).Text;
  125. Mobile = Convert.ToInt64((row.FindControl("TextBox10") as TextBox).Text);
  126. EmailID = (row.FindControl("TextBox11") as TextBox).Text;
  127. Address = (row.FindControl("TextBox12") as TextBox).Text;
  128. //
  129. string Query = "insert into tbl_Temp_Gridview4 (Name,Mobile,EmailID,Address) values(@Name,@Mobile,@EmailID,@Address)";
  130. //
  131. SqlCommand cmd = new SqlCommand(Query, con);
  132. //
  133. cmd.Parameters.AddWithValue("@Name", Name);
  134. cmd.Parameters.AddWithValue("@Mobile", Mobile);
  135. cmd.Parameters.AddWithValue("@EmailID", EmailID);
  136. cmd.Parameters.AddWithValue("@Address", Address);
  137. //
  138. con.Open();
  139. i = cmd.ExecuteNonQuery();
  140. con.Close();
  141. //
  142. if (i > 0)
  143. {
  144. //loading the Gridview
  145. LoadGridView();
  146. //
  147. lblMessage2.Visible = true;
  148. lblMessage2.ForeColor = Color.Green;
  149. lblMessage2.Text = "One Record Add Sucessfully";
  150. }
  151. }
  152. // this method will called in FooterTemplate after one record is added to the Gridview
  153. if (e.CommandName == "AddNew2")
  154. {
  155. // finding the value from the TextBox inside the EmptyDataTempalte
  156. Name = (row.FindControl("TextBox5") as TextBox).Text;
  157. Mobile = Convert.ToInt64((row.FindControl("TextBox6") as TextBox).Text);
  158. EmailID = (row.FindControl("TextBox7") as TextBox).Text;
  159. Address = (row.FindControl("TextBox8") as TextBox).Text;
  160. //
  161. string Query = "insert into tbl_Temp_Gridview4 (Name,Mobile,EmailID,Address) values(@Name,@Mobile,@EmailID,@Address)";
  162. //
  163. SqlCommand cmd = new SqlCommand(Query, con);
  164. //
  165. cmd.Parameters.AddWithValue("@Name", Name);
  166. cmd.Parameters.AddWithValue("@Mobile", Mobile);
  167. cmd.Parameters.AddWithValue("@EmailID", EmailID);
  168. cmd.Parameters.AddWithValue("@Address", Address);
  169. //
  170. con.Open();
  171. i = cmd.ExecuteNonQuery();
  172. con.Close();
  173. //
  174. if (i > 0)
  175. {
  176. //loading the Gridview
  177. LoadGridView();
  178. //
  179. lblMessage2.Visible = true;
  180. lblMessage2.ForeColor = Color.Green;
  181. lblMessage2.Text = "One Record Add Sucessfully";
  182. }
  183. }
  184. }
  185. }
  186. catch (Exception Exc)
  187. {
  188. lblMessage2.Visible = true;
  189. lblMessage2.ForeColor = Color.Red;
  190. lblMessage2.Text = " Application Error : " + Exc.Message;
  191. }
  192. finally
  193. {
  194. //
  195. }
  196. }
  197. protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
  198. {
  199. // Doing Paging here
  200. GridView1.PageIndex = e.NewPageIndex;
  201. //Calling tne Function to load the Gridview
  202. LoadGridView();
  203. }
  204. protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
  205. {
  206. if (con.State == ConnectionState.Open)
  207. {
  208. con.Close();
  209. }
  210. try
  211. {
  212. //retrieving the DataKey name here
  213. ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
  214. string Query = "delete from tbl_Temp_Gridview4 where ID=@ID";
  215. //
  216. SqlCommand cmd = new SqlCommand(Query, con);
  217. cmd.Parameters.AddWithValue("@ID", ID);
  218. //
  219. con.Open();
  220. i = cmd.ExecuteNonQuery();
  221. if (i > 0)
  222. {
  223. //Load the Gridview
  224. LoadGridView();
  225. }
  226. con.Close();
  227. }
  228. catch (Exception Exc)
  229. {
  230. lblMessage2.Visible = true;
  231. lblMessage2.ForeColor = Color.Red;
  232. lblMessage2.Text = " Application Error : " + Exc.Message;
  233. }
  234. finally
  235. {
  236. //
  237. }
  238. }
  239. protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
  240. {
  241. //this line of code will open the Gridview in the Edit mode
  242. GridView1.EditIndex = e.NewEditIndex;
  243. }
  244. protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
  245. {
  246. //this code will Cancel the row edit
  247. GridView1.EditIndex = -1;
  248. //Loading the Gridview again
  249. LoadGridView();
  250. }
  251. protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
  252. {
  253. try
  254. {
  255. //retrieving the DataKey name here
  256. ID = Convert.ToInt32(GridView1.DataKeys[e.RowIndex].Value.ToString());
  257. // ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
  258. Name = (GridView1.Rows[e.RowIndex].FindControl("TextBox1") as TextBox).Text;
  259. Mobile = Convert.ToInt64((GridView1.Rows[e.RowIndex].FindControl("TextBox2") as TextBox).Text);
  260. EmailID = (GridView1.Rows[e.RowIndex].FindControl("TextBox3") as TextBox).Text;
  261. Address = (GridView1.Rows[e.RowIndex].FindControl("TextBox4") as TextBox).Text;
  262. string Query = "update tbl_Temp_Gridview4 set Name=@Name, Mobile=@Mobile, EmailID=@EmailID, Address=@Address where ID=@ID ";
  263. //
  264. SqlCommand cmd = new SqlCommand(Query, con);
  265. //
  266. cmd.Parameters.AddWithValue("@Name", Name);
  267. cmd.Parameters.AddWithValue("@Mobile", Mobile);
  268. cmd.Parameters.AddWithValue("@EmailID", EmailID);
  269. cmd.Parameters.AddWithValue("@Address", Address);
  270. cmd.Parameters.AddWithValue("@ID", ID);
  271. //
  272. con.Open();
  273. i = cmd.ExecuteNonQuery();
  274. con.Close();
  275. //
  276. if (i > 0)
  277. {
  278. // Closing the Editmode
  279. GridView1.EditIndex = -1;
  280. //
  281. //loading the Gridview
  282. LoadGridView();
  283. //
  284. lblMessage2.Visible = true;
  285. lblMessage2.ForeColor = Color.Green;
  286. lblMessage2.Text = "One Record Updated Sucessfully";
  287. }
  288. }
  289. catch (Exception Exc)
  290. {
  291. lblMessage2.Visible = true;
  292. lblMessage2.ForeColor = Color.Red;
  293. lblMessage2.Text = " Application Error : " + Exc.Message;
  294. }
  295. finally
  296. {
  297. //
  298. }
  299. }
  300. protected void btnSaveAll_Click(object sender, EventArgs e)
  301. {
  302. // fanally saving all data to the Main table in the database
  303. try
  304. {
  305. string RgNo;
  306. //
  307. if (con.State == ConnectionState.Open)
  308. {
  309. con.Close();
  310. }
  311. if (txtRegidtNo.Text != "")
  312. {
  313. RgNo = txtRegidtNo.Text;
  314. // this will find all the dat from the Gridview row
  315. foreach (GridViewRow row in GridView1.Rows)
  316. {
  317. Name = (row.FindControl("Label1") as Label).Text;
  318. Mobile = Convert.ToInt64((row.FindControl("Label2") as Label).Text);
  319. EmailID = (row.FindControl("Label3") as Label).Text;
  320. Address = (row.FindControl("Label4") as Label).Text;
  321. //
  322. string Query = "insert into tbl_Gridview4 (Regid,Name,Mobile,EmailID,Address) values(@Regid,@Name,@Mobile,@EmailID,@Address)";
  323. //
  324. SqlCommand cmd = new SqlCommand(Query, con);
  325. //
  326. cmd.Parameters.AddWithValue("@Regid", RgNo);
  327. cmd.Parameters.AddWithValue("@Name", Name);
  328. cmd.Parameters.AddWithValue("@Mobile", Mobile);
  329. cmd.Parameters.AddWithValue("@EmailID", EmailID);
  330. cmd.Parameters.AddWithValue("@Address", Address);
  331. //
  332. con.Open();
  333. i = cmd.ExecuteNonQuery();
  334. con.Close();
  335. //
  336. if (i > 0)
  337. {
  338. //once all data is saved data from the temporary table need to deleted
  339. if (con.State == ConnectionState.Open)
  340. {
  341. con.Close();
  342. }
  343. string Query2 = "truncate table tbl_Temp_Gridview4 ";
  344. //
  345. SqlCommand cmd2 = new SqlCommand(Query2, con);
  346. //
  347. con.Open();
  348. int B = cmd2.ExecuteNonQuery();
  349. con.Close();
  350. //
  351. //
  352. lblMessage2.Visible = true;
  353. lblMessage2.ForeColor = Color.Green;
  354. lblMessage2.Text = "All Data Saved Sucessfully";
  355. //Loading the gridview again
  356. LoadGridView();
  357. //clearing the Gridview
  358. txtRegidtNo.Text = "";
  359. }
  360. }
  361. }
  362. }
  363. catch (Exception Exc)
  364. {
  365. lblMessage2.Visible = true;
  366. lblMessage2.ForeColor = Color.Red;
  367. lblMessage2.Text = " Application Error : " + Exc.Message;
  368. }
  369. finally
  370. {
  371. //
  372. }
  373. }
  374. }