code:
string path=@"c:\file";
try{
command.CommandText = "dbo.SpSample";
command.CommandType = CommandType.StoredProcedure;
dataAdapter = new SqlDataAdapter(command.CommandText, connect);
dataAdapter.Fill(dataSet, "dbo.SpSample");
DataTable t2 = dataSet.Tables["dbo.SpSample"];
string csv2 = string.Empty;
foreach (DataColumn column in t2.Columns)
{
csv2 += column.ColumnName + ',';
}
csv2 += "\r\n";
foreach (DataRow row in t2.Rows)
{
foreach (DataColumn column in t2.Columns)
{
csv2 += row[column.ColumnName].ToString().Replace(",", ";") + ',';
}
csv2 += "\r\n";
}
File.WriteAllText(exportPath + DateTime.Now + "Sample1.csv", csv2);
}
catch (Exception e){}
code is working fine but if i add timestamp in file.writealltext method getting error
timeformat(11/1/2023 12:00) should be like this
Vishal JoshiPosted Mar 14, 2023, 5:09 AM
Hello Meghna
Issue due to spacial charater in filename colon (:)
As per your code it will generate filename like below
C:\file\14-03-2023 10:34:36 AMSample1.csv
check the below screen print shows the list of spacial not allowed in a file name.
Please use below code to to get it work without spacial charater.
It will generate file path like below:
C:\file\14-03-2023 10-37-Sample1.csv
Thanks
Vishal
Naimish MakwanaPosted Mar 14, 2023, 3:43 AM
Hello Meghna,
The issue with adding the timestamp in the
File.WriteAllText()method is that the timestamp may contain characters that are not valid for a filename, such as the colon (:) character used to separate the hours, minutes, and seconds in a time value.To format the timestamp as "MM-dd-yyyy hhmm" (e.g. "11-01-2023 1200"), you can use the following code:
Thanks
Naimish Makwana