We took one TextBox and ListBox and on a button click it will move it to the ListBox and on another button click it will be saved to the database.

Initial chamber

Step 1

Open your Visual Studio 2010 and create an Empty Website, provide a suitable name (textListBox_demo).

Step 2

In Solution Explorer you get your empty website, then add two web forms and a SQL Server Database as in the following.

For Web Form:

textListBox_demo (your empty website) then right-click then select Add New Item -> Web Form. Name it textListBox_demo.aspx.

For SQL Server Database

textListBox_demo (your empty website) then right-click then select Add New Item -> SQL Server Database. (Add a database inside the App_Data_folder).

DATABASE CHAMBER

Step 3

In Server Explorer, click on your database (Database.mdf) then select Tables -> Add New Table. Make the table like this.

Table tbl_data (Don't Forget to make ID as IS Identity -- True)


table

Design chamber

Step 4

Now open your textListBox_demo.aspx file, where we create our design for GridView sorting.

TextListBox_demo.aspx

  1. <%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
  2. <%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="asp" %>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  4. <html xmlns="http://www.w3.org/1999/xhtml">
  5. <head runat="server">
  6. <title></title>
  7. <style type="text/css">
  8. .style1
  9. {
  10. width: 184px;
  11. }
  12. .style2
  13. {
  14. width: 169px;
  15. }
  16. </style>
  17. </head>
  18. <body>
  19. <form id="form1" runat="server">
  20. <asp:ToolkitScriptManager ID="ToolkitScriptManager1" runat="server">
  21. </asp:ToolkitScriptManager>
  22. <div>
  23. <table style="width:100%;">
  24. <tr>
  25. <td class="style1">
  26. </td>
  27. <td class="style2">
  28. </td>
  29. <td>
  30. </td>
  31. </tr>
  32. <tr>
  33. <td class="style1">
  34. <asp:TextBox ID="TextBox1" runat="server" BorderColor="#9933FF"
  35. BorderStyle="Solid"></asp:TextBox>
  36. </td>
  37. <td class="style2">
  38. <asp:Button ID="Button1" runat="server" onclick="Button1_Click"
  39. Text="Move to ListBox" />
  40. </td>
  41. <td>
  42. <asp:ListBox ID="ListBox1" runat="server" Width="200px" BackColor="#CCCCFF"
  43. ForeColor="#0033CC"></asp:ListBox>
  44. </td>
  45. </tr>
  46. <tr>
  47. <td class="style1">
  48. <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="TextBox1" ForeColor="Red" runat="server" ErrorMessage="Please Provide some text to textbox"></asp:RequiredFieldValidator>
  49. </td>
  50. <td class="style2">
  51. <asp:Label ID="lbmsg" runat="server"></asp:Label>
  52. </td>
  53. <td>
  54. <asp:Button ID="Button2" runat="server" onclick="Button2_Click"
  55. Text="Save to Database" Visible="False" />
  56. </td>
  57. </tr>
  58. </table>
  59. </div>
  60. </form>
  61. </body>
  62. </html>
Your design looks like this:

design
Code chamber

Step 5

Open your textListBox_demo.aspx.cs and write some code so that our application works.

textlistobx_demo.aspx.cs
  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. public partial class _Default : System.Web.UI.Page
  10. {
  11. protected void Page_Load(object sender, EventArgs e)
  12. {
  13. }
  14. protected void Button1_Click(object sender, EventArgs e)
  15. {
  16. if (this.TextBox1.Text != null)
  17. {
  18. ListBox1.Items.Add(this.TextBox1.Text);
  19. Button2.Visible = true;
  20. }
  21. }
  22. protected void Button2_Click(object sender, EventArgs e)
  23. {
  24. SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True");
  25. SqlCommand cmd = new SqlCommand("insert into tbl_data (data) values (@data)", con);
  26. cmd.Parameters.AddWithValue("data", ListBox1.Text);
  27. con.Open();
  28. int i = cmd.ExecuteNonQuery();
  29. con.Close();
  30. if (i!=0)
  31. {
  32. lbmsg.Text = "Your Data have been saved to Database";
  33. lbmsg.ForeColor = System.Drawing.Color.ForestGreen;
  34. }
  35. }
  36. }
Output chamber

data move to listbox

Output

I hope you like this. Thank you for reading. Have a good day.