Jump to content

Problem Converting C# to VB.NET in ASP.NET


kwilliams

Recommended Posts

I'm still a newbie to this whole process, so please bear with me. I'm including all of the referenced code at the bottom of this post.I'm trying to work with example code (Listing 7.10) from an online version of the book "XML for ASP.NET - Chapter 7: XSLT and ASP.NET", which can be seen at http://www.topxml.com/dotnet/articles/xslt/default.asp.But when I converted the C# code to VB.NET using the following converter site: http://www.developerfusion.co.uk/utilities...csharptovb.aspx, and I ran the ASP.NET page from within Visual Web Developer 2k5 Express, I received this error message:Compiler Error Message: BC30260: 'btnSubmit' is already declared as 'Protected Dim WithEvents btnSubmit As System.Web.UI.WebControls.Button' in this class.Source Error:Line 17: Public Class XsltGolferLine 18: Inherits System.Web.UI.PageLine 19: Protected btnSubmit As System.Web.UI.WebControls.ButtonLine 20: Protected pnlSelectGolfer As System.Web.UI.WebControls.PanelLine 21: Protected pnlTransformation As System.Web.UI.WebControls.PanelI've tried to research the error message, but I can't find a solution. So if anyone can let me know what I'm doing wrong, that would be great. Thanks for any help.ASP.NET code - xsltGolfer.aspx

