How to Get Data from EnterpriseLibrary 5.0


Bind Dropdownlist using Enterpriselibrary 5.0.

Your aspx page like

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title
>
</head>
<
body>
    <form id="form1" runat="server">
    <div>
      City:  <asp:DropDownList ID="ddl_city" runat="server">
        </asp:DropDownList>
    </div>
    </form
>
</body>
</
html>

Your .cs file like

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.Common;
using System.Data.SqlClient;
using System.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Common;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Data.Sql;

public partial class _Default : System.Web.UI.Page
{
    private string Connectionstring = ConfigurationManager.ConnectionStrings["connection"].ToString();
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindCityDropdownlist();

        }
    }

    private void BindCityDropdownlist()
    {
        DataSet ds = new DataSet();
        Database objDB = new SqlDatabase(this.Connectionstring);
        string SPName = "Get_CityStateAreaInqSrc";

        using (DbCommand objCMD = objDB.GetStoredProcCommand(SPName))
        {
            objDB.AddInParameter(objCMD, "@Action", DbType.Int64, 2);
            try
            {
                ds = objDB.ExecuteDataSet(objCMD);
            }
            catch (Exception ex)
            {
                EventLog objLOG = new EventLog();
                objLOG.LogError(ex);
                throw ex;
            }
        }

        if (ds != null && ds.Tables[0].Rows.Count > 0)
        {
            ddl_city.DataSource = ds.Tables[0];
            ddl_city.DataTextField = "Name";
            ddl_city.DataValueField = "Code";
            ddl_city.DataBind();
        }
    }

}
 


Similar Articles