Lennie Kuah

Lennie Kuah

  • NA
  • 27
  • 0

C#NET2008 Export Data To Excel Spreadsheet

Sep 24 2011 4:33 AM
 

Hullo Friends,

I do need your Help. Please help me.

I am using C#NET2008 for Window application to export data to Microsoft Excel 2003 Spreadsheet.

I am very surprised that the (DataReader)  DR.HasRow display TRUE means that it has record 

but the system display error message

 Error Message

Invalid attempt to read when no data is present. 

Here are the over coding:

 private void FRetrieveCustomerDetails()

        { //retrieve Customer details for Excel Banner Header --

            string strsql = string.Empty;

                   strsql += " Select CompanyName, Address, City, Country, PostalCode ";

                   strsql += " From TestCustomers ";

                  strsql += " Where (CustomerID = N'" + strCustID + "')";

             sqlconn = new SqlConnection(connstr);

            sqlconn.Open();

            sqlcmd = new SqlCommand(strsql, sqlconn);

            DR = sqlcmd.ExecuteReader(CommandBehavior.CloseConnection);

             if (DR.HasRows == true)               

            {

                strCustName = Convert.ToString(DR.GetValue(1));  <-- error

                strCustAdd = Convert.ToString(DR.GetValue(2));    <-- error

                strCustCity = Convert.ToString(DR.GetValue(3));    <-- error

                strCustCity += "  PostalCode : " + Convert.ToString(DR.GetValue(4)); <-- error

                strCustCountry = Convert.ToString(DR.GetValue(5)); <-- error

            }

            sqlconn.Close();

            sqlcmd.Dispose();

            DR.Dispose();

        }


Thank you for helping me.

Cheers,

Lennie Kuah


Answers (4)