how to get unique values from xml file and bind in dropdown list using 3 tier architecture with Linq to xml

Rajkumardotnet90
Posted by Rajkumardotnet90 under LINQ category on | Points: 40 | Views : 4609
Create a design page like follows in your .aspx Page.................
<table>
<tr>
<td>
<span>Category:</span></td>
<td>
<asp:DropDownList ID="ddcategory" runat="server" ></asp:DropDownList></td></tr>

</table>


create xml file as and save as doc1.xml


<?xml version="1.0" encoding="utf-8"?>
<products>

<product>
<product_id>1008</product_id>
<category>menswear</category>
<subcategory>shirt</subcategory>
</product>

<product>
<product_id>1208</product_id>
<category>menswear</category>
<subcategory>shirt</subcategory>
</product>

<product>
<product_id>1209</product_id>
<category>menswear</category>
<subcategory>shirt</subcategory>
</product>

<product>
<product_id>1030</product_id>
<category>menswear</category>
<subcategory>shirt</subcategory>
</product>

<product>
<product_id>1301</product_id>
<category>menswear</category>
<subcategory>shirt</subcategory>
</product>

<product>
<product_id>1008</product_id>
<category>menswear</category>
<subcategory>Pant</subcategory>
</product>

<product>
<product_id>1008</product_id>
<category>menswear</category>
<subcategory>brief</subcategory>
</product>

<product>
<product_id>1008</product_id>
<category>menswear</category>
<subcategory>shoe</subcategory>
</product>

<product>
<product_id>2008</product_id>
<category>Kidsswear</category>
<subcategory>tshirt</subcategory>
</product>

<product>
<product_id>2308</product_id>
<category>Kidsswear</category>
<subcategory>shirt</subcategory>
</product>

<product>
<product_id>2234</product_id>
<category>Kidsswear</category>
<subcategory>pant</subcategory>
</product>

</products>
.....................................................

i Have implement this function with 3 tier architecture so care to take the following steps....

Create a class in App_code folder and named as PROPERTY.cs

then paste the following code


public class CatSubgory
{
string a_cat, a_subcat;
public string cat
{
get { return a_cat; }
set { a_cat = value; }
}
public string subcat
{
get { return a_subcat; }
set{a_subcat = value;}
}
}
class MycatComparer : IEqualityComparer<CatSubgory>
{
public bool Equals(CatSubgory item1, CatSubgory item2)
{
return item1.cat.Equals(item2.cat);
}

public int GetHashCode(CatSubgory item)
{
return item.cat.GetHashCode();
}
}

create another class named as DAL.cs
add the following namespace in your dal class


public List<CatSubgory> Categoryload()
{
XDocument xdoc = XDocument.Load(HttpContext.Current.Server.MapPath("doc1.xml"));//ur xml file path..

try
{
var q = (from dd in xdoc.Descendants("product")
select new CatSubgory
{
cat = dd.Element("category").Value
}).Distinct(new MycatComparer());//for comparing unique values
return q.ToList();
}
catch
{
throw;
}
finally
{
xdoc = null;
}
}

create another class named as BAL.cs

public List<CatSubgory > CategoryLoad()
{
DAL Odal = new DAL();
try
{
return Odal.Categoryload ();
}
catch
{
throw;
}
finally
{
Odal = null;
}

}

your page load add the following code

protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
BinDropdown();
}

}


public void BinDropdown()
{
BAL Obal = new BAL();
try
{
List<CatSubgory> li = Obal.CategoryLoad();
if (li.ToList().Count > 0)
{
ddcategory.DataSource = li.ToList();
ddcategory.DataTextField = "cat";
ddcategory.DataBind();
ddcategory.Items.Insert(0, new ListItem("--Select--", ""));
}
else
{
Response.Write("Sry No records found");
}
}
catch
{
throw;
}
finally
{
Obal=null;
}
}

happy Programmers .......Share everything to everyone ............

Comments or Responses

Login to post response