CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Jan 2005
    Posts
    46

    Thumbs up C#NEt2008 Export Data to EXCEL Spreadsheet

    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

  2. #2
    Join Date
    Jul 2008
    Location
    WV
    Posts
    5,362

    Re: C#NEt2008 Export Data to EXCEL Spreadsheet

    I've not tried to do what you are doing here but unless I am mistaken you need to issue a DR.Read before you try to access the values.
    Always use [code][/code] tags when posting code.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured