We will bind some data from the database and we are using SQLDataSource.

Initial chamber

Step 1: Open Visual Studio 2010 and create an empty website. Give a suitable name Listview_demo.

Step 2: In Solution Explorer you will get your empty website. Add a web form, SQL Database. By going like the following:

For Web Form:

Listview_demo (Your Empty Website) - Right Click, Add New Item, then Web Form. Name it Listview_demo.aspx.

For SQL Server Database:

Listview_demo (Your Empty Website) - Right Click, Add New Item, then SQL Server Database. Add Database inside the App_Data_folder.

Database chamber

Step 3: Go to your database Database.mdf, we will create a table - tbl_Data. Go to database.mdf - Table and add New table. Design your table like the following:

Table - tbl_data (Don’t forget to make ID, Identity Specification - Yes)

table design

Show Table Data


table

Design chamber

Step 5: Now open your Listview_demo.aspx file, where we create our design for BINDING ListView using SQLDataSource.

ListView_demo.aspx

  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></title>
  6. </head>
  7. <body>
  8. <form id="form1" runat="server">
  9. <div>
  10. <asp:ListView ID="ListView1" runat="server" DataKeyNames="Product ID"
  11. DataSourceID="SqlDataSource1">
  12. <AlternatingItemTemplate>
  13. <li style="">Product ID:
  14. <asp:Label ID="Product_IDLabel" runat="server"
  15. Text='<%# Eval("[Product ID]") %>' />
  16. <br />
  17. Product Name:
  18. <asp:Label ID="Product_NameLabel" runat="server"
  19. Text='<%# Eval("[Product Name]") %>' />
  20. <br />
  21. Quantity:
  22. <asp:Label ID="QuantityLabel" runat="server" Text='<%# Eval("Quantity") %>' />
  23. <br />
  24. Unit Price:
  25. <asp:Label ID="Unit_PriceLabel" runat="server"
  26. Text='<%# Eval("[Unit Price]") %>' />
  27. <br />
  28. </li>
  29. </AlternatingItemTemplate>
  30. <EditItemTemplate>
  31. <li style="">Product ID:
  32. <asp:Label ID="Product_IDLabel1" runat="server"
  33. Text='<%# Eval("[Product ID]") %>' />
  34. <br />
  35. Product Name:
  36. <asp:TextBox ID="Product_NameTextBox" runat="server"
  37. Text='<%# Bind("[Product Name]") %>' />
  38. <br />
  39. Quantity:
  40. <asp:TextBox ID="QuantityTextBox" runat="server"
  41. Text='<%# Bind("Quantity") %>' />
  42. <br />
  43. Unit Price:
  44. <asp:TextBox ID="Unit_PriceTextBox" runat="server"
  45. Text='<%# Bind("[Unit Price]") %>' />
  46. <br />
  47. <asp:Button ID="UpdateButton" runat="server" CommandName="Update"
  48. Text="Update" />
  49. <asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
  50. Text="Cancel" />
  51. </li>
  52. </EditItemTemplate>
  53. <EmptyDataTemplate>
  54. No data was returned.
  55. </EmptyDataTemplate>
  56. <InsertItemTemplate>
  57. <li style="">Product Name:
  58. <asp:TextBox ID="Product_NameTextBox" runat="server"
  59. Text='<%# Bind("[Product Name]") %>' />
  60. <br />
  61. Quantity:
  62. <asp:TextBox ID="QuantityTextBox" runat="server"
  63. Text='<%# Bind("Quantity") %>' />
  64. <br />
  65. Unit Price:
  66. <asp:TextBox ID="Unit_PriceTextBox" runat="server"
  67. Text='<%# Bind("[Unit Price]") %>' />
  68. <br />
  69. <asp:Button ID="InsertButton" runat="server" CommandName="Insert"
  70. Text="Insert" />
  71. <asp:Button ID="CancelButton" runat="server" CommandName="Cancel"
  72. Text="Clear" />
  73. </li>
  74. </InsertItemTemplate>
  75. <ItemSeparatorTemplate>
  76. <br />
  77. </ItemSeparatorTemplate>
  78. <ItemTemplate>
  79. <li style="">Product ID:
  80. <asp:Label ID="Product_IDLabel" runat="server"
  81. Text='<%# Eval("[Product ID]") %>' />
  82. <br />
  83. Product Name:
  84. <asp:Label ID="Product_NameLabel" runat="server"
  85. Text='<%# Eval("[Product Name]") %>' />
  86. <br />
  87. Quantity:
  88. <asp:Label ID="QuantityLabel" runat="server" Text='<%# Eval("Quantity") %>' />
  89. <br />
  90. Unit Price:
  91. <asp:Label ID="Unit_PriceLabel" runat="server"
  92. Text='<%# Eval("[Unit Price]") %>' />
  93. <br />
  94. </li>
  95. </ItemTemplate>
  96. <LayoutTemplate>
  97. <ul ID="itemPlaceholderContainer" runat="server" style="">
  98. <li runat="server" id="itemPlaceholder" />
  99. </ul>
  100. <div style="">
  101. </div>
  102. </LayoutTemplate>
  103. <SelectedItemTemplate>
  104. <li style="">Product ID:
  105. <asp:Label ID="Product_IDLabel" runat="server"
  106. Text='<%# Eval("[Product ID]") %>' />
  107. <br />
  108. Product Name:
  109. <asp:Label ID="Product_NameLabel" runat="server"
  110. Text='<%# Eval("[Product Name]") %>' />
  111. <br />
  112. Quantity:
  113. <asp:Label ID="QuantityLabel" runat="server" Text='<%# Eval("Quantity") %>' />
  114. <br />
  115. Unit Price:
  116. <asp:Label ID="Unit_PriceLabel" runat="server"
  117. Text='<%# Eval("[Unit Price]") %>' />
  118. <br />
  119. </li>
  120. </SelectedItemTemplate>
  121. </asp:ListView>
  122. <asp:SqlDataSource ID="SqlDataSource1" runat="server"
  123. ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
  124. SelectCommand="SELECT * FROM [tbl_data]"></asp:SqlDataSource>
  125. </div>
  126. </form>
  127. </body>
  128. </html>
Output chamber

listview from database

Hope you liked this. Thank you for reading.