nkewney
asked on
Converting code from C# to VB.net
Dear Experts,
I'm trying to translate some code from C# to VB.net but am having some difficulties with a particular line. The code exports a dataset to an excel spreadsheet.
I have pasted the code below, and highlighted the line I'm struggling with, then pasted what I've done so far at the bottom.
ORIGINAL C# CODE
====================
using System.Data;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
namespace ExcelUtil
{
public class WorkbookEngine
{
// you could have other overloads if you want to get creative...
public static string CreateWorkbook(DataSet ds)
{
XmlDataDocument xmlDataDoc = new XmlDataDocument(ds);
XslTransform xt = new XslTransform();
StreamReader reader =new
StreamReader(typeof (WorkbookEngine).Assembly. GetManifes tResourceS tream(type of (WorkbookEngine), "Excel.xsl")); <------------ PROBLEM LINE !
XmlTextReader xRdr = new XmlTextReader(reader);
xt.Load(xRdr, null, null);
StringWriter sw = new StringWriter();
xt.Transform(xmlDataDoc, null, sw, null);
return sw.ToString();
}
}
}
NEW Code (VB) so far
=======================
Imports System.Data
imports System.IO
imports System.Xml
imports System.Xml.Xsl
Public Class ExcelUtil
Public Function CreateWorkbook(ByVal ds As DataSet) As String
Dim xmlDataDoc As New XmlDataDocument(ds)
Dim xt As New XslCompiledTransform()
Dim reader As StreamReader
CType(Type.GetType().Assem bly.GetMan ifestResou rceStream( Type.GetTy pe(Workboo kEngine), "Excel.xsl"), StreamReader)
^ This line is causing the problem !!
Any help would be appreciated
I'm trying to translate some code from C# to VB.net but am having some difficulties with a particular line. The code exports a dataset to an excel spreadsheet.
I have pasted the code below, and highlighted the line I'm struggling with, then pasted what I've done so far at the bottom.
ORIGINAL C# CODE
====================
using System.Data;
using System.IO;
using System.Xml;
using System.Xml.Xsl;
namespace ExcelUtil
{
public class WorkbookEngine
{
// you could have other overloads if you want to get creative...
public static string CreateWorkbook(DataSet ds)
{
XmlDataDocument xmlDataDoc = new XmlDataDocument(ds);
XslTransform xt = new XslTransform();
StreamReader reader =new
StreamReader(typeof (WorkbookEngine).Assembly.
XmlTextReader xRdr = new XmlTextReader(reader);
xt.Load(xRdr, null, null);
StringWriter sw = new StringWriter();
xt.Transform(xmlDataDoc, null, sw, null);
return sw.ToString();
}
}
}
NEW Code (VB) so far
=======================
Imports System.Data
imports System.IO
imports System.Xml
imports System.Xml.Xsl
Public Class ExcelUtil
Public Function CreateWorkbook(ByVal ds As DataSet) As String
Dim xmlDataDoc As New XmlDataDocument(ds)
Dim xt As New XslCompiledTransform()
Dim reader As StreamReader
CType(Type.GetType().Assem
^ This line is causing the problem !!
Any help would be appreciated
ASKER
Hi and thanks for the response
(StreamReader)(Type.GetTyp e().Assemb ly.GetMani festResour ceStream(T ype.GetTyp e(Workbook Engine), "Excel.xsl"))
This line isn't recognised by VB.net
Nick
(StreamReader)(Type.GetTyp
This line isn't recognised by VB.net
Nick
I see 2 things...
(1) There is no WorkbookEngine class in your VB code. You've named the class in VB "ExcelUtil".
(2) Do you really mean to pass the DataSet by value? Seems like it should be ByRef.
(1) There is no WorkbookEngine class in your VB code. You've named the class in VB "ExcelUtil".
(2) Do you really mean to pass the DataSet by value? Seems like it should be ByRef.
sorry I was going the other way vb.net to csharp. I'm not so good going the way you're going. I'm better at going vb to c#.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
what error are you actually getting?
ASKER
Dim reader As StreamReader = New StreamReader(Type.GetType( WorkbookEn gine).Asse mbly.GetMa nifestReso urceStream (Type.GetT ype(Workbo okEngine), "Excel.xsl"))
WorkbookEngine is a type and cannot be used an expression
Nick
WorkbookEngine is a type and cannot be used an expression
Nick
change GetType(WorkbookEngine) to GetType("WorkbookEngine") although you might need GetType("ExcelUtil.Workboo kEngine")
ASKER
Thanks... my last problem is with this line
Value of type 'System.Xml.XmlDataDocumen t' cannot be converted to 'System.Xml.XmlReader'.
Value of type 'System.IO.StringWriter' cannot be converted to 'System.Xml.XmlWriter'.
Value of type 'System.Xml.XmlDataDocumen
Value of type 'System.IO.StringWriter' cannot be converted to 'System.Xml.XmlWriter'.
ASKER
Here's the line
xt.Transform(xmlDataDoc, Nothing, sw, Nothing)
xt.Transform(xmlDataDoc, Nothing, sw, Nothing)
xt.Transform(xmlDataDoc, Nothing, sw, Nothing)
Return sw.ToString()
should probably be
XmlWriter xsw = new XmlWriter(sw);
xt.Transform(xRdr, Nothing, xsw, Nothing)
Return xsw.ToString()
Return sw.ToString()
should probably be
XmlWriter xsw = new XmlWriter(sw);
xt.Transform(xRdr, Nothing, xsw, Nothing)
Return xsw.ToString()
Here is a cheat sheet.
http://www.codeproject.com/dotnet/CheatSheetCastingNET.asp
Check your object references using the IDE and the WinCV tool that can be found in the bin Folder of your Framework.
Quick ref on WinCV (http://www.netomatix.com/ClassViewer.aspx)
http://www.codeproject.com/dotnet/CheatSheetCastingNET.asp
Check your object references using the IDE and the WinCV tool that can be found in the bin Folder of your Framework.
Quick ref on WinCV (http://www.netomatix.com/ClassViewer.aspx)
ASKER
Thanks for all your help so far folks, but I'm receiving a nullreference exception on a line and not sure how to resolve. My code is at the bottom of this post
Dim reader As StreamReader = New StreamReader(Type.GetType( "WorkbookE ngine").As sembly.Get ManifestRe sourceStre am(Type.Ge tType("Wor kbookEngin e"), "Excel.xsl"))
Object reference not set to an instance of an object
Code so far...
Imports System.Data
Imports System.IO
Imports System.Xml
Imports System.Xml.Xsl
Public Class WorkbookEngine
Public Function CreateWorkbook(ByVal ds As DataSet) As String
Dim xmlDataDoc As New XmlDataDocument(ds)
Dim xt As New XslCompiledTransform()
Dim reader As StreamReader = New StreamReader(Type.GetType( "WorkbookE ngine").As sembly.Get ManifestRe sourceStre am(Type.Ge tType("Wor kbookEngin e"), "Excel.xsl"))
Dim xRdr As XmlTextReader = New XmlTextReader(reader)
xt.Load(xRdr, Nothing, Nothing)
Dim sw As StringWriter = New StringWriter()
Dim xsw As New XmlTextWriter(sw)
xt.Transform(xRdr, Nothing, xsw, Nothing)
Return xsw.ToString()
End Function
End Class
being called by the following...
Dim OutletBLL1 As New GLBusinessLogic.OutletsBLL
Dim ds As DataSet = OutletBLL1.dsActivityOutle t(202)
Dim GLUtilities1 As New GLUtilities.WorkbookEngine
Dim XML As String = GLUtilities1.CreateWorkboo k(ds)
Any help would be appreciated
Dim reader As StreamReader = New StreamReader(Type.GetType(
Object reference not set to an instance of an object
Code so far...
Imports System.Data
Imports System.IO
Imports System.Xml
Imports System.Xml.Xsl
Public Class WorkbookEngine
Public Function CreateWorkbook(ByVal ds As DataSet) As String
Dim xmlDataDoc As New XmlDataDocument(ds)
Dim xt As New XslCompiledTransform()
Dim reader As StreamReader = New StreamReader(Type.GetType(
Dim xRdr As XmlTextReader = New XmlTextReader(reader)
xt.Load(xRdr, Nothing, Nothing)
Dim sw As StringWriter = New StringWriter()
Dim xsw As New XmlTextWriter(sw)
xt.Transform(xRdr, Nothing, xsw, Nothing)
Return xsw.ToString()
End Function
End Class
being called by the following...
Dim OutletBLL1 As New GLBusinessLogic.OutletsBLL
Dim ds As DataSet = OutletBLL1.dsActivityOutle
Dim GLUtilities1 As New GLUtilities.WorkbookEngine
Dim XML As String = GLUtilities1.CreateWorkboo
Any help would be appreciated
The Excel file would be XLS extension.
ASKER
The XLS file defines the presentation of the XML document and is used to translate XML documents into other formats
ASKER
Sorry, XSL !
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
shoud be (StreamReader)(Type.GetTyp