C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Communities   Interview   Jobs   Projects   Offshore Development    
Silverlight Tutorials | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Revenue Sharing |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...

New Feature: Community Sites: Create your own .NET community website and start earning from Google AdSense ! It's Free !




Transfer the data of an Arraylist into the Data table in C#.Net


Posted Date: 28 May 2007    Resource Type: Articles    Category: .NET Framework

Posted By: rekha kanda       Member Level: Bronze
Rating:     Points: 10



Introduction



The following sample code show how to transfer the data of an Arraylist into the Datatable

Paragraph Heading 1




using System;
using System.Collections;
using System.Text;
using System.Data;
using System.Collections;

namespace MyConverter
{
class Converters
{
public static DataTable ToDataTable(ArrayList alist)
{ DataTable dt = new DataTable();

if (alist[0] == null)
throw new FormatException("Parameter ArrayList empty");
dt.TableName = alist[0].GetType().Name;
DataRow dr;
System.Reflection.PropertyInfo [] propInfo =alist[0].GetType().GetProperties();
for(int i=0; i< propInfo.Length;i++)
{
dt.Columns.Add(propInfo[i].Name, propInfo[i].PropertyType);
}

for(int row =0;row < alist.Count ; row++)
{
dr = dt.NewRow();
for (int i=0;i< propInfo.Length;i++)
{
object tempObject =alist[row];

object t = propInfo[i].GetValue(tempObject, null);
/*object t =tempObject.GetType().InvokeMember(propInfo[i].Name,
R.BindingFlags.GetProperty , null,tempObject , new object [] {});*/
if (t!=null)
dr[i] = t.ToString();
}
dt.Rows.Add(dr);
}
return dt;
}

public static DataTable ToDataTable(ArrayList alist, ArrayList alColNames)
{
DataTable dt = new DataTable();

if (alist[0] == null)
throw new FormatException("Parameter ArrayList empty");
dt.TableName = alist[0].GetType().Name;
DataRow dr;
System.Reflection.PropertyInfo[] propInfo = alist[0].GetType().GetProperties();
for (int i = 0; i < propInfo.Length; i++)
{
for (int j = 0; j < alColNames.Count; j++)
{
if (alColNames[j].ToString() == propInfo[i].Name)
{
dt.Columns.Add(propInfo[i].Name, propInfo[i].PropertyType);
break;
}
}
}

for (int row = 0; row < alist.Count; row++)
{
dr = dt.NewRow();
for (int i = 0; i < dt.Columns.Count; i++)
{
object tempObject = alist[row];

object t = propInfo[i].GetValue(tempObject, null);
/*object t =tempObject.GetType().InvokeMember(propInfo[i].Name,
R.BindingFlags.GetProperty , null,tempObject , new object [] {});*/
if (t != null)
dr[i] = t.ToString();
}
dt.Rows.Add(dr);
}
return dt;
}
}
}



Paragraph Heading N





Summary







Responses

Author: Arthy    09 Oct 2008Member Level: Silver   Points : 1
The is copied from http://www.codeproject.com/KB/dotnet/Collection_to_table.aspx

:(


Feedbacks      
Popular Tags   What are tags ?   Search Tags  
Transfer the data of an Arraylist into the Data table in C#.Net  .  

Post Feedback


This is a strictly moderated forum. Only approved messages will appear in the site. Please use 'Spell Check' in Google toolbar before you submit.
You must Sign In to post a response.
Next Resource: Practical Implementation of MSMQ Using ASP.NET and SQL SERVER
Previous Resource: Reading data from an XML file using C#
Return to Discussion Resource Index
Post New Resource
Category: .NET Framework


Post resources and earn money!
 
Related Resources



dotNet Slackers   BizTalk Adaptors    Web Design

silicone halloween mask

Contact Us    Privacy Policy    Terms Of Use