<%@ Page Language="VB" CodeFile="xsltGolfer.aspx.vb" Inherits="TestBed.Chapter7.XsltGolfer" Debug="True" %><HTML>	<HEAD>		<style type="text/css">			.blackText {font-family:arial;color:#000000;} .largeYellowText 			{font-family:arial;font-size:18pt;color:#ffff00;} .largeBlackText 			{font-family:arial;font-size:14pt;color:#000000;} .borders {border-left:1px 			solid #000000;border-right:1px solid #000000; border-top:1px solid 			#000000;border-bottom:1px solid #000000;}		</style>	</HEAD>	<body>		<form method="post" runat="server">			<asp:Panel ID="pnlSelectGolfer" Runat="server">				<TABLE width=400 bgColor=#efefef border=0 class="borders">					<TR>						<TD bgColor=#02027a>							<SPAN style="FONT-SIZE: 14pt; COLOR: #ffffff; FONT-FAMILY: arial">								Select a Golfer by Name:							</SPAN>						</TD>					</TR>					<TR>						<TD>							<asp:DropDownList id=ddGolferName runat="server">							</asp:DropDownList>						</TD>					</TR>					<TR>						<TD>							 						</TD>					</TR>					<TR>						<TD>							<asp:Button id=btnSubmit runat="server" Text="Get Golfer!">							</asp:Button>						</TD>					</TR>				</TABLE>			</asp:Panel>			<asp:Panel ID="pnlTransformation" Runat="server" Visible="False">				<DIV id=divTransformation runat="server">				</DIV>				<P>					<asp:LinkButton id=lnkBack Runat="server" Text="Back"> Back</asp:LinkButton></P>			</asp:Panel>		</form>	</body></HTML>

C# version - xsltGolfer.aspx.cs

namespace TestBed.Chapter7{	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;	using System.Xml;	using System.Xml.XPath;	using System.Xml.Xsl;	using System.IO;	using System.Text;	/// <summary>	///		Summary description for test.	/// </summary>	public class XsltGolfer : System.Web.UI.Page		{		protected System.Web.UI.WebControls.Button btnSubmit;		protected System.Web.UI.WebControls.Panel pnlSelectGolfer;		protected System.Web.UI.WebControls.Panel pnlTransformation;		protected System.Web.UI.WebControls.DropDownList ddGolferName;		protected System.Web.UI.WebControls.LinkButton lnkBack;		protected System.Web.UI.HtmlControls.HtmlGenericControl divTransformation;		private string xmlPath;			public XsltGolfer() 		{			Page.Init += new System.EventHandler(Page_Init);		}		protected void Page_Init(object sender, EventArgs e) 		{			xmlPath = Server.MapPath("listing7.1.xml");			InitializeComponent();		}		#region Web Form Designer generated code		/// <summary>		///	Required method for Designer support - do not modify		///	the contents of this method with the code editor.		/// </summary>		private void InitializeComponent()		{				this.lnkBack.Click += new System.EventHandler(this.lnkBack_Click);			this.btnSubmit.Click += new System.EventHandler(this.btnSubmit_Click);			this.Load += new System.EventHandler(this.Page_Load);		}		#endregion		protected void btnSubmit_Click(object sender, System.EventArgs e) {						string xslPath = Server.MapPath("xsltGolfer.xsl"); 			XmlTextReader xmlReader = null;			StringBuilder sb = new StringBuilder();			StringWriter sw = new StringWriter(sb);						try {				xmlReader = new XmlTextReader(xmlPath);							//Instantiate the XPathDocument Class				XPathDocument doc = new XPathDocument(xmlReader);							//Instantiate the XslTransform Classes				XslTransform transform = new XslTransform();				transform.Load(xslPath);				//Add Parameters				XsltArgumentList args = new XsltArgumentList();				args.AddParam("golferName","",this.ddGolferName.SelectedItem.Value);				//Call Transform() method				transform.Transform(doc, args, sw);				//Hide Panels				this.pnlSelectGolfer.Visible = false;				this.pnlTransformation.Visible = true;				this.divTransformation.InnerHtml = sb.ToString();			}			catch (Exception excp) {				Response.Write(excp.ToString());			}			finally {				xmlReader.Close();				sw.Close();			}		}private void Page_Load(object sender, System.EventArgs e) {	if (!Page.IsPostBack) {		FillDropDown("firstName");	}}		protected void lnkBack_Click(object sender, System.EventArgs e)		{			this.pnlSelectGolfer.Visible = true;			this.pnlTransformation.Visible = false;			FillDropDown("firstName");		}private void FillDropDown(string element) {	string name = "";	this.ddGolferName.Items.Clear();	System.Xml.XmlTextReader reader = new XmlTextReader(xmlPath);	object firstNameObj = reader.NameTable.Add("firstName");	while (reader.Read()) 	{		if (reader.Name.Equals(firstNameObj)) 		{			name = reader.ReadString();			ListItem item = new ListItem(name,name);			this.ddGolferName.Items.Add(item);		}	}	reader.Close();}	}}

VB.NET verison - xsltGolfer.aspx.vb

Imports System.TextImports System.IOImports System.Xml.XslImports System.Xml.XPathImports System.XmlImports System.Web.UI.HtmlControlsImports System.Web.UI.WebControlsImports System.Web.UIImports System.Web.SessionStateImports System.WebImports System.DrawingImports System.DataImports System.ComponentModelImports System.CollectionsImports SystemNamespace TestBed.Chapter7	Public Class XsltGolfer		Inherits System.Web.UI.Page		Protected btnSubmit As System.Web.UI.WebControls.Button		Protected pnlSelectGolfer As System.Web.UI.WebControls.Panel		Protected pnlTransformation As System.Web.UI.WebControls.Panel		Protected ddGolferName As System.Web.UI.WebControls.DropDownList		Protected lnkBack As System.Web.UI.WebControls.LinkButton		Protected divTransformation As System.Web.UI.HtmlControls.HtmlGenericControl		Private xmlPath As String		Public Sub New()			AddHandler Page.Init, AddressOf Page_Init		End Sub		Protected Sub Page_Init(ByVal sender As Object, ByVal e As EventArgs)			xmlPath = Server.MapPath("listing7.1.xml")			InitializeComponent()		End Sub		Private Sub InitializeComponent()			AddHandler Me.lnkBack.Click, AddressOf Me.lnkBack_Click			AddHandler Me.btnSubmit.Click, AddressOf Me.btnSubmit_Click			AddHandler Me.Load, AddressOf Me.Page_Load		End Sub		Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs)			Dim xslPath As String = Server.MapPath("xsltGolfer.xsl")			Dim xmlReader As XmlTextReader = Nothing			Dim sb As StringBuilder = New StringBuilder			Dim sw As StringWriter = New StringWriter(sb)			Try				xmlReader = New XmlTextReader(xmlPath)				Dim doc As XPathDocument = New XPathDocument(xmlReader)				Dim transform As XslTransform = New XslTransform				transform.Load(xslPath)				Dim args As XsltArgumentList = New XsltArgumentList				args.AddParam("golferName", "", Me.ddGolferName.SelectedItem.Value)				transform.Transform(doc, args, sw)				Me.pnlSelectGolfer.Visible = False				Me.pnlTransformation.Visible = True				Me.divTransformation.InnerHtml = sb.ToString			Catch excp As Exception				Response.Write(excp.ToString)			Finally				xmlReader.Close()				sw.Close()			End Try		End Sub		Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)			If Not Page.IsPostBack Then				FillDropDown("firstName")			End If		End Sub		Protected Sub lnkBack_Click(ByVal sender As Object, ByVal e As System.EventArgs)			Me.pnlSelectGolfer.Visible = True			Me.pnlTransformation.Visible = False			FillDropDown("firstName")		End Sub		Private Sub FillDropDown(ByVal element As String)			Dim name As String = ""			Me.ddGolferName.Items.Clear()			Dim reader As System.Xml.XmlTextReader = New XmlTextReader(xmlPath)			Dim firstNameObj As Object = reader.NameTable.Add("firstName")			While reader.Read				If reader.Name.Equals(firstNameObj) Then					name = reader.ReadString					Dim item As ListItem = New ListItem(name, name)					Me.ddGolferName.Items.Add(item)				End If			End While			reader.Close()		End Sub	End ClassEnd Namespace

Link to comment
Share on other sites

When I was first learning .NET, I would always declare the controls that were being used on my pages in the code behinds - just like your C# example shows. If my page were:

<%@ Page Language="C#" CodeFile="xsltGolfer.aspx.cs" Inherits="TestBed.Chapter7.XsltGolfer" Debug="True" %><html><body><asp:Button id="btnSubmit" Text="Get Golfer!" runat="server /></body></html>

My code behind would also declare that btn:

public class XsltGolfer : System.Web.UI.Page		{		protected System.Web.UI.WebControls.Button btnSubmit;

However, as I get more comfortable developing .NET projects, I am realizing that Visual Studio (I assume the same with the Express versions) writes a lot of code that isn't strictly necessary. I have found that I don't need to declare my controls in the code behind file because they have already been declared in the aspx page.You might try modifying your VB from this:

Public Class XsltGolfer		Inherits System.Web.UI.Page		Protected btnSubmit As System.Web.UI.WebControls.Button		Protected pnlSelectGolfer As System.Web.UI.WebControls.Panel		Protected pnlTransformation As System.Web.UI.WebControls.Panel		Protected ddGolferName As System.Web.UI.WebControls.DropDownList		Protected lnkBack As System.Web.UI.WebControls.LinkButton		Protected divTransformation As System.Web.UI.HtmlControls.HtmlGenericControl		Private xmlPath As String		Public Sub New()			AddHandler Page.Init, AddressOf Page_Init		End Sub

To this:

Public Class XsltGolfer		Inherits System.Web.UI.Page		Private xmlPath As String		Public Sub New()			AddHandler Page.Init, AddressOf Page_Init		End Sub

I know it isn't necessary to declare those controls in both places - perhaps it isn't even correct to do so in VB and this may be the cause of your problems - and you will still be able to access the controls in the code behind.I hope this helps!

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...