• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 4661
  • Last Modified:

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
0
nkewney
Asked:
nkewney
  • 7
  • 7
  • 2
  • +1
2 Solutions
 
gjutrasCommented:
CType(Type.GetType().Assembly.GetManifestResourceStream(Type.GetType(WorkbookEngine), "Excel.xsl"), StreamReader)
shoud be (StreamReader)(Type.GetType().Assembly.GetManifestResourceStream(Type.GetType(WorkbookEngine), "Excel.xsl"))
0
 
nkewneyAuthor Commented:
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
0
 
Bill-HansonCommented:
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.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
gjutrasCommented:
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#.
0
 
gjutrasCommented:
A fairly direct conversion is:
Imports System.Data
Imports System.IO
Imports System.Xml
Imports System.Xml.Xsl
 
Namespace ExcelUtil
 Public Class WorkbookEngine
 ' you could have other overloads if you want to get creative...
 Public Shared Function CreateWorkbook(ByVal ds As DataSet) As String
  Dim xmlDataDoc As XmlDataDocument =  New XmlDataDocument(ds)
   Dim xt As XslTransform =  New XslTransform()
   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()
   xt.Transform(xmlDataDoc, Nothing, sw, Nothing)
   Return sw.ToString()
 End Function
 End Class
End Namespace
0
 
gjutrasCommented:
what error are you actually getting?
0
 
nkewneyAuthor Commented:
           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
0
 
gjutrasCommented:
change GetType(WorkbookEngine) to GetType("WorkbookEngine") although you might need GetType("ExcelUtil.WorkbookEngine")
0
 
nkewneyAuthor Commented:
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'.
0
 
nkewneyAuthor Commented:
Here's the line

 xt.Transform(xmlDataDoc, Nothing, sw, Nothing)
0
 
gjutrasCommented:
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()
0
 
Tommie Nathaniel Carter, Jr., MBACommented:
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)
0
 
nkewneyAuthor Commented:
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
0
 
Tommie Nathaniel Carter, Jr., MBACommented:
The Excel file would be XLS extension.
0
 
nkewneyAuthor Commented:
The XLS file defines the presentation of the XML document and is used to translate XML documents into other formats
0
 
nkewneyAuthor Commented:
Sorry, XSL !
0
 
gjutrasCommented:
does that file exist in that path?  I'd switch from using just filename to path and filename.
0

Featured Post

Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

  • 7
  • 7
  • 2
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now