web 2.0

ASP.NET(vb.net) & SQL Server and GridView

ASP.NET(vb.net) & SQL Server and GridView - This is  example scripts how to use ASP.NET get rows data record from SQL Server database and view show all result to GridView control.

ShotDev Focus:
- ASP.NET(vb.net) & SQL Server and GridView

Example

AspNetSQLServerGridView.aspx

  1. <%@ Import Namespace="System.Data"%>  
  2. <%@ Import Namespace="System.Data.SqlClient"%>  
  3. <%@ Page Language="VB" %>  
  4. <script runat="server">  
  5. Dim objConn As SqlConnection  
  6. Dim objCmd As SqlCommand  
  7.   
  8. Sub Page_Load(sender As Object, e As EventArgs)  
  9. Dim strConnString As String  
  10. strConnString = "Server=localhost;Uid=sa;PASSWORD=;database=mydatabase;Max Pool Size=400;Connect Timeout=600;"  
  11. objConn = New SqlConnection(strConnString)  
  12. objConn.Open()  
  13.   
  14. BindData()  
  15. End Sub  
  16.   
  17. Sub BindData()  
  18. Dim strSQL As String  
  19. strSQL = "SELECT * FROM customer"  
  20.   
  21. Dim dtReader As SqlDataReader  
  22. objCmd = New SqlCommand(strSQL, objConn)  
  23. dtReader = objCmd.ExecuteReader()  
  24.   
  25. '*** BindData to GridView ***'  
  26. myGridView.DataSource = dtReader  
  27. myGridView.DataBind()  
  28.   
  29. dtReader.Close()  
  30. dtReader = Nothing  
  31.   
  32. End Sub  
  33.   
  34. Sub Page_UnLoad()  
  35. objConn.Close()  
  36. objConn = Nothing  
  37. End Sub  
  38.   
  39. Private Sub myGridView_RowDataBound(sender As Object, e As GridViewRowEventArgs)  
  40. '*** CustomerID ***'  
  41. Dim lblCustomerID As Label = CType(e.Row.FindControl("lblCustomerID"),Label)  
  42. IF Not IsNothing(lblCustomerID) Then  
  43. lblCustomerID.Text = e.Row.DataItem("CustomerID")  
  44. End IF  
  45.   
  46. '*** Name ***'  
  47. Dim lblName As Label = CType(e.Row.FindControl("lblName"),Label)  
  48. IF Not IsNothing(lblName) Then  
  49. lblName.Text = e.Row.DataItem("Name")  
  50. End IF  
  51.   
  52. '*** Email ***'  
  53. Dim lblEmail As Label = CType(e.Row.FindControl("lblEmail"),Label)  
  54. IF Not IsNothing(lblEmail) Then  
  55. lblEmail.Text = e.Row.DataItem("Email")  
  56. End IF  
  57.   
  58. '*** CountryCode ***'  
  59. Dim lblCountryCode As Label = CType(e.Row.FindControl("lblCountryCode"),Label)  
  60. IF Not IsNothing(lblCountryCode) Then  
  61. lblCountryCode.Text = e.Row.DataItem("CountryCode")  
  62. End IF  
  63.   
  64. '*** Budget ***'  
  65. Dim lblBudget As Label = CType(e.Row.FindControl("lblBudget"),Label)  
  66. IF Not IsNothing(lblBudget) Then  
  67. lblBudget.Text = FormatNumber(e.Row.DataItem("Budget"),2)  
  68. End IF  
  69.   
  70. '*** Used ***'  
  71. Dim lblUsed As Label = CType(e.Row.FindControl("lblUsed"),Label)  
  72. IF Not IsNothing(lblUsed) Then  
  73. lblUsed.Text = FormatNumber(e.Row.DataItem("Used"),2)  
  74. End IF  
  75. End Sub  
  76.   
  77. </script>  
  78. <html>  
  79. <head>  
  80. <title>ShotDev.Com Tutorial</title>  
  81. </head>  
  82. <body>  
  83. <form id="form1" runat="server">  
  84. <asp:GridView id="myGridView" runat="server" AutoGenerateColumns="False" onRowDataBound="myGridView_RowDataBound">  
  85.   
  86. <Columns>  
  87.   
  88. <asp:TemplateField HeaderText="CustomerID">  
  89. <ItemTemplate>  
  90. <asp:Label id="lblCustomerID" runat="server"></asp:Label>  
  91. </ItemTemplate>  
  92. </asp:TemplateField>  
  93.   
  94. <asp:TemplateField HeaderText="Name">  
  95. <ItemTemplate>  
  96. <asp:Label id="lblName" runat="server"></asp:Label>  
  97. </ItemTemplate>  
  98. </asp:TemplateField>  
  99.   
  100. <asp:TemplateField HeaderText="Email">  
  101. <ItemTemplate>  
  102. <asp:Label id="lblEmail" runat="server"></asp:Label>  
  103. </ItemTemplate>  
  104. </asp:TemplateField>  
  105.   
  106. <asp:TemplateField HeaderText="CountryCode">  
  107. <ItemTemplate>  
  108. <asp:Label id="lblCountryCode" runat="server"></asp:Label>  
  109. </ItemTemplate>  
  110. </asp:TemplateField>  
  111.   
  112. <asp:TemplateField HeaderText="Budget">  
  113. <ItemTemplate>  
  114. <asp:Label id="lblBudget" runat="server"></asp:Label>  
  115. </ItemTemplate>  
  116. </asp:TemplateField>  
  117.   
  118. <asp:TemplateField HeaderText="Used">  
  119. <ItemTemplate>  
  120. <asp:Label id="lblUsed" runat="server"></asp:Label>  
  121. </ItemTemplate>  
  122. </asp:TemplateField>  
  123.   
  124. </Columns>  
  125. </asp:GridView>  
  126. </form>  
  127. </body>  
  128. </html>  

Screenshot

ASP.NET(vb.net) & SQL Server and GridView
.
.
.
Download this script.
Download

1 Star2 Stars3 Stars4 Stars5 Stars6 Stars7 Stars8 Stars9 Stars10 Stars (1 votes, average: 1.00 out of 10)
Loading ... Loading ...

Leave a Reply

You must be logged in to post a comment.