locked
PROBLEM CODE CONVERSION ONE FUNCTION RRS feed

  • Question

  • good afternoon I'm making an application in vb.net 2015 and sql 2014 I have this code in c # I convert it to vb .net but it throws me problem someone could help me

    original code

     public ArrayList RetreiveSaleItems(int SaleID)
            {
                ArrayList ProductsList = new ArrayList();
    
                using (SqlConnection connection = new SqlConnection(ConnectionString))
                {
                    SqlCommand command = new SqlCommand("SELECT ProductName, ProductPrice, ProductQuantity, ProductTotal FROM SaleItems where SaleID = '" + SaleID + "';", connection);
                    connection.Open();
    
                    SqlDataReader reader = command.ExecuteReader();
    
                    if (reader.HasRows)
                    {
                        while (reader.Read())
                        {
                            string ProductName = reader.GetString(0);
                            decimal ProductPrice = reader.GetDecimal(1);
                            int ProductQuantity = reader.GetInt32(2);
                            decimal ProductTotal = reader.GetDecimal(3);
    
                            ProductsList.Add(new Details() { Name = ProductName, Price = ProductPrice, Quantity = ProductQuantity, Total = ProductTotal });
                        }
                    }
                    reader.Close();
    
                    return ProductsList;
                }
            }
        }

    converted code

      Public Function RetreiveSaleItems(SaleID As Integer) As ArrayList
            Dim ProductsList As New ArrayList()
            Using connection As New SqlConnection(ConnectionString)
                Dim command As New SqlCommand("SELECT ProductName, ProductPrice, ProductQuantity, ProductTotal FROM SaleItems where SaleID = '" & SaleID & "';", connection)
                connection.Open()
                Dim reader As SqlDataReader = command.ExecuteReader()
                If reader.HasRows Then
                    While reader.Read()
                        Dim ProductName As String = reader.GetString(0)
                        Dim ProductPrice As Decimal = reader.GetDecimal(1)
                        Dim ProductQuantity As Integer = reader.GetInt32(2)
                        Dim ProductTotal As Decimal = reader.GetDecimal(3)
                            Function ProductsList.Add(Details( As New)
     Name = ProductName, Price = ProductPrice, Quantity = ProductQuantity, Total = ProductTotal)
     End Function
    
    
        End While
        End If
    
        End Using
                    reader.Close()
                    Return ProductsList
    
        End Function

    Thursday, March 22, 2018 8:15 PM

All replies

  •    ProductsList.Add(new Details with {.Name = ProductName, .Price = ProductPrice, .Quantity = ProductQuantity, .Total = ProductTotal})
    

    Success
    Cor

    Thursday, March 22, 2018 8:30 PM
  •    ProductsList.Add(new Details with {.Name = ProductName, .Price = ProductPrice, .Quantity = ProductQuantity, .Total = ProductTotal})
    

    Success
    Cor

    Cor lightert thanks for answering that part served me but I'm missing the other part sends me error

      Public Function RetreiveSaleItems(SaleID As Integer) As ArrayList
            Dim ProductsList As New ArrayList()
            Using connection As New SqlConnection(ConnectionString)
                Dim command As New SqlCommand("SELECT ProductName, ProductPrice, ProductQuantity, ProductTotal FROM SaleItems where SaleID = '" & SaleID & "';", connection)
                connection.Open()
                Dim reader As SqlDataReader = command.ExecuteReader()
                If reader.HasRows Then
                    While reader.Read()
                        Dim ProductName As String = reader.GetString(0)
                        Dim ProductPrice As Decimal = reader.GetDecimal(1)
                        Dim ProductQuantity As Integer = reader.GetInt32(2)
                        Dim ProductTotal As Decimal = reader.GetDecimal(3)
                        ProductsList.Add(New Details With {.Name = ProductName, .Price = ProductPrice, .Quantity = ProductQuantity, .Total = ProductTotal})
    
     End Function error
    
    
        End While error
        End If  error
    
        End Using  error
                    reader.Close()  error
                    Return ProductsList error
    
        End Function error
    

    Friday, March 23, 2018 1:30 PM
  • Get rid of that first "End Function" - with Cor's answer you no longer have a lambda, so you don't need to end the lambda.

    Convert between VB, C#, C++, & Java (http://www.tangiblesoftwaresolutions.com)
    Instant C# - VB to C# Converter
    Instant VB - C# to VB Converter

    Friday, March 23, 2018 2:10 PM