RSS

upload csv file in silverlight

10 Mar

upload csv file in silverlight with header.

read csv file and write into database with class…….

//Create List with class

List<tbltest> lArrObjclsGridLoadExcelData = new List<tbltest>();

//Read CSV File and write in class
public void lFnLoadCSVDataInGrid()
{
try
{

lArrObjclsGridLoadExcelData.Clear();
int j = 1;

OpenFileDialog dlgOpen = new OpenFileDialog();
dlgOpen.Filter = “CSV Files (*.csv)|*.csv”;
bool? res = dlgOpen.ShowDialog();

if (res == true)
{

txtCSV.MaskedText = dlgOpen.File.Name;
txtCSV.IsReadOnly = true;

StreamReader lObjStreamReader = new StreamReader(dlgOpen.File.OpenRead());

bool lBlnIsColumnRow = true;

while (lObjStreamReader.Read() != null)
{
string lStrLine = lObjStreamReader.ReadLine();

if (lBlnIsColumnRow)
{
lBlnIsColumnRow = false;
continue;
}

if (lStrLine == null)
break;

if (lStrLine.Trim() == “”)
continue;

string[] lArrStrCells = null;

lArrStrCells = lStrLine.Split(“,”.ToCharArray());

if (lArrStrCells == null)
continue;

if (!(lArrStrCells.Length == 11))
continue;

tbltest lObjclsGridLoadExcelData = new tbltest();

lObjclsGridLoadExcelData.Leadidpk = j;
lObjclsGridLoadExcelData.Listidfk = j;

lObjclsGridLoadExcelData.Phonecode = Convert.ToSByte(lArrStrCells[0]);
lObjclsGridLoadExcelData.Phonenumber = lArrStrCells[1];
lObjclsGridLoadExcelData.Title = lArrStrCells[2];
lObjclsGridLoadExcelData.Firstname = lArrStrCells[3];
lObjclsGridLoadExcelData.Lastname = lArrStrCells[4];
lObjclsGridLoadExcelData.Surename = lArrStrCells[5];
lObjclsGridLoadExcelData.Address1 = lArrStrCells[6];
lObjclsGridLoadExcelData.Address2 = lArrStrCells[7];
lObjclsGridLoadExcelData.City = lArrStrCells[8];
lObjclsGridLoadExcelData.State = lArrStrCells[9];
lObjclsGridLoadExcelData.Zip = lArrStrCells[10];
lObjclsGridLoadExcelData.Datetimecallback = DateTime.Now;
lObjclsGridLoadExcelData.Comments = “”;
lObjclsGridLoadExcelData.Agent = “sajjad”;
lObjclsGridLoadExcelData.Attempts = 2;

lArrObjclsGridLoadExcelData.Add(lObjclsGridLoadExcelData);

j++;
}

lObjStreamReader.Close();
lObjStreamReader.Dispose();

}
//  return lArrObjclsGridLoadExcelData;
}
catch (Exception ex)
{
//  lFnShowPopupErr(ex.Message.ToString());
// return null;
}
}

 

 

load csv in datagrid

 

private void btnUpload_Click(object sender, RoutedEventArgs e)
{

try
{
lFnLoadCSVDataInGrid();
       grdList.ItemsSource = lArrObjclsGridLoadExcelData.ToList();
}

catch (Exception)
{
throw;

}

}

Advertisements
 
Leave a comment

Posted by on March 10, 2012 in Silverlight

 

Tags: , , ,

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: