locked
working with data from .csv file RRS feed

  • Question

  • User-275064894 posted

    i am working on a project and i have the data in a .csv file.

    i want to know how can i retrive data from the .csv file? which control should i use that would have that function?  

    Wednesday, February 17, 2010 2:13 PM

All replies

  • User-1635724035 posted

    http://aspalliance.com/1107_CodeSnip_Reading_CSV_Files_Using_Dataset.2

     

    Hope this helps you 

    Wednesday, February 17, 2010 4:07 PM
  • User-192640548 posted
    protected void btnImport_Click(object sender, EventArgs e)
            {
                EmployeeBL objEmployeeBL = new EmployeeBL();
    
                //Check file exists or not and if it is then set it's different attributes
                FileStream objFileStream;
                if (!System.IO.File.Exists("D:\\Session Detail\\Employee.csv"))
                {
                    objFileStream = null;
                    lblMessage.Text = "Sorry,File Does't Exists.";
                    pnlMessage.Visible = true;
                }
                else
                {
                    objFileStream = new FileStream("D:\\Session Detail\\Employee.csv", FileMode.Open, FileAccess.Read );
                }
    
                //Create object of StreamReader and Store the one row data from CSV file in string array using split operation
                //After that call function which add data in database
                StreamReader sr = new StreamReader(objFileStream);
                string line = string.Empty;
                line = sr.ReadLine();
                try
                {
                    while ((line = sr.ReadLine()) != null)
                    {
                        string[] Tokens = line.Split(',');
                        objEmployeeBL.AddNewEmployeeDetail(Convert.ToInt32(Tokens[0]), Tokens[1], Tokens[2], Convert.ToInt32(Tokens[3]), Convert.ToInt32(Tokens[4]), Tokens[5], Tokens[6]);
                    }
                }
                catch (Exception ex)
                {
                    throw(ex);
                }
                finally
                {
                    sr.Close();
                    lblMessage.Text = "All Data Are Imported Successfully.";
                    pnlMessage.Visible = true;
                }
            }
    
            protected void btnMessage_Click(object sender, EventArgs e)
            {
                pnlMessage.Visible = false;
            }
    
            protected void btnEmport_Click(object sender, EventArgs e)
            {
                EmployeeBL objEmployeeBL = new EmployeeBL();
                DataSet objDataSet = new DataSet();
    
                try
                {
                    //Read and store data from dataset in dataset
                    objDataSet = objEmployeeBL.GetAllEmployeeDetails();
    
                    //Check file exists or not and if it is then set it's different attributes
                    FileStream objFileStream ;
                    if (!System.IO.File.Exists("D:\\Session Detail\\Employee.csv"))
                    {
                        objFileStream = new FileStream("D:\\Session Detail\\Employee.csv", FileMode.CreateNew, FileAccess.Write);
                    }
                    else
                    {
                        objFileStream = new FileStream("D:\\Session Detail\\Employee.csv", FileMode.Append, FileAccess.Write);
                    }
                    
                    ExportDataGridToCSV(objDataSet,objFileStream );
                }
                catch (Exception ex)
                {
                    throw (ex);
                }
                finally
                {
                    objDataSet.Dispose();
                    lblMessage.Text = "All Data Are Exported Successfully.";
                    pnlMessage.Visible = true;
                }
            }
    
            //Create object of StreamWriter and store data in CSV file from dataset
            private void ExportDataGridToCSV(DataSet dSet,FileStream objFileStream)
            {
                StreamWriter sw = new StreamWriter(objFileStream);
    
                int tableColumns = dSet.Tables[0].Columns.Count;
                for (int count = 0; count < tableColumns; count++)
                {
                    sw.Write(dSet.Tables[0].Columns[count]);
                    if (count < tableColumns - 1)
                    {
                        sw.Write(",");
                    }
                }
                sw.Write(sw.NewLine);
    
                foreach (DataRow dr in dSet.Tables[0].Rows)
                {
                    for (int count = 0; count < tableColumns; count++)
                    {
                        if (!Convert.IsDBNull(dr[count]))
                        {
                            sw.Write(dr[count].ToString());
                        }
                        if (count < tableColumns - 1)
                        {
                            sw.Write(",");
                        }
                    }
                    sw.Write(sw.NewLine);
                }
                sw.Close();
            }
        }
    Wednesday, August 24, 2011 8:51 AM