web 2.0

ASP.NET(vb.net) & ListView - SQL Server 2000,2005,2008 - System.Data.SqlClient

ASP.NET(vb.net) & ListView - SQL Server 2000,2005,2008 - System.Data.SqlClient Example scripts how to use ListView control in asp.net , Using Connector System.Data.SqlClient namespace is the .NET Framework Data Provider for SQL Server,  how to the connect to Microsoft SQL Server (2000,2005,2008) Database.

ShotDev Focus:
- XXXXXXXXXXXXX

Example

ListView1.aspx

  1. <%@ Page Language="VB" AutoEventWireup="false" CodeFile="ListView1.aspx.vb" Inherits="ListView1" %>  
  2.   
  3. <%@ Register assembly="System.Web.Extensions, Version=3.5.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35" namespace="System.Web.UI.WebControls" tagprefix="asp" %>  
  4.   
  5. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
  6.   
  7. <html xmlns="http://www.w3.org/1999/xhtml">  
  8. <head runat="server">  
  9. <title>ShotDev.Com Tutorial</title>  
  10. </head>  
  11. <body>  
  12. <form id="form1" runat="server">  
  13. <asp:ListView ID="myListView" runat="server" DataKeyNames="CustomerID">  
  14.   
  15. <LayoutTemplate>  
  16. <table>  
  17. <tr>  
  18. <td>  
  19. <table id="Table1" runat="server" border="1">  
  20. <tr>  
  21. <th id="Th1" runat="server">  
  22. Select</th>  
  23. <th id="Th2" runat="server">  
  24. CustomerID</th>  
  25. <th id="Th3" runat="server">  
  26. Name</th>  
  27. <th id="Th4" runat="server">  
  28. Email</th>  
  29. <th id="Th5" runat="server">  
  30. CountryCode</th>  
  31. <th id="Th6" runat="server">  
  32. Budget</th>  
  33. <th id="Th7" runat="server">  
  34. Used</th>  
  35. </tr>  
  36. <tr ID="itemPlaceholder" runat="server">  
  37. </tr>  
  38. </table>  
  39. </td>  
  40. </tr>  
  41. </table>  
  42. </LayoutTemplate>  
  43.   
  44. <ItemTemplate>  
  45. <tr>  
  46. <td>  
  47. <asp:CheckBox id="chkCustomerID" runat="server"/>  
  48. </td>  
  49. <td>  
  50. <asp:Label ID="lblCustomerID" runat="server"/>  
  51. </td>  
  52. <td>  
  53. <asp:Label ID="lblName" runat="server"/>  
  54. </td>  
  55. <td>  
  56. <asp:Label ID="lblEmail" runat="server"/>  
  57. </td>  
  58. <td>  
  59. <asp:Label ID="lblCountryCode" runat="server" />  
  60. </td>  
  61. <td>  
  62. <asp:Label ID="lblBudget" runat="server" />  
  63. </td>  
  64. <td>  
  65. <asp:Label ID="lblUsed" runat="server" />  
  66. </td>  
  67. </tr>  
  68. </ItemTemplate>  
  69.   
  70. </asp:ListView>  
  71.   
  72. <br />  
  73. <asp:Button id="Button1" onclick="Button1_Click" runat="server" Text="Submit"></asp:Button>  
  74. <hr />  
  75. <asp:Label id="lblText" runat="server"></asp:Label>  
  76. </form>  
  77. </body>  
  78. </html>  

