Wrox Programmer Forums
Go Back   Wrox Programmer Forums > ASP.NET and ASP > ASP.NET 2.0 > ASP.NET 2.0 Basics
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 14th, 2009, 01:15 PM
bex bex is offline
Friend of Wrox
 
Join Date: Aug 2008
Posts: 154
Thanks: 7
Thanked 1 Time in 1 Post
Default How to translate C# code to VB

hi thete i need to to be able to download a gridview in vb i did found ther write code how to do that but it is in C# and i dont know any c# :
can any one show me the vb version

the code is here
Code:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using iTextSharp.text;
using iTextSharp.text.pdf;
using iTextSharp.text.html;
using System.IO;
using System.Collections;
using System.Net;
publicpartialclass_Default : System.Web.UI.Page
{
protectedvoid Page_Load(object sender, EventArgs e)
{
}
protectedvoid btnExport_Click(object sender, EventArgs e)
{
HtmlForm form = newHtmlForm();
form.Controls.Add(GridView1);
StringWriter sw = newStringWriter();
HtmlTextWriter hTextWriter = newHtmlTextWriter(sw);
form.Controls[0].RenderControl(hTextWriter);
string html = sw.ToString();
Document Doc = new Document();
 
//PdfWriter.GetInstance
//(Doc, new FileStream(Request.PhysicalApplicationPath 
//+ "\\AmitJain.pdf", FileMode.Create));
PdfWriter.GetInstance(Doc, newFileStream(Environment.GetFolderPath
(Environment.SpecialFolder.Desktop)+ "\\AmitJain.pdf", FileMode.Create));
Doc.Open();
 
Chunk c = new Chunk("Export GridView to PDF Using iTextSharp \n",FontFactory.GetFont("Verdana", 15));
Paragraph p = new Paragraph();
p.Alignment = Element.ALIGN_CENTER;
p.Add(c);
Chunk chunk1 = new Chunk("By Amit Jain, [email protected] \n",FontFactory.GetFont("Verdana", 8));
Paragraph p1 = new Paragraph();
p1.Alignment = Element.ALIGN_RIGHT;
p1.Add(chunk1);
 
Doc.Add(p);
Doc.Add(p1);
 
System.Xml.XmlTextReader xmlReader = 
new System.Xml.XmlTextReader(newStringReader(html));
HtmlParser.Parse(Doc, xmlReader);
 
Doc.Close();
string Path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop)+ "\\AmitJain.pdf";
 
 
 
ShowPdf(Path);
 
 
}
privatevoid ShowPdf(string strS)
{
Response.ClearContent();
Response.ClearHeaders();
Response.ContentType = "application/pdf";
Response.AddHeader("Content-Disposition","attachment; filename=" + strS);
Response.TransmitFile(strS);
Response.End();
//Response.WriteFile(strS);
Response.Flush();
Response.Clear();
 
}
 
}
thanks
__________________
bx
 
Old February 14th, 2009, 03:19 PM
Lee Dumond's Avatar
Wrox Author
 
Join Date: Jan 2008
Posts: 923
Thanks: 12
Thanked 166 Times in 162 Posts
Default

Have you tried

http://converter.telerik.com/
__________________
Visit my blog at http://leedumond.com
Follow me on Twitter: http://twitter.com/LeeDumond

Code:
if (this.PostHelpedYou)
{
   ClickThanksButton(); 
}
 
Old February 14th, 2009, 03:25 PM
Friend of Wrox
 
Join Date: Jun 2008
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts
Default

As a general rule, you can transform C# to VB pretty easily.

The biggest differences:
(1) C# uses the form typename variablename to declare variables. VB uses DIM variablename AS typename.

VB also allows a more compact form when creating a NEW instance of a class, as we'll see below.

(2) C# uses a semicolon on the end of every statement and a statement can continue across as many lines as needed.

VB does not use a semicolon and if a statement must continue across multiple lines you must use an underline at the end of each line to signify continuation.

(3) C# uses \n to indicate an embedded new line character and other backslash-letter combinations to indicate other special characters. VB does none of that. So you must append a newline separately to a string and you don't need to use \\ to indicate a single \ in the string.


****

There are other differences, of course, but mostly these are pretty simple (and wordier!) transformations.

So here is a *SAMPLE* of a translation of PART of that code:
Code:
Protected Sub btnExport_Click(sender As Object, e As EventArgs)
    Dim form As new HtmlForm() ' notice how we only mention the type name once! NICE!
    form.Controls.Add(GridView1)
    Dim sw As new StringWriter()
    Dim hTextWriter As new HtmlTextWriter(sw)
    form.Controls[0].RenderControl(hTextWriter)
    Dim html As String = sw.ToString( )
    Dim Doc As new Document()
 
    PdfWriter.GetInstance( Doc, _
                           newFileStream( Environment.GetFolderPath(Environment.SpecialFolder.Desktop) _
                                 + "\AmitJain.pdf", _
                                          FileMode.Create) _
                         )
    Doc.Open()
    Dim c As new Chunk( "Export GridView to PDF Using iTextSharp " & vbNewLine, _
                        FontFactory.GetFont("Verdana", 15) _
                      )
    Dim p As new Paragraph()
    p.Alignment = Element.ALIGN_CENTER
    p.Add(c)
    
    Dim chunk1 = As new Chunk( "By Amit Jain, [email protected] " & vbNewLine, _
                               FontFactory.GetFont("Verdana", 8) _
                             )
    Dim p1 As new Paragraph()
    p1.Alignment = Element.ALIGN_RIGHT
    p1.Add(chunk1)
 
    Doc.Add(p)
    Doc.Add(p1)
 
    Dim xmlReader As new System.Xml.XmlTextReader(newStringReader(html))
    HtmlParser.Parse(Doc, xmlReader)
 
    Doc.Close()

    ... and so on ...

Last edited by Old Pedant; February 14th, 2009 at 03:28 PM..





Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I write this vb 6 code to work in vb 2008? sanderson Visual Basic 2008 Essentials 3 June 10th, 2008 01:46 PM
Urgent:hard disk serial code and vb code ivanlaw Pro VB 6 0 July 25th, 2007 04:05 AM
VB: .Exe file, serial code and activation code ivanlaw Pro VB 6 8 July 6th, 2007 05:44 AM
How can i translate this VB code into C#? Dreamfly ASP.NET 2.0 Basics 2 April 13th, 2007 12:53 AM
how to translate a code in DAO to ADO itsmenow Access VBA 2 July 7th, 2005 09:19 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.