Code Simplified – Viral Sarvaiya

Code Simplified – Viral Sarvaiya, Web Developer Friends, dot net Developer, Sql Server Developer

Posts Tagged ‘datatable’

Convert data from Generic List to DataTable.

Posted by Viral Sarvaiya on May 9, 2013


Today i am sharing very good a function which convert all Generic list’s data to Datatable is as below

public DataTable LINQToDataTable<T>(IEnumerable<T> varlist)
{
  DataTable dtReturn = new DataTable();

  // column names
  PropertyInfo[] oProps = null;

  if (varlist == null) return dtReturn;

  foreach (T rec in varlist)
  {
    // Use reflection to get property names, to create table, Only first time, others will follow
    if (oProps == null)
    {
      oProps = ((Type)rec.GetType()).GetProperties();
      foreach (PropertyInfo pi in oProps)
      {
        Type colType = pi.PropertyType;

        if ((colType.IsGenericType) && (colType.GetGenericTypeDefinition() == typeof(Nullable<>)))
        {
          colType = colType.GetGenericArguments()[0];
        }

        dtReturn.Columns.Add(new DataColumn(pi.Name, colType));
      }
    }
    DataRow dr = dtReturn.NewRow();

    foreach (PropertyInfo pi in oProps)
    {
      dr[pi.Name] = pi.GetValue(rec, null) == null ? DBNull.Value : pi.GetValue(rec, null);
    }

    dtReturn.Rows.Add(dr);
  }
  return dtReturn;
}

Hope this will helps you.
Thanks.

Advertisements

Posted in .Net, C#, General, LINQ | Tagged: , , , , , , , , , , | Leave a Comment »

DataTable to CSV File and CSV file to Dataset in asp.net

Posted by Viral Sarvaiya on April 21, 2011


Hi,

here i give you 2 simple function for the export Datatable/Dataset to CSV file format and import CSV file to dataset/DataTable.

1. DataTable/DataSet to CSV file

private void DataTableToCSVFile(string filename)
{
HttpContext context = HttpContext.Current;
context.Response.Clear();
foreach (DataColumn column in dtExcelUpdown.Columns)
{
context.Response.Write(column.ColumnName + ",");
}
context.Response.Write(Environment.NewLine);
foreach (DataRow row in dtExcelUpdown.Rows)
{
for (int i = 0; i < dtExcelUpdown.Columns.Count; i++)
{
context.Response.Write(row[i].ToString().Replace(",", string.Empty) + ",");
}
context.Response.Write(Environment.NewLine);
}
context.Response.ContentType = "text/csv";
context.Response.AppendHeader("Content-Disposition", "attachment; filename=" + filename + ".csv");
context.Response.End();
}

2. CSV file to DataSet/DataTable

from the upload control you have to save to directory and then this path and file name give as a parameter of the function.


private DataSet GetCVSFile(string pathName, string fileName)
{
OleDbConnection ExcelConnection = new OleDbConnection(@"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + pathName + ";Extended Properties=Text;");
OleDbCommand ExcelCommand = new OleDbCommand(@"SELECT * FROM " + fileName, ExcelConnection);

OleDbDataAdapter ExcelAdapter = new OleDbDataAdapter(ExcelCommand);

ExcelConnection.Open();

DataSet ExcelDataSet = new DataSet();
ExcelAdapter.Fill(ExcelDataSet);

ExcelConnection.Close();
return ExcelDataSet;
}

thanks…

enjoy coding…..

Posted in .Net, ASP.NET, asp.net feature | Tagged: , , , , , , , , | Leave a Comment »

Removing Duplicate Records from Dataset/DataTable

Posted by Viral Sarvaiya on September 27, 2010


Hello friends….

Hear i demonstrate, how to remove the rows from the dataset that have duplicate rows.

Duplicate DataTable looks like,

Name                                         Company

Viral                                          BNF Tech

Sandeep                                   Gateway Tech

Dharmik                                   Om info

Malhar                                       BNF Tech

Viral                                           BNF Tech

Dharmik                                   Om info

Viral                                           BNF Tech

Sandeep                                   Gateway Tech

now this is the function that remove the duplicate rows from the DataTable as below,

public DataTable DuplicateRowRemove(DataTable dt, string Col)
 {
 Hashtable hTable = new Hashtable();
 ArrayList ArrDupli = new ArrayList();

 foreach (DataRow r in dt.Rows)
 {
 if (HeshTbl.Contains(r[Col]))
 ArrDupli.Add(r);
 else
 HeshTbl.Add(r[Col], string.Empty);

}

 foreach (DataRow R in ArrDupli)
 dt.Rows.Remove(R);

 return dt;
 }

so, after the removing the duplicate data DataTable shows as blow,

Name                                         Company

Viral                                          BNF Tech

Sandeep                                   Gateway Tech

Dharmik                                   Om info

Malhar                                       BNF Tech

thanks you…..

Malhar                                       BNF Tech

Posted in ASP.NET | Tagged: , , , , | 9 Comments »

How to Use datatable in asp.net

Posted by Viral Sarvaiya on July 3, 2009


hear i explain the use of the data table

first u have to take a new data table


Dim myDataTable As New DataTable

then take a column


Dim myDataColumn As DataColumn

give the name of the collumns


myDataColumn = New DataColumn()
myDataColumn.DataType = Type.GetType("System.String")
myDataColumn.ColumnName = "First column name"
myDataTable.Columns.Add(myDataColumn)

myDataColumn = New DataColumn()
myDataColumn.DataType = Type.GetType("System.String")
myDataColumn.ColumnName = "Second column name"
myDataTable.Columns.Add(myDataColumn)

take a row of the datatable


Dim row As DataRow

give the value to that row


row = myDataTable.NewRow()

row("First column Name") = value
row("Second column Name") = value

and finnaly add that row to the table.


myDataTable.Rows.Add(row)

now you can use mydatatable as data table as per requirement of the project

Posted in ASP.NET | Tagged: , , , | Leave a Comment »

 
%d bloggers like this: