Click to See Complete Forum and Search --> : Help w/conversion from C# to VB.NET


kwilliams
10-13-2006, 02:37 PM
I found a great article titled "Creating Dynamic ASP.NET Server Controls Using XML" at http://www.dnzone.com/showDetail.asp?TypeId=2&NewsId=151&LinkFile=page3%2Ehtm&offset=10.

But all of the code was written in C#. I've tried using the C# to VB.NET conversion tool at http://www.kamalpatel.net/ConvertCSharp2VB.aspx, but I'm still running into errors. This is the latest error:

BC30205: End of statement expected.
Line 19:
Line 20: Public Class WebForm1
Line 21: Inherits System.Web.UI.Page Public class WebForm1 : System.Web.UI.Page
Line 22:
Line 23: Protected Title As System.Web.UI.WebControls.TextBox

And since I don't know C#, and I'm a newbie to VB.NET, I'm not sure how to proceed. If anyone can give me some help and/or some converted code, that would be great. Thanks for any help.

Original C# Code:
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

// add references for XSLT
using System.Xml;
using System.Xml.Xsl;
using System.Xml.XPath;

// add references for Streams
using System.IO;

namespace CustomSurveys
{
/// <summary>
/// This is a custom survey page, where the questions
/// are created at runtime
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox Title;
protected System.Web.UI.WebControls.PlaceHolder survey;
protected System.Web.UI.WebControls.Literal ThankYouLabel;

private void Page_Load(object sender, System.EventArgs e)
{
if (IsPostBack) {
ProcessSurveyResults();
survey.Visible = false;
ThankYouLabel.Visible = true;
} else {
survey.Visible = true;
ThankYouLabel.Visible = false;
}
}

private void CreateSurvey() {
// Load the data source
XPathDocument surveyDoc = new XPathDocument(Server.MapPath("ExSurvey.xml"));

// Load the xslt to do the transformations
XslTransform transform = new XslTransform();
transform.Load(Server.MapPath("MakeControls.xslt"));

// Get the transformed result
StringWriter sw = new StringWriter();
transform.Transform(surveyDoc, null, sw);
string result = sw.ToString();

// remove the namespace attribute
result = result.Replace("xmlns:asp=\"remove\"", "");

// parse the control(s) and add it to the page
Control ctrl = Page.ParseControl(result);
survey.Controls.Add(ctrl);
}

private void ProcessSurveyResults() {
// Load the data source
XPathDocument surveyDoc = new XPathDocument(Server.MapPath("ExSurvey.xml"));
// create an iterator
XPathNodeIterator itr = surveyDoc.CreateNavigator().Select("//question");
// string builder for survey body
System.Text.StringBuilder sb;
sb = new System.Text.StringBuilder();
// submission information
sb.Append("Survey submitted on " + DateTime.Now + Environment.NewLine);
// foreach question
while (itr.MoveNext()) {
// get the control name
string controlName = itr.Current.GetAttribute("name", "");
// append question information
sb.Append(controlName);
sb.Append(" : ");
// get the control
object ctrl = FindControl(controlName);
// append the correct filled out information
if (ctrl is TextBox) {
sb.Append(((TextBox)ctrl).Text);
}
if (ctrl is RadioButtonList) {
// the selected item might be null
if (((RadioButtonList)ctrl).SelectedItem != null) {
sb.Append(((RadioButtonList)ctrl).SelectedItem.Value);
}
}
sb.Append(Environment.NewLine);
}
string body = sb.ToString();

// send the results
System.Web.Mail.SmtpMail.SmtpServer = "your.smtp.server";
System.Web.Mail.SmtpMail.Send("survey@somewhere.com", "your@address.com", "Survey result", body);
}

#region Web Form Designer generated code
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: This call is required by the ASP.NET Web Form Designer.
//
InitializeComponent();
CreateSurvey();
base.OnInit(e);
}

/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.Load += new System.EventHandler(this.Page_Load);

}
#endregion
}
}

[b]Converted VB.NET Code:
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls

' add references for XSLT
Imports System.Xml
Imports System.Xml.Xsl
Imports System.Xml.XPath

' add references for Streams
Imports System.IO

Public Class WebForm1
Inherits System.Web.UI.Page Public class WebForm1 : System.Web.UI.Page

Protected Title As System.Web.UI.WebControls.TextBox
Protected survey As System.Web.UI.WebControls.PlaceHolder
Protected ThankYouLabel As System.Web.UI.WebControls.Literal

'***THIS SECTION BELOW WOULD NOT CONVERT***
private void Page_Load(Object sender, System.EventArgs e)
{
if (IsPostBack) {
ProcessSurveyResults();
survey.Visible = false;
ThankYouLabel.Visible = true;
} else {
survey.Visible = true;
ThankYouLabel.Visible = false;
}
}
'***THIS SECTION ABOVE WOULD NOT CONVERT***

Private Sub CreateSurvey()
' Load the data source
Dim surveyDoc As XPathDocument = New XPathDocument(Server.MapPath("ExSurvey.xml"))

' Load the xslt to do the transformations
Dim transform As XslTransform = New XslTransform()
transform.Load(Server.MapPath("MakeControls.xslt"))

' Get the transformed result
Dim sw As StringWriter = New StringWriter()
transform.Transform(surveyDoc, Nothing, sw)
Dim result As String = sw.ToString()

' remove the namespace attribute
result = result.Replace("xmlns:asp=\"remove\"", "")

' parse the control(s) and add it to the page
Dim ctrl As Control = Page.ParseControl(result)
survey.Controls.Add(ctrl)
End Sub

Private Sub ProcessSurveyResults()
' Load the data source
Dim surveyDoc As XPathDocument = New XPathDocument(Server.MapPath("ExSurvey.xml"))
' create an iterator
Dim itr As XPathNodeIterator = surveyDoc.CreateNavigator().Select("//question")
' string builder for survey body
Dim sb As System.Text.StringBuilder
sb = New System.Text.StringBuilder()
' submission information
sb.Append("Survey submitted on " + DateTime.Now + Environment.NewLine)
' foreach question
While itr.MoveNext()
' get the control name
Dim controlName As String = itr.Current.GetAttribute("name","")
' append question information
sb.Append(controlName)
sb.Append(" : ")
' get the control
Dim ctrl As Object = FindControl(controlName)
' append the correct filled out information
If TypeOf ctrl Is TextBox Then
sb.Append((CType(ctrl, TextBox)).Text)
End If
If TypeOf ctrl Is RadioButtonList Then
' the selected item might be null
If Not(CType(ctrl,RadioButtonList)).SelectedItem Is Nothing Then
sb.Append((CType(ctrl, RadioButtonList)).SelectedItem.Value)
End If
End If
sb.Append(Environment.NewLine)
End While
Dim body As String = sb.ToString()

' send the results
System.Web.Mail.SmtpMail.SmtpServer = "your.smtp.server"
System.Web.Mail.SmtpMail.Send("survey@somewhere.com", "your@address.com", "Survey result", body)
End Sub

#Region 'Web Form Designer generated code
Overrides Protected Sub OnInit"(ByVal e As EventArgs)
'
' CODEGEN: This call is required by the ASP.NET Web Form Designer.
'
InitializeComponent()
CreateSurvey()
MyBase.OnInit(e)
End Sub

Private Sub InitializeComponent()
Me.Load += New System.EventHandler(Me.Page_Load)

End Sub
#End Region
End Class

sirpelidor
10-13-2006, 07:42 PM
the author wrote the code in .net 1.0, normal translator you found from google only translate 1.x.

I had made few adjustments to make sure it compiles; see if it works.

p.s: i haven't really study the code and how it works, but you do aware of none of the those methods are being call from page_load right?

John_Nick
10-17-2006, 08:31 AM
Please paste your C# & Vb.net code for converting it to
vb.net & c# code respectively.

http://www.developerfusion.co.uk/utilities/convertcsharptovb.aspx