locked
HELP: Conver C# codes to VB codes RRS feed

  • Question

  • User-830563764 posted

    I have found a useful example ASPnet Web page coded in C#.  Your help is greatly appreciated if the C# code is coverted to VB.


    *********************************************************************************************
    private void Page_Load(object sender, System.EventArgs e)
    {
        if (!IsPostBack)

       {
           SqlConnection objConn = new SqlConnection(Configuration.AppSettings["DSN"]);
           SqlCommand objCmd = new SqlCommand("select TestName from SpecialTests oder by TestName", objConn);
           objConn.Open();
           cblTests.DataSource = objCmd.ExecuteReader(CommandBehavior, CloseConnection):
           cblTests.DataBind();
     

          objCmd = new SqlCommand("select # from ConsultTests where ConsultPK=intUserID,objConn);
          objConn.Open();
          SqlDataReader objReader = objCmd.ExecuteReader();
         

         While (objReader.Read())
         {
            ListItem currentCheckBox = cblTests.Items.FindByValue(objReader("TestName".ToString());
            if (currentCheckBox != null)
            {
                 currentCheckBox .Selected = true;
            }
         }
       }
    }

          

     

    Saturday, October 4, 2008 3:19 PM

Answers

  • User397347636 posted

    No converter will be able to convert code which has syntax errors in the original language.

    1. The "select..." string has no closing double quote.

    2. "While" is not C# syntax.  "while" is.

    3. The statement starting with "ListItem" is missing a closing parenthesis.

    After correcting for this, the VB equivalent is (via Instant VB):

     

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    	If (Not IsPostBack) Then
    
    	   Dim objConn As New SqlConnection(Configuration.AppSettings("DSN"))
    	   Dim objCmd As New SqlCommand("select TestName from SpecialTests oder by TestName", objConn)
    	   objConn.Open()
    	   cblTests.DataSource = objCmd.ExecuteReader(CommandBehavior, CloseConnection):
    	   cblTests.DataBind()
    
    
    	  objCmd = New SqlCommand("select # from ConsultTests where ConsultPK=intUserID",objConn)
    	  objConn.Open()
    	  Dim objReader As SqlDataReader = objCmd.ExecuteReader()
    
    
    	 Do While objReader.Read()
    		Dim currentCheckBox As ListItem = cblTests.Items.FindByValue(objReader("TestName".ToString()))
    		If currentCheckBox IsNot Nothing Then
    			 currentCheckBox.Selected = True
    		End If
    	 Loop
    	End If
    End Sub
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 4, 2008 8:24 PM

All replies

  • User-158764254 posted

    A variety of online conversion services have been listed in this FAQ: http://forums.asp.net/t/1100603.aspx

    Saturday, October 4, 2008 6:48 PM
  • User397347636 posted

    No converter will be able to convert code which has syntax errors in the original language.

    1. The "select..." string has no closing double quote.

    2. "While" is not C# syntax.  "while" is.

    3. The statement starting with "ListItem" is missing a closing parenthesis.

    After correcting for this, the VB equivalent is (via Instant VB):

     

    Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
    	If (Not IsPostBack) Then
    
    	   Dim objConn As New SqlConnection(Configuration.AppSettings("DSN"))
    	   Dim objCmd As New SqlCommand("select TestName from SpecialTests oder by TestName", objConn)
    	   objConn.Open()
    	   cblTests.DataSource = objCmd.ExecuteReader(CommandBehavior, CloseConnection):
    	   cblTests.DataBind()
    
    
    	  objCmd = New SqlCommand("select # from ConsultTests where ConsultPK=intUserID",objConn)
    	  objConn.Open()
    	  Dim objReader As SqlDataReader = objCmd.ExecuteReader()
    
    
    	 Do While objReader.Read()
    		Dim currentCheckBox As ListItem = cblTests.Items.FindByValue(objReader("TestName".ToString()))
    		If currentCheckBox IsNot Nothing Then
    			 currentCheckBox.Selected = True
    		End If
    	 Loop
    	End If
    End Sub
     
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Saturday, October 4, 2008 8:24 PM
  • User-830563764 posted

    Thank you very much.  I did notice the syntax errors.  However, after correcting the errors, I still can not
    run the above 2 websites to convert my C# codes.

    Will try your VB codes next Monday when back to work.  Thanks.  

    Saturday, October 4, 2008 10:10 PM
  • User-830563764 posted

     Anything wrong with the following C# code. The Converter website indicated there was an error:
      '------^--- Syntax error: ';' expected

     Line 1?

     

    Line 1       While (objReader.Read())
    Line 2         {
    Line 3          ListItem currentCheckBox = cblTests.Items.FindByValue(objReader["TestName"].ToString());
    Line 4          if (currentCheckBox != null)
    Line 5         {
    Line 6             currentCheckBox.Selected = true;
    Line 7         }
    Line 8         }

    Saturday, October 4, 2008 11:31 PM
  • User-830563764 posted

     I found the error: while, not While

    Let me try again.

    Sunday, October 5, 2008 12:41 AM
  • User-830563764 posted

    Finally, I found   "http://converter.telerik.com/ " worked for my C# code after correcting the errors.  Thank you all.

    Sunday, October 5, 2008 1:01 AM
  • User-830563764 posted

     Thanks for your VB code.  I have successfully inserted these codes into my existing aspx.vb file.
    However, somehow, the preloading checkBoxList.selected items based on my ConsultTests table
    did not displayed as 'checked buttons.'  

    To the best of my knowledge, neith FindByValue nor FindByText worked for my application.
    (Debugging showed Error something like: FindByValue/text not declared.)  Any suggestions for
    coding objReader ("TestName".ToString())?    

    Thanks,
    Jeffrey

    Monday, October 6, 2008 8:26 PM