Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Having problems converting a VB.NET code to C#

Posted on 2009-12-22
4
Medium Priority
?
260 Views
Last Modified: 2012-05-08
Hello experts,
I've got a buton which was originally created using VB.NET but just recently i decided to learn C# thus converted the whole page including the codes beneath the controls. Initially i was using the free online webconverter but unfortunately not lucky enough coz i'm having heaps of errors
The logic of the button OnClick event is to generate an XML file based on the values passed as a parameter that invokes a stored procedure.
The code i've got works using VB but i'm having trouble running it on C# (after conversion).
Any help would do...
###############
##  VB Code  ##
###############
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
        Using conn As New Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString2").ConnectionString)
            Dim cmd As New Data.SqlClient.SqlCommand("MyProc", conn)
            cmd.CommandType = Data.CommandType.StoredProcedure
            conn.Open()

            cmd.Parameters.Add("@lID", Data.SqlDbType.VarChar)
            cmd.Parameters("@ID").Value = TextBox1.Text.ToString()

            cmd.Parameters.Add("@Parm1", Data.SqlDbType.Int)
            cmd.Parameters("@Parm1").Value = TextBox2.Text.ToString()

            cmd.Parameters.Add("@Parm2", Data.SqlDbType.VarChar)
            cmd.Parameters("@Parm2").Value = TextBox3.Text.ToString()

            cmd.Parameters.Add("@Parm3", Data.SqlDbType.VarChar)
            cmd.Parameters("@Parm3").Value = TextBox4.Text.ToString()

            Dim da As New Data.SqlClient.SqlDataAdapter(cmd)
            Dim ds As New Data.DataSet
            da.Fill(ds)
            Dim nowString As String = Now.ToString("dd-MM-yyyy hh.mm.ss")
            Dim fileName As String = "C:\TEMP\MyFile_" & nowString & ".xml"
            ds.WriteXml(fileName)
            Dim doc As System.Xml.XmlDataDocument = New System.Xml.XmlDataDocument(ds)
            Dim transform As New System.Xml.Xsl.XslCompiledTransform()
            transform.Load("C:\MyXSLTemplate.xsl")
            Dim writer As System.Xml.XmlTextWriter = New System.Xml.XmlTextWriter(fileName, System.Text.Encoding.GetEncoding("iso-8859-1"))
            writer.WriteStartDocument()
            writer.Formatting = System.Xml.Formatting.Indented
            writer.IndentChar = Chr(9)
            writer.Indentation = 1
            transform.Transform(doc, Nothing, writer)
        End Using
    End Sub



##########################################################
## Converted to C#. (but is generating heaps of errors) ##
##########################################################
protected void // ERROR: Handles clauses are not supported in C# Button1_Click(object sender, System.EventArgs e) 
{ 
    using (Data.SqlClient.SqlConnection conn = new Data.SqlClient.SqlConnection(ConfigurationManager.ConnectionStrings("ConnectionString2").ConnectionString)) { 
        Data.SqlClient.SqlCommand cmd = new Data.SqlClient.SqlCommand("MyProc", conn); 
        cmd.CommandType = Data.CommandType.StoredProcedure; 
        conn.Open(); 
        
        cmd.Parameters.Add("@lID", Data.SqlDbType.VarChar); 
        cmd.Parameters("@ID").Value = TextBox1.Text.ToString(); 
        
        cmd.Parameters.Add("@Parm1", Data.SqlDbType.Int); 
        cmd.Parameters("@Parm1").Value = TextBox2.Text.ToString(); 
        
        cmd.Parameters.Add("@Parm2", Data.SqlDbType.VarChar); 
        cmd.Parameters("@Parm2").Value = TextBox3.Text.ToString(); 
        
        cmd.Parameters.Add("@Parm3", Data.SqlDbType.VarChar); 
        cmd.Parameters("@Parm3").Value = TextBox4.Text.ToString(); 
        
        Data.SqlClient.SqlDataAdapter da = new Data.SqlClient.SqlDataAdapter(cmd); 
        Data.DataSet ds = new Data.DataSet(); 
        da.Fill(ds); 
        string nowString = Now.ToString("dd-MM-yyyy hh.mm.ss"); 
        string fileName = "C:\\TEMP\\MyFile_" + nowString + ".xml"; 
        ds.WriteXml(fileName); 
        System.Xml.XmlDataDocument doc = new System.Xml.XmlDataDocument(ds); 
        System.Xml.Xsl.XslCompiledTransform transform = new System.Xml.Xsl.XslCompiledTransform(); 
        transform.Load("C:\\MyXSLTemplate.xsl"); 
        System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(fileName, System.Text.Encoding.GetEncoding("iso-8859-1")); 
        writer.WriteStartDocument(); 
        writer.Formatting = System.Xml.Formatting.Indented; 
        writer.IndentChar = Strings.Chr(9); 
        writer.Indentation = 1; 
        transform.Transform(doc, null, writer); 
    } 
}

