473,327 Members | 2,016 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,327 software developers and data experts.

C#NET2008 Retrieve DataReader fieldName

126 100+
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.Cells[intRow, icol] = Convert.ToString(DR["OrderID"].Name);

Here are the overall coding.
Expand|Select|Wrap|Line Numbers
  1. sqlconn = new SqlConnection(connstr);
  2. sqlconn.Open();
  3. sqlcmd = new SqlCommand(strSql, sqlconn);
  4. DR = sqlcmd.ExecuteReader()
  5.  
  6. if (DR.HasRows == false)
  7.  {
  8.    MessageBox.Show("Customer Does not have Invoices");
  9.  } //endif
  10. else
  11.  {
  12.   xlExcel.Visible = true;
  13.   Excel.Range xlRange;
  14.   Excel.Range xlwrkSheet_Range;
  15.  
  16.   //create column header
  17.   intRow = 5;
  18.   int icol;
  19.   int iRecCnt = DR.FieldCount;
  20.  
  21.   for (icol = 1; icol < iRecCnt; icol++)
  22.   {
  23.    switch (icol)
  24.     {
  25.      case 1:
  26.       xlWrkSheet.Cells[intRow, icol] = Convert.ToString (DR["OrderID"].Name);
  27.   xlWrkSheet.Columns.ColumnWidth=10;                             
  28.    break;
  29.  
  30.    case 2:
  31.    xlWrkSheet.Cells[intRow, icol] = Convert.ToString(DR["OrderDate"].Name);
  32.   xlWrkSheet.Columns.ColumnWidth = 12;                              
  33.   break;
  34.  
  35.  case 3:
  36.  xlWrkSheet.Cells[intRow, icol] = Convert.ToString(DR["RequireDate"].name);
  37.  xlWrkSheet.Columns.ColumnWidth = 12;    
  38.  break;
  39.  
  40.  case 4:
  41.  xlWrkSheet.Cells[intRow, icol] = Convert.ToString(DR["ShipDate"].Name);
  42.  xlWrkSheet.Columns.ColumnWidth = 12;
  43.  break;
  44. }
  45. }
  46.  
  47.  
Dec 13 '10 #1
3 2332
Christian Binder
218 Expert 100+
Try calling DR.Read() between ExecuteReader() and accessing its properties DR["..."].

See also: msdn
Dec 13 '10 #2
lenniekuah
126 100+
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.
Dec 13 '10 #3
lenniekuah
126 100+
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.
Expand|Select|Wrap|Line Numbers
  1. // ---- create column header ----
  2.   intRow = 5;
  3.   int icol =0;
  4.   int intFld = 0;
  5.   int iRecCnt = DR.FieldCount;
  6.  
  7.  for (icol = 1; icol <= iRecCnt;icol++)                  
  8.    {
  9.     //create column Header using DataReader Column Name
  10. lWrkSheet.Cells[intRow, icol]= DR.GetName(intFld);                      
  11.    intFld += 1;
  12.    }
  13.  

Wish you all the Best and enjoy yourself.
Love sharing it with you.
Dec 14 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Mike | last post by:
I am trying to use a datareader to retrieve data and set some properties. I am getting a "Specified method is not supported" error on Line xxx. The data base is Oracle 9i and the field is a...
5
by: Jason Huang | last post by:
Hi, Is it possible to bind DataReader to a DataGrid in C# windows form? And how? And can we update data in a DataSet by using the DataReader? Thanks for help. Jason
6
by: Grant | last post by:
I am connecting to an access database using a datareader in C#. I get results when I run a certain query from Access but when I run it from Code it does not retrieve any results. I have put a stop...
5
by: Joel Reinford | last post by:
I am attempting to verify whether a datareader is closed by calling the databind on a control. for example (fake code) Dim rdr as SqlDataReader rdr = mycommand.executeReader... ...
20
by: Mark | last post by:
Hi all, quick question , a DataView is memory resident "view" of data in a data table therefore once populated you can close the connection to the database. Garbage collection can then be used to...
1
by: Shapper | last post by:
Hello, I have a working DataReader on a web page. I used it on a DataGrid. Something like: id name city 1 John London 2 Mary New York How can I display the...
20
by: fniles | last post by:
I am using VB.NET 2003, SQL 2000, and SqlDataReader. As I read data from tblA, I want to populate tblB. I use SQLDataReader for both tables. I do not use thread. When I ExecuteReader on tblB, I...
2
by: lenniekuah | last post by:
Hullo Good Friends, I need your help. Please help me. I am using C#NET2008 to develop Window application I am trying to retrieve Sales Transaction and print it on WORD Document as statement...
9
by: lenniekuah | last post by:
Hullo Good Guys, I need your help. I am very puzzled about using WITH with C#NET2008. The coding below when used in VB.NET I will be using WITH : Wth me.ListBoxCust .ColumnWidth =...
0
by: lenniekuah | last post by:
Hullo Wonderful Friends, I need your help again. Please help me. I am using WORD Template which has a table containing only 1 row and 5 columns. This row is for Column headers Text. description...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.