Saturday, November 8, 2008

Migrate DataSet to List<Type> for Data Binding for Silverlight 2 Grid.

I have wrote 2 article on Data binding in Silverlight 2, one with normal traditional “asmx” web services and another with WCF services. you can check them at :

http://pendsevikram.blogspot.com/2008/10/silverlight-2-grid-linq-to-sql.html

http://pendsevikram.blogspot.com/2008/10/silverlight-2-grid-with-silverlight.html

There was very well response to both articles..I got lots of mails from many..But the feedback is how one can do it with DataSet [Traditional Data Container] and pass it over service, without using LINQ or migrating to LINQ.

Well Big thanks to Mr. Mahesh Sabnis who is one of my mentor at consulting department where I am working right now, We did it together and came up with this simple code snippet which is self explanatory and you just need to plug it in your service logic and rest will work fine as it was with LINQ to SQL.

       public List<clsEmployee> GetAllEmployee()

       {

           SqlConnection Conn = new SqlConnection("Data Source=.;Initial
Catalog=Company;Integrated Security=SSPI");

           SqlDataAdapter AdEmp = new SqlDataAdapter("Select * from
Employee",Conn);

           DataSet Ds = new DataSet();

           AdEmp.Fill(Ds,"Employee");

           DataTable DtEmp = Ds.Tables["Employee"];

           List<clsEmployee> listEmp = new List<clsEmployee>();

           foreach (DataRow dr in DtEmp.Rows)

           {

               clsEmployee objEmp = new clsEmployee();

               objEmp.EmpNo = Convert.ToInt32(dr["EmpNo"]);

               objEmp.EmpName = dr["EmpName"].ToString();

               objEmp.Salary= Convert.ToInt32(dr["Salary"]);

               objEmp.DeptNo = Convert.ToInt32(dr["DeptNo"]);

               listEmp.Add(objEmp);

           }

            return listEmp;

      }

Let me know your feedback, I would glad to explore if you have any problem areas specific..will be nice to try it !

Vikram.

No comments: