locked
C# > VB cannot make it. RRS feed

  • Question

  • User-742754981 posted

    Hello,

    I am trying to convert a C# file from this article  into VB to use in my VB web application. I don't want to use the convert to dll and add reference method. It works in C# very well when I test it at another web site. when I convert it into VB with various tools it shows no errors and no results as well. I wonder what the problem is with the conversion.

    C# Code Behind of GSiteMap.aspx:

    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;
    using System.Xml;
    using System.Web.Configuration;
    public partial class GSiteMap : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            Response.Clear();
            Response.ContentType = "text/xml";
            using (XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8))
            {
                writer.WriteStartDocument();
                writer.WriteStartElement("urlset");
                writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9");
                writer.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
                writer.WriteAttributeString("xsi:schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd");
                writer.WriteStartElement("url");
    
                string connect = WebConfigurationManager.ConnectionStrings["MyConn"].ConnectionString;
                using (SqlConnection conn = new SqlConnection(connect))
                {
                    using (SqlCommand cmd = new SqlCommand("GetSiteMapContent", conn))
                    {
                        cmd.CommandType = CommandType.StoredProcedure;
                        conn.Open();
                        using (SqlDataReader rdr = cmd.ExecuteReader())
                        {
                            // Get the date of the most recent article
                            rdr.Read();
                            writer.WriteElementString("loc", "http://www.example.com/Default.aspx");
                            writer.WriteElementString("lastmod", String.Format("{0:yyyy-MM-dd}", rdr[0]));
                            writer.WriteElementString("changefreq", "daily");
                            writer.WriteElementString("priority", "1.0");
                            writer.WriteEndElement();
                            // Move to the Article IDs
                            rdr.NextResult();
                            while (rdr.Read())
                            {
                                writer.WriteStartElement("url");
                                writer.WriteElementString("loc", "http://www.example.com/c.aspx?g=" + rdr[1].ToString() + "&k=" + rdr[0].ToString());
                                if (rdr[1] != DBNull.Value)
                                    writer.WriteElementString("lastmod", String.Format("{0:yyyy-MM-dd}", rdr[2]));
                                writer.WriteElementString("changefreq", "monthly");
                                writer.WriteElementString("priority", "0.5");
                                writer.WriteEndElement();
                            }
                            // Finally move to the Category IDs
                            rdr.NextResult();
                            while (rdr.Read())
                            {
                                writer.WriteStartElement("url");
                                writer.WriteElementString("loc", "http://www.example.com/l.aspx?g=" + rdr[0].ToString());
                                writer.WriteElementString("priority", "0.5");
                                writer.WriteEndElement();
                            }
                            writer.WriteEndElement();
                            writer.WriteEndDocument();
                            writer.Flush();
                        }
                        Response.End();
                    }
                }
            }
        }
    }


    VB Code Behind of GSiteMap.aspx That doesn't return any results:

    Imports System
    Imports System.Data
    Imports System.Data.SqlClient
    Imports System.Text
    Imports System.Xml
    Imports System.Web.Configuration
    Partial Public Class GSiteMap
    	Inherits System.Web.UI.Page
    	Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)
    		Response.Clear()
    		Response.ContentType = "text/xml"
    		Using writer As New XmlTextWriter(Response.OutputStream, Encoding.UTF8)
    			writer.WriteStartDocument()
    			writer.WriteStartElement("urlset")
    			writer.WriteAttributeString("xmlns", "http://www.sitemaps.org/schemas/sitemap/0.9")
    			writer.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance")
    			writer.WriteAttributeString("xsi:schemaLocation", "http://www.sitemaps.org/schemas/sitemap/0.9 http://www.sitemaps.org/schemas/sitemap/0.9/sitemap.xsd")
    			writer.WriteStartElement("url")
    
    			Dim connect As String = WebConfigurationManager.ConnectionStrings("MyConn").ConnectionString
    			Using conn As New SqlConnection(connect)
    				Using cmd As New SqlCommand("GetSiteMapContent", conn)
    					cmd.CommandType = CommandType.StoredProcedure
    					conn.Open()
    					Using rdr As SqlDataReader = cmd.ExecuteReader()
    						' Get the date of the most recent article
    						rdr.Read()
    						writer.WriteElementString("loc", "http://www.example.com/Default.aspx")
    						writer.WriteElementString("lastmod", String.Format("{0:yyyy-MM-dd}", rdr(0)))
    						writer.WriteElementString("changefreq", "daily")
    						writer.WriteElementString("priority", "1.0")
    						writer.WriteEndElement()
    						' Move to the Article IDs
    						rdr.NextResult()
    						Do While rdr.Read()
    							writer.WriteStartElement("url")
    							writer.WriteElementString("loc", "http://www.example.com/c.aspx?g=" & rdr(1).ToString() & "&k=" & rdr(0).ToString())
    							İf rdr(1) IsNot DBNull.Value Then
    								writer.WriteElementString("lastmod", String.Format("{0:yyyy-MM-dd}", rdr(2)))
    							End If
    							writer.WriteElementString("changefreq", "monthly")
    							writer.WriteElementString("priority", "0.5")
    							writer.WriteEndElement()
    						Loop
    						' Finally move to the Category IDs
    						rdr.NextResult()
    						Do While rdr.Read()
    							writer.WriteStartElement("url")
    							writer.WriteElementString("loc", "http://www.example.com/l.aspx?g=" & rdr(0).ToString())
    							writer.WriteElementString("priority", "0.5")
    							writer.WriteEndElement()
    						Loop
    						writer.WriteEndElement()
    						writer.WriteEndDocument()
    						writer.Flush()
    					End Using
    					Response.End()
    				End Using
    			End Using

    thanks in advance...

    Saturday, January 22, 2011 4:22 AM

Answers

  • User-417784260 posted

    Two things.  


    In VB the Page load function needs an handles me.load after it

    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgsHandles Me.Load

    Second remember a using block in addition to making sure your variable gets disposed it wraps the code in a try catch block.  If adding the me.load does not solve the problem step through the code and make sure you are not getting an error.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 22, 2011 6:27 AM

All replies

  • User-693248168 posted

    Hi,

    Why is the link showing 

    http://www.example.k12.tr/Default.aspx")

    but in C# 

    it shows only

    "http://www.example.com/Default.aspx"


    It is in your line

     writer.WriteElementString("loc", "http://www.example.k12.tr/Default.aspx")


    Saturday, January 22, 2011 6:25 AM
  • User-417784260 posted

    Two things.  


    In VB the Page load function needs an handles me.load after it

    Protected Sub Page_Load(ByVal sender As ObjectByVal e As System.EventArgsHandles Me.Load

    Second remember a using block in addition to making sure your variable gets disposed it wraps the code in a try catch block.  If adding the me.load does not solve the problem step through the code and make sure you are not getting an error.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, January 22, 2011 6:27 AM
  • User-742754981 posted

    Thank you so much Ken. you saved my day.

    Saturday, January 22, 2011 6:48 AM