Convert Datatable to generic list collection in c#
Introduction :In this article i will show you how to convert Datatable to generic list collection in c# . It is very easy to conversion form DataTable to generic list collection .I have showed here as step by step
Class Example
public class City
{
public int CityId { get; set; }
public string Cityname { get; set; }
}
C# Code Convert DataTable to generic list collection :
List<City> CityCollection = new List<City>();
DataSet Ds = GetDataSet("Select CountryID,CountryName from Countrytable");
CityCollection = Ds.Tables[0].AsEnumerable().Select(data => new City() { CityId = (int)data["CountryID"], Cityname = (string)data["CountryName"] }).ToList();
public static DataSet GetDataSet(string Query)
{
DataSet Ds = new DataSet();
try
{
string strCon = @"Data Source=ServerName;Initial Catalog=Test;Integrated Security=True";
SqlConnection Con = new SqlConnection(strCon);
SqlDataAdapter Da = new SqlDataAdapter(Query, Con);
Da.Fill(Ds);
}
catch (Exception) { }
return Ds;
}
Introduction : In this article i will show you how to bind a DropDownList to avoid page refresh via a webmethod .It is very useful and many time we require to use Jquery ajax . I have here provided html and c# code behind code to understand Jquery Ajax in asp.net .
C# Code for binding Dropdownlist using Jquery Ajax
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.Web.Services;
using System.Data.SqlClient;
public partial class AjaxInJquery : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
[WebMethod]
public static List GetCityData()
{
List CityCollection = new List();
try
{
DataSet Ds = GetDataSet("Select * from Countrytable");
CityCollection = Ds.Tables[0].AsEnumerable().Select(data => new City() { CityId = (int)data["CountryID"], Cityname = (string)data["CountryName"] }).ToList();
}
catch (Exception) { }
return CityCollection;
}
public static DataSet GetDataSet(string Query)
{
DataSet Ds = new DataSet();
try
{
string strCon = @"Data Source=ServerName;Initial Catalog=Test;Integrated Security=True";
SqlConnection Con = new SqlConnection(strCon);
SqlDataAdapter Da = new SqlDataAdapter(Query, Con);
Da.Fill(Ds);
}
catch (Exception) { }
return Ds;
}
}
public class City
{
public int CityId { get; set; }
public string Cityname { get; set; }
}
Order by Ascending and Descending class List<> in C# :
Introduction : In this article i will explain you how to sort class collection ( List<> of class ) in linq c# . I done the easy code to understand how to uses Order by Ascending and Descending of class list in linq c#.
Class example for sorting class List<> In c#: Following is the user class example with constructor in c#.
public class User
{
public int UserId { get; set; }
public string Firstname { get; set; }
public User(int userId, string firstname)
{
UserId = userId;
Firstname = firstname;
}
}
Ascending and Descending Sorting class list in linq C#:
List<User> UserList = new List<User>();
UserList.Add(new User(1, "bimal"));
UserList.Add(new User(2, "punit"));
UserList.Add(new User(3, "amit"));
UserList = UserList.OrderBy(x => x.Firstname).ToList(); //Ascending Sorted the class list using linq.
UserList = UserList.OrderByDescending(x => x.Firstname).ToList(); // Descending Sorted the class list using linq.
Add,Update,Delete,paging with DetailsView in asp.net c#
Introduction: In this article i will show you how to bind DetailsView in asp.net c# . I have implemented all operation like paging ,add ,edit and delete in DetailsView in asp.net c# .
In following i have written the Css class to format the DetaildView.
Html Code for DetailsView in asp.net :
Introduction : In this article i will show you how to export GridView to excel file. Many times we require to export the GridView to excel sheet file.I have here written following code in asp.net c# to export the gridview control to excel sheet file. Html code for gridview export to excel sheet :
Introduction : In this article i will show how to detect browser in Jquery . Many times we require to detect browser and need to do code accordingly as per browser. By using following code you can detect Internet Explorer Browser ,Mozila Browser ,Safari Browser In Jquery
Jquery Code to Detect Browser :
<script src="Scripts/jquery-1.4.1-vsdoc.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.js" type="text/javascript"></script>
<script src="Scripts/jquery-1.4.1.min.js" type="text/javascript"></script>
<script type="text/javascript">
$(document).ready(function () {
if ($.browser.msie) { alert('Internet Explorer and Version is ' + $.browser.version) }
if ($.browser.safari) { alert('Safari Browser .') }
if ($.browser.mozilla) { alert('Mozila Browser and Version is ' + $.browser.version) }
});
</script>