Open in new window

0
Comment
Question by:jsuanque
  • 2
4 Comments
 
LVL 61

Accepted Solution

by:
HainKurt earned 1000 total points
ID: 26110208
here it is... (I guess so ;)
using System.Data.SqlClient;

    protected void button1_Click(object sender, EventArgs e)
    {
        using (SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString2"].ConnectionString))
        {
            SqlCommand cmd = new SqlCommand("MyProc", conn);
            cmd.CommandType = CommandType.StoredProcedure;
            conn.Open();

            cmd.Parameters.Add("@lID", SqlDbType.VarChar);
            cmd.Parameters["@ID"].Value = TextBox1.Text;

            /* similar to above
            cmd.Parameters.Add("@Parm1", SqlDbType.Int);
            cmd.Parameters("@Parm1").Value = TextBox2.Text.ToString();

            cmd.Parameters.Add("@Parm2", SqlDbType.VarChar);
            cmd.Parameters("@Parm2").Value = TextBox3.Text.ToString();

            cmd.Parameters.Add("@Parm3", SqlDbType.VarChar);
            cmd.Parameters("@Parm3").Value = TextBox4.Text.ToString();*/

            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            string nowString = DateTime.Now.ToString("dd-MM-yyyy hh.mm.ss");
            string fileName = "C:\\TEMP\\MyFile_" + nowString + ".xml";
            ds.WriteXml(fileName);
            System.Xml.XmlDataDocument doc = new System.Xml.XmlDataDocument(ds);
            System.Xml.Xsl.XslCompiledTransform transform = new System.Xml.Xsl.XslCompiledTransform();
            transform.Load("C:\\MyXSLTemplate.xsl");
            System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(fileName, System.Text.Encoding.GetEncoding("iso-8859-1"));
            writer.WriteStartDocument();
            writer.Formatting = System.Xml.Formatting.Indented;
            writer.IndentChar = (char)(9);
            writer.Indentation = 1;
            transform.Transform(doc, null, writer);
        }
    }
}

Open in new window

0
 
LVL 15

Expert Comment

by:rajeeshmca
ID: 26110211
Hi  jsuanque,,

can i know what is the error u are getting
0
 

Author Closing Comment

by:jsuanque
ID: 31669300
Thanks. Merry Christmas ...it works!!!
0
 

Author Comment

by:jsuanque
ID: 26110285
Hello Rajeeshmca...i guess it's all due to syntactic ones coz comparing from what HainKurt provided...here are the difference...
MyInitial Code:
cmd.Parameters("@ID").Value = TextBox1.Text.ToString();

HainKurt:
 cmd.Parameters["@ID"].Value = TextBox1.Text;

And so on...sorry could not get the error message now coz it already works.

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
In a question here at Experts Exchange (https://www.experts-exchange.com/questions/29062564/Adobe-acrobat-reader-DC.html), a member asked how to create a signature in Adobe Acrobat Reader DC (the free Reader product, not the paid, full Acrobat produ…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses
Course of the Month20 days, 22 hours left to enroll

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question