OleCommand and OleParameter : OleCommand « ADO.net Database « ASP.NET Tutorial

Home
ASP.NET Tutorial
1.ASP.Net Instroduction
2.Language Basics
3.ASP.net Controls
4.HTML Controls
5.Page Lifecycle
6.Response
7.Collections
8.Validation
9.Development
10.File Directory
11.Sessions
12.Cookie
13.Cache
14.Custom Controls
15.Profile
16.Configuration
17.LINQ
18.ADO.net Database
19.Data Binding
20.Ajax
21.Authentication Authorization
22.I18N
23.Mobile
24.WebPart
25.XML
ASP.NET Tutorial » ADO.net Database » OleCommand 
18.11.1.OleCommand and OleParameter
<%@ Page Language="C#" %>
<%@ import Namespace="System.Data" %>
<%@ import Namespace="System.Data.OleDb" %>
<script runat="server">

    void Page_Load(object sender, EventArgs e) {
        if (!Page.IsPostBack)
        {
            OleDbConnection myConnection = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE="+ Server.MapPath("EmployeeDatabase.mdb;"));
    
            try{
                string CommandTextPublisher = "SELECT ID, FirstName FROM Employee";
                OleDbCommand myCommandPublishers = new OleDbCommand(CommandTextPublisher, myConnection);
    
                myConnection.Open();
    
                DropDownList1.DataSource = myCommandPublishers.ExecuteReader();
                DropDownList1.DataTextField = "FirstName";
                DropDownList1.DataValueField = "ID";
                DropDownList1.DataBind();
                DropDownList1.Items.Insert(0new ListItem("-- All Publishers --""0"));
            }
            catch (Exception ex){
                throw(ex);
            }
            finally{
                myConnection.Close();
            }
        }
    }
    
    void ApplyFilter_Click(Object sender, EventArgs e) {
        OleDbConnection myConnection = new OleDbConnection("PROVIDER=Microsoft.Jet.OLEDB.4.0;DATA SOURCE="+ Server.MapPath("EmployeeDatabase.mdb;"));
    
        try{
            string CommandTextBooks = "SELECT Book.BookTitle, Publisher.PublisherName FROM Book INNER JOIN Publisher on Book.BookPublisherID = Publisher.PublisherID WHERE ?=0 OR Book.BookPublisherID=? ORDER BY Book.BookTitle";
            OleDbCommand myCommandBooks = new OleDbCommand(CommandTextBooks, myConnection);
    
            OleDbParameter myParameter1 = new OleDbParameter();
            myParameter1.ParameterName = "@pub1";
            myParameter1.OleDbType = OleDbType.Integer;
            myParameter1.Value = DropDownList1.SelectedValue;
            myCommandBooks.Parameters.Add (myParameter1);

            OleDbParameter myParameter2 = new OleDbParameter();
            myParameter2.ParameterName = "@pub2";
            myParameter2.OleDbType = OleDbType.Integer;
            myParameter2.Value = DropDownList1.SelectedValue;
            myCommandBooks.Parameters.Add (myParameter2);
    
            myConnection.Open();
    
            DataGrid1.DataSource = myCommandBooks.ExecuteReader();
            DataGrid1.DataBind();
        }
        catch (Exception ex){
            throw(ex);
        }
        finally{
            myConnection.Close();
        }
    }

</script>
<html>
<head>
</head>
<body>
    <form runat="server">
        
            Select a Publisher: 
            <asp:DropDownList id="DropDownList1" runat="server"></asp:DropDownList>
            &nbsp; 
            <asp:Button id="Button1" onclick="ApplyFilter_Click" runat="server" Text="Show Titles"></asp:Button>
        
        <asp:datagrid id="DataGrid1" runat="server" EnableViewState="False">
            <HeaderStyle font-bold="True" forecolor="white" backcolor="#4A3C8C"></HeaderStyle>
            <ItemStyle backcolor="#DEDFDE"></ItemStyle>
        </asp:datagrid>
    </form>
</body>
</html>
18.11.OleCommand
18.11.1.OleCommand and OleParameter
java2s.com  | Contact Us | Privacy Policy
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.