Here is the code:
- protected void Write_CSV_From_Recordset2(SqlDataReader oDataReader)
- {
- StringBuilder builder = new StringBuilder();
- List<string> columnNames = new List<string>();
- List<string> rows = new List<string>();
- for (int i = 0; i < oDataReader.FieldCount; i++)
- {
- string tmpColumnName = oDataReader.GetName(i);
- columnNames.Add(tmpColumnName);
- }
- builder.Append(string.Join(",", columnNames.ToArray())).Append("\n");
- List<string> currentRow = new List<string>();
- while (oDataReader.Read())
- {
- ////base.WriteLog(oDataReader.FieldCount + "fieldcount");
- for (int i = 0; i < oDataReader.FieldCount; i++)
- {
- object item = oDataReader[i];
- currentRow.Add(item.ToString());
- }
- }
- //builder.Append(string.Join("\n", rows.ToArray())).Append("\n");
- rows.Add(string.Join(",", currentRow.ToArray()));
- builder.Append(string.Join(",", rows.ToArray())).Append("\n");
- Response.Clear();
- Response.ContentType = "text/csv";
- Response.AddHeader("Content-Disposition", "attachment;filename=pretestscore.csv");
- Response.Write(builder.ToString());
- Response.End();
- }
The problem is that :
while output is coming problem that means while (oDataReader.Read()) function the value are coming just like 281063,70,7091,85,TEST,200,test,NULL. how to get actually data from the table? Where is the wrong in my code? any suggestion?
Here is the table structure:

Nitin SontakkeRank #264 · 9.6K reputationPosted Jan 24, 2017, 9:52 PM EST
Nitin SontakkeRank #264 · 9.6K reputationPosted Jan 24, 2017, 9:57 PM EST
Manoj MaharanaRank #4,500 · 340 reputationPosted Jan 24, 2017, 10:46 PM EST
protected void Write_CSV_From_Recordset2(SqlDataReader oDataReader) { string strCSV = string.Empty; for (int i = 0; i { string tmpColumnName = oDataReader.GetName(i); strCSV += tmpColumnName + ','; } strCSV += "\r\n"; while (oDataReader.Read()) { for (int i = 0; i { object item = oDataReader[i]; strCSV += item.ToString().Replace(",", ";") + ','; } strCSV += "\r\n"; } //Download the CSV file. Response.Clear(); Response.Buffer = true; Response.AddHeader("content-disposition", "attachment;filename=pretestscore.csv"); Response.Charset = ""; Response.ContentType = "application/text"; Response.Output.Write(strCSV); Response.Flush(); Response.End(); } This is the solution.Nitin SontakkeRank #264 · 9.6K reputationPosted Jan 24, 2017, 11:13 PM EST