How to get the Data In List and retrive data from this list to string with LINQ
Firslty create a table from which we have to retrive data .
Create table category_name(
id tinyint IDENTITY(1,1),
category_name varchar(200),
cat_id tinyint
)
and insert some value in this table .In this table we enter some category Id like 1,2,3,4,5
insert into category_name
values('Football',1),
('Cricket',1),
('Rugby',1),
('Hockey',1),
('Tennis',1),
('Polo',1),
('Horse Racing',1),
('Musical',2),
('Ballet',2),
('Classical music',2),
('Opera',2),
('Traditional art',4),
('Modern art',4),
('Historical',4),
('Military',4),
('Religious',4),
('wine',5),
('whiskey',5),
('liquor',5),
('Others',5),
('Kosher',3),
('Halal',3),
('No red meat',3),
('No fish',3),
('No shell fish',3),
('Dairy free',3)
After createion and insertion in table Now we come to ASp.net code .
Make a class (name DTCategory ) in App_code folder for Set the property like below
*You are free to add any name according you. I USe the DTCategory and call this everywhere where its requirement*
public class DTCategory
{
public string CategoryName
{ get; set; }
public Int16 ID
{ get; set; }
}
we make above because we have to pass this in our list for getting more than one value .
After this add another file in your App_code folder and put method for get all category name as well as ID from database .Lets this file name is
DALGetName.
Do'nt forget to add common namespace like System.Collection
put this code into DALGetName
public List<DTCategory> GetAllCategoryName()
{
List<DTCategory> lst = new List<DTCategory>();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "proc_get_all_category_name";
try
{
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
DTCategory dto = new DTCategory();
dto.CategoryName= Convert.ToString(dr["catergory_name"]);
dto.ID = Convert.ToInt16(dr["id"]);
lst.Add(dto);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
return lst;
}
=============================
Now your Appcode work is Finish . now lets come in own aspx page where we want to retrive this with LINQ.
In above i get all the categoryname as well as Id in List . But our requirment is get the data in ID wise and bind it into DropDown List are any string . So we use the LINQ to get data
with our requirement .
Firstly we have to declare this is in above the page load. Dont forgot to add namespace System.Text
public string _allsports = "";
public string _allarts = "";
public string _allcultural = "";
public string _allgastronomy = "";
public string _alldietry = "";
protected void GetAllCategoryName()
{
#region /*This region is used for get the data dynamically */
DALGetName objdalcat = new DALGetName(); // Call this method into your .cs page
List<DTCategory> lstCategory = new List<DTCategory>();
lstCategory = objdalcat.GetAllCategoryName();
//Bind Sport
var QuerySports = from x in lstCategory
where x.Id== 1
select x;
StringBuilder sbSports = new StringBuilder();
foreach (DTOContactCategoryName i in QuerySports)
{
sbSports.Append("<option value=" + i.ID + ">" + i.CategoryName + "</option>");
}
_allsports = sbSports.ToString();
//Bind Arts
var QueryArts = from x in lstCategory
where x.Id== 2
select x;
StringBuilder sbArt = new StringBuilder();
foreach (DTOContactCategoryName i in QueryArts)
{
sbArt.Append("<option value=" + i.ID + " >" + i.CategoryName + "</option>");
}
_allarts = sbArt.ToString();
//Bind Cultural
var QueryCultural = from x in lstCategory
where x.Id== 4
select x;
StringBuilder sbCultural = new StringBuilder();
foreach (DTOContactCategoryName i in QueryCultural)
{
sbCultural.Append("<option value=" + i.ID + " >" + i.CategoryName + "</option>");
}
_allcultural = sbCultural.ToString();
//Bind Gastronomy
var QueryGastronomy = from x in lstCategory
where x.Id== 5
select x;
StringBuilder sbGastronomy = new StringBuilder();
foreach (DTOContactCategoryName i in QueryGastronomy)
{
sbGastronomy.Append("<option value=" + i.ID + ">" + i.CategoryName + "</option>");
}
_allgastronomy = sbGastronomy.ToString();
//Bind Other one Category which ID is 3
var QueryDietary = from x in lstCategory
where x.Id== 3
select x;
StringBuilder sbDietary = new StringBuilder();
foreach (DTOContactCategoryName i in QueryDietary)
{
sbDietary.Append("<option value=" + i.ID + ">" + i.CategoryName + "</option>");
}
_alldietry = sbDietary.ToString();
#endregion
}
call this method on pageload method
====================in .aspx page we have some dropdown list and we bind the data which is Bind into this .cs Page
<select class="ddlOptions">
<%=_allsports %>
</select>
<select class="ddlOptions">
<%=_allcultural %>
</select>
<select class="ddlOptions">
<%=_allarts %>
</select>
<select class="ddlOptions">
<%=_allgastronomy %>
</select>
<select class="ddlOptions">
<%=_alldietry %>
</select>
Hope this will help you . If any issue then let me know
Firslty create a table from which we have to retrive data .
Create table category_name(
id tinyint IDENTITY(1,1),
category_name varchar(200),
cat_id tinyint
)
and insert some value in this table .In this table we enter some category Id like 1,2,3,4,5
insert into category_name
values('Football',1),
('Cricket',1),
('Rugby',1),
('Hockey',1),
('Tennis',1),
('Polo',1),
('Horse Racing',1),
('Musical',2),
('Ballet',2),
('Classical music',2),
('Opera',2),
('Traditional art',4),
('Modern art',4),
('Historical',4),
('Military',4),
('Religious',4),
('wine',5),
('whiskey',5),
('liquor',5),
('Others',5),
('Kosher',3),
('Halal',3),
('No red meat',3),
('No fish',3),
('No shell fish',3),
('Dairy free',3)
After createion and insertion in table Now we come to ASp.net code .
Make a class (name DTCategory ) in App_code folder for Set the property like below
*You are free to add any name according you. I USe the DTCategory and call this everywhere where its requirement*
public class DTCategory
{
public string CategoryName
{ get; set; }
public Int16 ID
{ get; set; }
}
we make above because we have to pass this in our list for getting more than one value .
After this add another file in your App_code folder and put method for get all category name as well as ID from database .Lets this file name is
DALGetName.
Do'nt forget to add common namespace like System.Collection
put this code into DALGetName
public List<DTCategory> GetAllCategoryName()
{
List<DTCategory> lst = new List<DTCategory>();
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "proc_get_all_category_name";
try
{
con.Open();
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
DTCategory dto = new DTCategory();
dto.CategoryName= Convert.ToString(dr["catergory_name"]);
dto.ID = Convert.ToInt16(dr["id"]);
lst.Add(dto);
}
}
catch (Exception ex)
{
throw ex;
}
finally
{
con.Close();
}
return lst;
}
=============================
Now your Appcode work is Finish . now lets come in own aspx page where we want to retrive this with LINQ.
In above i get all the categoryname as well as Id in List . But our requirment is get the data in ID wise and bind it into DropDown List are any string . So we use the LINQ to get data
with our requirement .
Firstly we have to declare this is in above the page load. Dont forgot to add namespace System.Text
public string _allsports = "";
public string _allarts = "";
public string _allcultural = "";
public string _allgastronomy = "";
public string _alldietry = "";
protected void GetAllCategoryName()
{
#region /*This region is used for get the data dynamically */
DALGetName objdalcat = new DALGetName(); // Call this method into your .cs page
List<DTCategory> lstCategory = new List<DTCategory>();
lstCategory = objdalcat.GetAllCategoryName();
//Bind Sport
var QuerySports = from x in lstCategory
where x.Id== 1
select x;
StringBuilder sbSports = new StringBuilder();
foreach (DTOContactCategoryName i in QuerySports)
{
sbSports.Append("<option value=" + i.ID + ">" + i.CategoryName + "</option>");
}
_allsports = sbSports.ToString();
//Bind Arts
var QueryArts = from x in lstCategory
where x.Id== 2
select x;
StringBuilder sbArt = new StringBuilder();
foreach (DTOContactCategoryName i in QueryArts)
{
sbArt.Append("<option value=" + i.ID + " >" + i.CategoryName + "</option>");
}
_allarts = sbArt.ToString();
//Bind Cultural
var QueryCultural = from x in lstCategory
where x.Id== 4
select x;
StringBuilder sbCultural = new StringBuilder();
foreach (DTOContactCategoryName i in QueryCultural)
{
sbCultural.Append("<option value=" + i.ID + " >" + i.CategoryName + "</option>");
}
_allcultural = sbCultural.ToString();
//Bind Gastronomy
var QueryGastronomy = from x in lstCategory
where x.Id== 5
select x;
StringBuilder sbGastronomy = new StringBuilder();
foreach (DTOContactCategoryName i in QueryGastronomy)
{
sbGastronomy.Append("<option value=" + i.ID + ">" + i.CategoryName + "</option>");
}
_allgastronomy = sbGastronomy.ToString();
//Bind Other one Category which ID is 3
var QueryDietary = from x in lstCategory
where x.Id== 3
select x;
StringBuilder sbDietary = new StringBuilder();
foreach (DTOContactCategoryName i in QueryDietary)
{
sbDietary.Append("<option value=" + i.ID + ">" + i.CategoryName + "</option>");
}
_alldietry = sbDietary.ToString();
#endregion
}
call this method on pageload method
====================in .aspx page we have some dropdown list and we bind the data which is Bind into this .cs Page
<select class="ddlOptions">
<%=_allsports %>
</select>
<select class="ddlOptions">
<%=_allcultural %>
</select>
<select class="ddlOptions">
<%=_allarts %>
</select>
<select class="ddlOptions">
<%=_allgastronomy %>
</select>
<select class="ddlOptions">
<%=_alldietry %>
</select>
Hope this will help you . If any issue then let me know
Thanks
ROHIT SRIVASTAVA
No comments:
Post a Comment