SqlDataReader Demo : SqlDataReader « ADO.net Database « ASP.Net






SqlDataReader Demo

<%@ Import namespace="System.Data" %>
<%@ Import namespace="System.Data.SqlClient" %>

<html>
  <head>
    <title>Data Reader</title>
  </head>
  <body>
    <h1>Data Reader</h1>
    Total Products listed = 
    <asp:DataGrid id="dgCount" runat="server"
                  BorderWidth=0 ShowHeader=False />
    <br/>
    <asp:DataGrid id="dgData" runat="server" />
  </body>
</html>

<script language="VB" runat="server">
Sub Page_Load(Source As Object, E As EventArgs)
  Dim strConnection As String = "Data Source=whsql-v08.prod.mesa1.secureserver.net;Initial Catalog=DB_49907;User ID=java2suser;Password='password';"
  Dim objConnection As New SqlConnection(strConnection)
  objConnection.Open()

  Dim strSQLcount As String
  strSQLcount = "SELECT COUNT(FirstName) FROM Employee"

  Dim objCommand As New SqlCommand(strSQLcount, objConnection)
  Dim objReader As SqlDataReader = objCommand.ExecuteReader()

  dgCount.DataSource = objReader
  dgCount.DataBind()

  objReader.Close()

  Dim strProcName As String
  strProcName = "SELECT * FROM Employee"
  objCommand.CommandText = strProcName

  objReader = objCommand.ExecuteReader()

  dgData.DataSource = objReader
  dgData.DataBind()

  objConnection.Close()
End Sub
</script>

           
       








Related examples in the same category

1.Get data from SqlDataReader
2.Bind SqlDataReader to GridView