C#NET2008 Retrieve DataReader fieldName

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • lenniekuah
    New Member
    • Oct 2006
    • 126

    C#NET2008 Retrieve DataReader fieldName

    Hullo Awesome Friends,
    I am using C#NET2008 and Microsoft Excel 2003.

    I encounter an interesting problem. I am trying to retrieve the Field Name from DataReader to insert onto Excel Spreadsheet Row onto each column as Header.

    Also having problem trying to aligned the Column as Center.So that all text are Center alignment.

    Sample coding that is not working;
    xlWrkSheet.Cell s[intRow, icol] = Convert.ToStrin g(DR["OrderID"].Name);

    Here are the overall coding.
    Code:
    sqlconn = new SqlConnection(connstr);
    sqlconn.Open();
    sqlcmd = new SqlCommand(strSql, sqlconn);
    DR = sqlcmd.ExecuteReader()
    
    if (DR.HasRows == false)
     {
       MessageBox.Show("Customer Does not have Invoices");
     } //endif
    else
     {
      xlExcel.Visible = true;
      Excel.Range xlRange;
      Excel.Range xlwrkSheet_Range;
    
      //create column header
      intRow = 5;
      int icol;
      int iRecCnt = DR.FieldCount;
    
      for (icol = 1; icol < iRecCnt; icol++)
      {
       switch (icol)
        {
         case 1:
          xlWrkSheet.Cells[intRow, icol] = Convert.ToString (DR["OrderID"].Name);
      xlWrkSheet.Columns.ColumnWidth=10;                             
       break;
      
       case 2:
       xlWrkSheet.Cells[intRow, icol] = Convert.ToString(DR["OrderDate"].Name);
      xlWrkSheet.Columns.ColumnWidth = 12;                              
      break;
    
     case 3:
     xlWrkSheet.Cells[intRow, icol] = Convert.ToString(DR["RequireDate"].name);
     xlWrkSheet.Columns.ColumnWidth = 12;    
     break;
              
     case 4:
     xlWrkSheet.Cells[intRow, icol] = Convert.ToString(DR["ShipDate"].Name);
     xlWrkSheet.Columns.ColumnWidth = 12;
     break;
    }
    }
  • Christian Binder
    Recognized Expert New Member
    • Jan 2008
    • 218

    #2
    Try calling DR.Read() between ExecuteReader() and accessing its properties DR["..."].

    See also: msdn

    Comment

    • lenniekuah
      New Member
      • Oct 2006
      • 126

      #3
      Hullo Christian Binder,
      Regarding your statement I do not understand it. Can you please provide sample coding of it.

      Your sample DR["..."] does this retrieve the column name of the data value ? All I know is that this DR[".."] retrieve the data value not the Table Column Name.

      Comment

      • lenniekuah
        New Member
        • Oct 2006
        • 126

        #4
        Hullo Helpful Friends,Finally I managed to retrieve the DataReader Column Name for display on the Excel Spreadsheet Columns as Header.

        I would like to share the working Coding here with Newbies who may have similar problems: The coding are in C#NET2008 and Excel Version is Microsoft Office 2003.

        Here are the coding.
        Code:
        // ---- create column header ----
          intRow = 5;
          int icol =0;
          int intFld = 0;
          int iRecCnt = DR.FieldCount;
                         
         for (icol = 1; icol <= iRecCnt;icol++)                  
           {
            //create column Header using DataReader Column Name
        lWrkSheet.Cells[intRow, icol]= DR.GetName(intFld);                      
           intFld += 1;
           }

        Wish you all the Best and enjoy yourself.
        Love sharing it with you.

        Comment

        Working...