Manoj Maharana

Manoj Maharana

  • NA
  • 362
  • 126.9k

data is not coming in table format on sqldatareader

Jan 25 2017 1:23 AM
 Here is the code:
  1. protected void Write_CSV_From_Recordset2(SqlDataReader oDataReader)  
  2.     {  
  3.         StringBuilder builder = new StringBuilder();  
  4.         List<string> columnNames = new List<string>();  
  5.         List<string> rows = new List<string>();  
  6.   
  7.         for (int i = 0; i < oDataReader.FieldCount; i++)  
  8.         {  
  9.             string tmpColumnName = oDataReader.GetName(i);  
  10.   
  11.             columnNames.Add(tmpColumnName);  
  12.         }  
  13.   
  14.         builder.Append(string.Join(",", columnNames.ToArray())).Append("\n");  
  15.   
  16.         List<string> currentRow = new List<string>();  
  17.   
  18.   
  19.         while (oDataReader.Read())  
  20.         {  
  21.   
  22.             ////base.WriteLog(oDataReader.FieldCount + "fieldcount");  
  23.             for (int i = 0; i < oDataReader.FieldCount; i++)  
  24.             {  
  25.                 object item = oDataReader[i];  
  26.   
  27.   
  28.                 currentRow.Add(item.ToString());  
  29.   
  30.   
  31.   
  32.             }  
  33.   
  34.   
  35.   
  36.   
  37.         }  
  38.   
  39.         //builder.Append(string.Join("\n", rows.ToArray())).Append("\n");    
  40.   
  41.         rows.Add(string.Join(",", currentRow.ToArray()));  
  42.         builder.Append(string.Join(",", rows.ToArray())).Append("\n");  
  43.   
  44.         Response.Clear();  
  45.         Response.ContentType = "text/csv";  
  46.         Response.AddHeader("Content-Disposition""attachment;filename=pretestscore.csv");  
  47.         Response.Write(builder.ToString());  
  48.         Response.End();  
  49.     }  

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:
 

Answers (4)