wreckingcru
New member
- Joined
- Aug 16, 2006
- Messages
- 1
- Programming Experience
- Beginner
I'm trying to output a SQL query that is constructed thru my VB.net GUI
into an excel file.
Here is the code I'm using:
'Sqlstmt is the SQL query statement
'Conn is the SQL Connection object
cmd = New SqlCommand(Sqlstmt, Conn)
datareader = cmd.ExecuteReader()
'datareader is the SQLdatareader object
Dim objexcel As Excel.Application
Dim objwkb As Workbook
Dim objwksht As Worksheet
objexcel = New Excel.Application
objwkb = objexcel.Workbooks.Add
objwksht = objwkb.ActiveSheet()
Dim cols = datareader.FieldCount()
objwksht.Range(objwksht.Cells(1, 1), objwksht.Cells(5,
cols)).CopyFromRecordset(datareader)
The line above is causing an exception, i.e., I never receive any data
in the excel sheet. I put this in a try statement to catch the
exception and it is this:
System.InvalidCastException: No such interface supported
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData&
msgData)
at Excel.Range.CopyFromRecordset(Object Data, Object MaxRows, Object MaxColumns)
at WindowsApplication1.Form1.btGenReport_Click(Object sender,
EventArgs e) in C:.....\Form1.vb:line 416
I'm a complete newbie at .NET (Unix C/C++ programming background).
Can anyone advise about what I'm doing wrong? I've sort of "learnt" the
code from others, so the Range command is kinda sketchy to me.....
What other alternative ways would there be to take the "datareader"
data and put it in an excel file (I'm trying to create a report based
on the data from a SQL table)?
Thanks!
into an excel file.
Here is the code I'm using:
'Sqlstmt is the SQL query statement
'Conn is the SQL Connection object
cmd = New SqlCommand(Sqlstmt, Conn)
datareader = cmd.ExecuteReader()
'datareader is the SQLdatareader object
Dim objexcel As Excel.Application
Dim objwkb As Workbook
Dim objwksht As Worksheet
objexcel = New Excel.Application
objwkb = objexcel.Workbooks.Add
objwksht = objwkb.ActiveSheet()
Dim cols = datareader.FieldCount()
objwksht.Range(objwksht.Cells(1, 1), objwksht.Cells(5,
cols)).CopyFromRecordset(datareader)
The line above is causing an exception, i.e., I never receive any data
in the excel sheet. I put this in a try statement to catch the
exception and it is this:
System.InvalidCastException: No such interface supported
at System.RuntimeType.ForwardCallToInvokeMember(String memberName, BindingFlags flags, Object target, Int32[] aWrapperTypes, MessageData&
msgData)
at Excel.Range.CopyFromRecordset(Object Data, Object MaxRows, Object MaxColumns)
at WindowsApplication1.Form1.btGenReport_Click(Object sender,
EventArgs e) in C:.....\Form1.vb:line 416
I'm a complete newbie at .NET (Unix C/C++ programming background).
Can anyone advise about what I'm doing wrong? I've sort of "learnt" the
code from others, so the Range command is kinda sketchy to me.....
What other alternative ways would there be to take the "datareader"
data and put it in an excel file (I'm trying to create a report based
on the data from a SQL table)?
Thanks!