This article explains the following:

Database Chamber

Use the following table and Stored Procedure to demonstrate this concept.

  1. Create table Mas_Employee
  2. (
  3. Id int primary key identity(1,1),
  4. Name varchar(50)
  5. )
  6. --To get all the employees
  7. Create Procedure USP_Select_Mas_Employee
  8. @Id int = null
  9. AS
  10. Begin
  11. Select E.Id, E.Name
  12. From Mas_Employee E
  13. Where Id = Isnull(@Id, Id)
  14. End
  15. --To Insert employee details
  16. Create Procedure USP_Insert_Mas_Employee
  17. @Name varchar(50)
  18. AS
  19. Begin
  20. Insert into Mas_Employee(Name) Values(@Name)
  21. End
  22. --To Delete employee details
  23. Create Procedure USP_Delete_Mas_Employee
  24. @Id int
  25. AS
  26. Begin
  27. Delete From Mas_Employee
  28. where Id=@Id
  29. End
Application Chamber

To create the project -

Web.Config:

Create the connection string in the Web.Config file as in the following code snippet:

  1. <connectionStrings>
  2. <add name="conStr"
  3. connectionString="Password= 1234; User ID=sa; Database=DB_CsharpCorner; Data Source=."
  4. providerName="System.Data.SqlClient"/>
  5. </connectionStrings>
Next: Right-click on Solution Explorer and add a web form to your project.

Webform Design:

Design you Webform (.aspx page) as in the following:
  1. <form id="form1" runat="server">
  2. <div style="width: 100%;" align="center">
  3. <fieldset style="width: 40%;">
  4. <legend>Delete Selected item from ListBox and Database</legend>
  5. <table style="width: 100%;">
  6. <tr>
  7. <td>
  8. Name:
  9. </td>
  10. <td>
  11. <asp:TextBox ID="txtEmpName" runat="server"></asp:TextBox>
  12. </td>
  13. <td>
  14. <asp:Button ID="btnAdd" runat="server" Text="Add" OnClick="btnAdd_Click" />
  15. </td>
  16. </tr>
  17. <tr>
  18. <td colspan="3" style="text-align:center;">
  19. <asp:Label ID="lblMsg" runat="server"></asp:Label>
  20. </td>
  21. </tr>
  22. <tr>
  23. <td colspan="2">
  24. <asp:ListBox ID="lstEmployee" runat="server"></asp:ListBox>
  25. </td>
  26. <td>
  27. <asp:Button ID="btnDelete" runat="server" Text="Delete Selected Item" OnClick="btnDelete_Click" />
  28. </td>
  29. </tr>
  30. </table>
  31. </fieldset>
  32. </div>
  33. </form>
CodeBehind:

Add the following namespaces:
  1. using System.Data;
  2. using System.Data.SqlClient;
  3. using System.Configuration;
  4. using System.Drawing;
Invoke the ConnectionString from Web.Config as in the following:
  1. SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["conStr"].ConnectionString);
User Defined Functions:
  1. #region User Defined Methods
  2. //To Clear the fields
  3. private void Clear()
  4. {
  5. txtEmpName.Text = string.Empty;
  6. lblMsg.Text = string.Empty;
  7. }
  8. //To bind listBox from Database
  9. private void BindListBox()
  10. {
  11. SqlDataAdapter adp = new SqlDataAdapter("USP_Select_Mas_Employee", con);
  12. adp.SelectCommand.CommandType = CommandType.StoredProcedure;
  13. DataSet ds = new DataSet();
  14. adp.Fill(ds);
  15. if (ds != null && ds.Tables.Count > 0 && ds.Tables[0].Rows.Count > 0)
  16. {
  17. lstEmployee.DataSource = ds;
  18. lstEmployee.DataValueField = "Id";
  19. lstEmployee.DataTextField = "Name";
  20. lstEmployee.DataBind();
  21. }
  22. }
  23. //To delete the selected item from Database
  24. private void DeleteSelectedItem(int Id)
  25. {
  26. SqlCommand cmd = new SqlCommand("USP_Delete_Mas_Employee", con);
  27. cmd.CommandType = CommandType.StoredProcedure;
  28. cmd.Parameters.AddWithValue("@Id", Id);
  29. if (con.State == ConnectionState.Closed)
  30. {
  31. con.Open();
  32. }
  33. int result = cmd.ExecuteNonQuery();
  34. if (result > 0)
  35. {
  36. Clear();
  37. //BindListBox();
  38. lblMsg.Text = "Employee deleted successfully";
  39. lblMsg.ForeColor = Color.Green;
  40. }
  41. }
  42. //To add the employee from textbox to listBox
  43. private void AddEmployee(string Name)
  44. {
  45. SqlCommand cmd = new SqlCommand("USP_Insert_Mas_Employee", con);
  46. cmd.CommandType = CommandType.StoredProcedure;
  47. cmd.Parameters.AddWithValue("@Name", Name);
  48. if (con.State == ConnectionState.Closed)
  49. {
  50. con.Open();
  51. }
  52. int result = cmd.ExecuteNonQuery();
  53. if (result > 0)
  54. {
  55. Clear();
  56. BindListBox();
  57. lblMsg.Text = "Employee added successfully";
  58. lblMsg.ForeColor = Color.Green;
  59. }
  60. }
  61. #endregion
Page Event Handlers:
  1. #region Page Event Handlers
  2. protected void Page_Load(object sender, EventArgs e)
  3. {
  4. lblMsg.Text = string.Empty;
  5. if (!Page.IsPostBack)
  6. {
  7. BindListBox();
  8. }
  9. }
  10. protected void btnAdd_Click(object sender, EventArgs e)
  11. {
  12. if (txtEmpName.Text.ToString() != string.Empty || txtEmpName.Text.ToString() != null)
  13. AddEmployee(txtEmpName.Text.ToString());
  14. else
  15. {
  16. lblMsg.Text = "Please provide the Name";
  17. lblMsg.ForeColor = Color.Red;
  18. return;
  19. }
  20. }
  21. protected void btnDelete_Click(object sender, EventArgs e)
  22. {
  23. if (Convert.ToInt32(lstEmployee.SelectedValue) < 0)
  24. {
  25. lblMsg.Text = "Please select an item";
  26. lblMsg.ForeColor = Color.Red;
  27. return;
  28. }
  29. else
  30. {
  31. for (int i = lstEmployee.Items.Count - 1; i >= 0; i--)
  32. {
  33. if (lstEmployee.Items[i].Selected)
  34. {
  35. DeleteSelectedItem(Convert.ToInt32(lstEmployee.Items[i].Value));
  36. lstEmployee.Items.Remove(lstEmployee.Items[i]);
  37. }
  38. }
  39. }
  40. }
  41. #endregion
Output:

Enter the name and click on Add.

Enter the name

Then the output will be as in the following screenshot:

output

Select the ListBox Item and click Delete Selected Item button then it will be Deleted in ListBox as well as in the database.

Select the ListBox

I hope you enjoyed this article. Please provide your valuable suggestions and feedback to make this article much reliable.