-
September 24th, 2011, 03:44 AM
#1
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
-
September 24th, 2011, 06:36 AM
#2
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|