Inline binding DataSet : SqlDataAdapter « ADO.net Database « ASP.NET Tutorial






<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.SqlClient" %>
<script runat="server">
    DataSet myDataSet = new DataSet();
    
    void Page_Load(object sender, EventArgs e) {
       string ConnectionString = Convert.ToString(ConfigurationSettings.AppSettings["MSDEConnectString"]);
       string CommandText = "SELECT * FROM Publisher";
    
       SqlConnection myConnection = new SqlConnection(ConnectionString);
       SqlCommand myCommand = new SqlCommand(CommandText, myConnection);
    
       SqlDataAdapter myAdapter = new SqlDataAdapter();
    
       myAdapter.SelectCommand = myCommand;
    
       try {
          myConnection.Open();
          myAdapter.Fill(myDataSet,"Publisher");
       } catch (Exception ex) {
          throw (ex);
       } finally {
          myConnection.Close();
       }
       Page.DataBind();
    
    }

</script>
<html>
<body>
    <form runat="server">
        
            <asp:Label id="lblName" runat="server"> Name
            : <%# DataBinder.Eval (myDataSet.Tables["Publisher"].Rows[0], "[PublisherName]") %> </asp:Label>
            <br />
            City: <asp:Label id="lblCity" runat="server" text='<%# DataBinder.Eval (myDataSet.Tables["Publisher"].Rows[0], "[PublisherCity]") %>'> </asp:Label>
            <br />
            Contact : 
            <asp:HyperLink id="hypEmail" runat="server" NavigateUrl='<%# DataBinder.Eval (myDataSet.Tables["Publisher"].Rows[0], "[PublisherContact_Email]", "mailto:{0}") %>' Text='<%# DataBinder.Eval (myDataSet.Tables["Publisher"].Rows[0], "[PublisherContact_Email]") %>'></asp:HyperLink>
            <br />
            Homesite: 
            <asp:HyperLink id="hypWebsite" runat="server" NavigateUrl='<%# DataBinder.Eval (myDataSet.Tables["Publisher"].Rows[0], "[PublisherWebsite]") %>'>
                <%# DataBinder.Eval (myDataSet.Tables["Publisher"].Rows[0], "[PublisherWebsite]") %> 
            </asp:HyperLink>
        
        
            <asp:Label id="lblError" runat="server"></asp:Label>
        
    </form>
</body>
</html>


File: Web.config

<configuration>
    <appSettings>
        <add key="MSDEConnectString" value="server=(local)\YourDatabase;database=Books;uid=YourID;pwd=letmein;" />
    </appSettings>
</configuration>








18.5.SqlDataAdapter
18.5.1.Inline binding DataSet
18.5.2.Using an object of SqlDataAdapter to fill a DataTable (C#)
18.5.3.Using an object of SqlDataAdapter to fill a DataTable (VB)
18.5.4.FillLoadOption property of the Fill method on data adapters.