Link to home
Start Free TrialLog in
Avatar of nkewney
nkewneyFlag for United Kingdom of Great Britain and Northern Ireland

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.GetManifestResourceStream(typeof (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().Assembly.GetManifestResourceStream(Type.GetType(WorkbookEngine), "Excel.xsl"), StreamReader)
^ This line is causing the problem !!

Any help would be appreciated
Avatar of gjutras
gjutras

CType(Type.GetType().Assembly.GetManifestResourceStream(Type.GetType(WorkbookEngine), "Excel.xsl"), StreamReader)
shoud be (StreamReader)(Type.GetType().Assembly.GetManifestResourceStream(Type.GetType(WorkbookEngine), "Excel.xsl"))
Avatar of nkewney

ASKER

Hi and thanks for the response

(StreamReader)(Type.GetType().Assembly.GetManifestResourceStream(Type.GetType(WorkbookEngine), "Excel.xsl"))

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.
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
Avatar of gjutras
gjutras

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
what error are you actually getting?
Avatar of nkewney

ASKER

           Dim reader As StreamReader = New StreamReader(Type.GetType(WorkbookEngine).Assembly.GetManifestResourceStream(Type.GetType(WorkbookEngine), "Excel.xsl"))

WorkbookEngine is a type and cannot be used an expression

Nick
change GetType(WorkbookEngine) to GetType("WorkbookEngine") although you might need GetType("ExcelUtil.WorkbookEngine")
Avatar of nkewney

ASKER

Thanks... my last problem is with this line

Value of type 'System.Xml.XmlDataDocument' cannot be converted to 'System.Xml.XmlReader'.
Value of type 'System.IO.StringWriter' cannot be converted to 'System.Xml.XmlWriter'.
Avatar of nkewney

ASKER

Here's the line

 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()
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)
Avatar of nkewney

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("WorkbookEngine").Assembly.GetManifestResourceStream(Type.GetType("WorkbookEngine"), "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("WorkbookEngine").Assembly.GetManifestResourceStream(Type.GetType("WorkbookEngine"), "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.dsActivityOutlet(202)

        Dim GLUtilities1 As New GLUtilities.WorkbookEngine
        Dim XML As String = GLUtilities1.CreateWorkbook(ds)

Any help would be appreciated
The Excel file would be XLS extension.
Avatar of nkewney

ASKER

The XLS file defines the presentation of the XML document and is used to translate XML documents into other formats
Avatar of nkewney

ASKER

Sorry, XSL !
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial