Stuart1044
Stuart1044

Reputation: 444

StreamWriter not writing last value

I have some code which executes a SQL Stored Procedure and then writes the result to a CSV file, however the last value is not being written to the file.

Code:

conn = new SqlConnection(_sConnectionString);
conn.Open();

SqlCommand cmd = new SqlCommand("usp_PS_Test", conn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("@ExportDate", DateTime.Now));
cmd.Parameters.Add(new SqlParameter("@SiteId", _iSiteId));

using(SqlDataReader rdr = cmd.ExecuteReader())
    using(StreamWriter salesWriter = new StreamWriter(_sSalesFileName))
        while(rdr.Read())
        {
            salesWriter.WriteLine("{0},{1},{2},{3},{4}",rdr["TransactionDate"],rdr["ProductCode"] , rdr["Qty"] ,rdr["Price"] , rdr["NominalCode"]);
        }

The column "NominalCode" is not being written to the file, any suggestions?

I have tried the same code but with a different stored procedure and again the last column is missing.

Data returned from stored procedure:

TransactionDate ProductCode Qty Price   NominalCode
2015-07-30 00:00:00 SERVICE 1   45.00   4000001
2015-07-30 00:00:00 SERVICE 1   15.00   4000001

Also, stupid moment, how can i format the "Price" column to be to currency/money format?

Thank You

Upvotes: 0

Views: 127

Answers (1)

Stuart1044
Stuart1044

Reputation: 444

Turns out (as expected I admit) that there was a different underlying issue in the code, where a parameter for the stored procedure was set. This in turn was not joining correctly to retrieve the require value.

Thank you for the help and advice.

Upvotes: 1

Related Questions