Click here to Skip to main content
15,900,378 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi devs,

i am retriving the data from local .csv file and displaying that into gridview
i am getting date into datetime format please suggest hpw to retrieve data into string format so it will useful for me to implement searching functionality in csv data
thanks


C#
string filename = path;
         FileInfo file = new FileInfo(filename);
         string ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=\"" + file.DirectoryName + "\";Extended                           Properties='text;HDR=Yes;FMT=Delimited(,)';";
         OleDbConnection con = new OleDbConnection(ConnectionString);
         if (txtSearch.Text == string.Empty)
         {

             OleDbCommand cmd = new OleDbCommand(string.Format("SELECT * FROM [" + file.Name + "]"), con);
             cmd.Parameters.AddWithValue("@p1", txtSearch.Text);
             try
             {
                 con.Open();
                 OleDbDataAdapter oda = new OleDbDataAdapter(cmd);
                 DataTable dt = new DataTable("CSVTable");
                 oda.Fill(dt);
                 GridView1.DataSource = dt;
                 GridView1.DataBind();
             }
             catch (Exception)
             {
                 GridView1.DataSource = null;
                 GridView1.DataBind();
             }
Posted
Updated 6-Dec-15 22:22pm
v4

1 solution

What is the scope/purpose of converting it because, you can't do it in the dataadaptar, therefore inside the datatable you can search for the date column and change the datatype.

Like for example


C#
foreach (DataColumn cl in dt.Columns)
               {
                 if (cl.ColumnName.ToUpper().Contains("DATE"))
                        cl.DataType = typeof(DateTime);
                }



Or you can convert the data with data.ToString() / Convert.ToString(data).

Hope i have understood you and would help you solve the problem you are facing.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900