Click here to Skip to main content
15,887,946 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
I am trying to create log file, how come is there an erro in the directory path? the one bolded.

C#
public static void LogFileWrite(string message)
        {
            FileStream fileStream = null;
            StreamWriter streamWriter = null;

            }
Posted
Updated 3-Aug-15 4:34am
v11
Comments
PIEBALDconsult 2-Aug-15 23:54pm    
You need to move the declaration of ds out of the using block.
[no name] 3-Aug-15 6:54am    
Because your path is wrong. That is why your get the error. You either need to escape the \ characters or make you path string a literal using @.

1 solution

C#
// declare your dataset here..
DataSet ds = new DataSet();
using (SqlConnection conn = new SqlConnection("Connection String"))
{

    SqlCommand sqlComm = new SqlCommand("dbo.Test", conn);

    sqlComm.CommandType = CommandType.StoredProcedure;

    SqlDataAdapter da = new SqlDataAdapter(sqlComm);
    //DataSet ds = new DataSet();
    da.Fill(ds);
}
//create csv file
StreamWriter sw = null;
StringBuilder sb = new StringBuilder();

foreach (DataTable dt in ds.Tables)
{
    sw = new StreamWriter(string.Format(@"C:\Users\GP Test Folder\Export" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".csv", dt.TableName));

    for (int i = 0; i < dt.Rows.Count; i++)
    {
        sb.Clear();

        for (int j = 0; j < dt.Columns.Count; j++)
        {
            sb.Append(dt.Rows[i][j]);
            if (j == (dt.Columns.Count - 1)) sb.Append(",");
        }
        sw.WriteLine(sb.ToString());
    }
    sw.Close();
}

Also you haven't include table name in your file name.
C#
string.Format(@"C:\Users\GP Test Folder\Export{0}" + datetime.ToString("dd-MM-yyyy_hh-mm-ss") + ".csv", dt.TableName)
 
Share this answer
 
v2
Comments
Member 11878313 3-Aug-15 2:15am    
Hi there, thanks for the help it works. However I have updated my question and errors. Are you able to help me with it? Thanks alot here:)
DamithSL 3-Aug-15 2:23am    
what you want to save as log message?
put something there as you need. please try to understand the error and try to solve yourself first.
Member 11878313 3-Aug-15 3:05am    
Im actually trying to store all the general errors and info for now before I proceed on to futher enhancements. Yeah trying to work on it at the same time:)

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