ListView1.aspx.vb

  1. Imports System.Data  
  2. Imports System.Data.SqlClient  
  3.   
  4. Partial Class ListView1  
  5. Inherits System.Web.UI.Page  
  6.   
  7. Dim objConn As SqlConnection  
  8. Dim objCmd As SqlCommand  
  9.   
  10. Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgs) Handles Me.Load  
  11. Dim strConnString As String  
  12. strConnString = "Server=localhost;UID=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"  
  13. objConn = New SqlConnection(strConnString)  
  14. objConn.Open()  
  15.   
  16. If Not Page.IsPostBack() Then  
  17. BindData()  
  18. End If  
  19. End Sub  
  20.   
  21. Protected Sub BindData()  
  22. Dim strSQL As String  
  23. strSQL = "SELECT * FROM customer"  
  24.   
  25. Dim dtReader As SqlDataReader  
  26. objCmd = New SqlCommand(strSQL, objConn)  
  27. dtReader = objCmd.ExecuteReader()  
  28.   
  29. '*** BindData to ListView ***'  
  30. myListView.DataSource = dtReader  
  31. myListView.DataBind()  
  32.   
  33. dtReader.Close()  
  34. dtReader = Nothing  
  35.   
  36. End Sub  
  37.   
  38. Sub Page_UnLoad()  
  39. objConn.Close()  
  40. objConn = Nothing  
  41. End Sub  
  42.   
  43. Protected Sub myListView_ItemDataBound(ByVal sender As ObjectByVal e As ListViewItemEventArgs) Handles myListView.ItemDataBound  
  44.   
  45. Dim lvDataItem As ListViewDataItem = CType(e.Item, ListViewDataItem)  
  46.   
  47. '*** CustomerID ***'  
  48. Dim lblCustomerID As Label = CType(e.Item.FindControl("lblCustomerID"), Label)  
  49. If Not IsNothing(lblCustomerID) Then  
  50. lblCustomerID.Text = lvDataItem.DataItem("CustomerID")  
  51. End If  
  52.   
  53. '*** Name ***'  
  54. Dim lblName As Label = CType(e.Item.FindControl("lblName"), Label)  
  55. If Not IsNothing(lblName) Then  
  56. lblName.Text = lvDataItem.DataItem("CustomerID")  
  57. End If  
  58.   
  59. '*** Email ***'  
  60. Dim lblEmail As Label = CType(e.Item.FindControl("lblEmail"), Label)  
  61. If Not IsNothing(lblEmail) Then  
  62. lblEmail.Text = lvDataItem.DataItem("Email")  
  63. End If  
  64.   
  65. '*** CountryCode ***'  
  66. Dim lblCountryCode As Label = CType(e.Item.FindControl("lblCountryCode"), Label)  
  67. If Not IsNothing(lblCountryCode) Then  
  68. lblCountryCode.Text = lvDataItem.DataItem("CountryCode")  
  69. End If  
  70.   
  71. '*** Budget ***'  
  72. Dim lblBudget As Label = CType(e.Item.FindControl("lblBudget"), Label)  
  73. If Not IsNothing(lblBudget) Then  
  74. lblBudget.Text = lvDataItem.DataItem("Budget")  
  75. End If  
  76.   
  77. '*** Used ***'  
  78. Dim lblUsed As Label = CType(e.Item.FindControl("lblUsed"), Label)  
  79. If Not IsNothing(lblUsed) Then  
  80. lblUsed.Text = lvDataItem.DataItem("Used")  
  81. End If  
  82. End Sub  
  83.   
  84. Sub Button1_Click(ByVal sender As ObjectByVal e As EventArgs) Handles Button1.Click  
  85. Dim chkCusID As CheckBox  
  86. Dim lblID As Label  
  87. Dim i As Integer  
  88. lblText.Text = ""  
  89. For i = 0 To myListView.Items.Count - 1  
  90. chkCusID = myListView.Items(i).FindControl("chkCustomerID")  
  91. lblID = myListView.Items(i).FindControl("lblCustomerID")  
  92. If chkCusID.Checked = True Then  
  93. '*** Have lblID.Text ***'  
  94. Me.lblText.Text = Me.lblText.Text & "<br>" & lblID.Text  
  95. End If  
  96. Next  
  97. End Sub  
  98.   
  99. End Class  

Create a asp.net file and save to path root-path/dotnet/

Run
http://localhost/dotnet/ListView1.aspx

Screenshot

ASP.NET(vb.net) & ListView - SQL Server 2000,2005,2008 - System.Data.SqlClient
.
.
.

Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (No Ratings Yet)
Loading ... Loading ...

Leave a Reply

You must be logged in to post a comment.