none
update statement in vb.net RRS feed

  • Question

  • Dear All Below is my update statement . 

       For Each row As DataGridViewRow In CashpaymentDataGridView.Rows
    
    
                '  Using cn As New SqlConnection(appx.myconnection)
    
                Using cn As New SqlConnection(appx.myconnection)
                    cn.Open()
                    Using com As New SqlCommand("", cn)
                        com.CommandText = "Update COA set opening_balance = opening_balance - @dr,type=@type where AccountID=@id"
                            com.Parameters.AddWithValue("@id", row.Cells(3).Value)
                            com.Parameters.AddWithValue("@dr", row.Cells(8).Value)
    
                        com.Parameters.AddWithValue("@type", "CR")
                        com.ExecuteNonQuery()
                        com.CommandText = "Update COA set opening_balance = opening_balance + @dr,type=@typee where AccountID=@id"
                            com.Parameters("@id").Value = row.Cells(5).Value
                            com.Parameters("@dr").Value = row.Cells(7).Value
                        com.Parameters.AddWithValue("@typee", "DR")
                        com.ExecuteNonQuery()
    
    
                    End Using
                End Using
    
    
    
            Next

    now here this script is working fine but what i need is ..... when user save the data, the parameter @typee and @type work as per Chart of account balance.         like 

    if balance in chart of account goes to negative or less then 0 then it must save it as CR else DR . 

    i wrote a code for it but it not working 

                        For ia As Integer = 0 To COADataGridView.Rows.Count - 1
                            If COADataGridView.Rows(ia).Cells(5).Value < 0 Then
    
                                com.Parameters.AddWithValue("@type", "CR")
    
                            Else
    
                                com.Parameters.AddWithValue("@type", "DR")
    
                            End If

    i wrote this code after but it not working .        com.Parameters("@dr").Value = row.Cells(7).Value

    below is my complete code 

            For Each row As DataGridViewRow In CashpaymentDataGridView.Rows
    
    
                '  Using cn As New SqlConnection(appx.myconnection)
    
                Using cn As New SqlConnection(appx.myconnection)
                    cn.Open()
                    Using com As New SqlCommand("", cn)
    
    
                   
    
    
                            com.CommandText = "Update COA set opening_balance = opening_balance - @dr,type=@type where AccountID=@id"
                            com.Parameters.AddWithValue("@id", row.Cells(3).Value)
                            com.Parameters.AddWithValue("@dr", row.Cells(8).Value)
    
    
    
    
                        For ia As Integer = 0 To COADataGridView.Rows.Count - 1
                            If COADataGridView.Rows(ia).Cells(5).Value < 0 Then
    
                                com.Parameters.AddWithValue("@type", "CR")
    
                            Else
    
                                com.Parameters.AddWithValue("@type", "DR")
    
                            End If
    
    
    
                            com.ExecuteNonQuery()
    
    
    
    
                            com.CommandText = "Update COA set opening_balance = opening_balance + @dr,type=@typee where AccountID=@id"
                            com.Parameters("@id").Value = row.Cells(5).Value
                            com.Parameters("@dr").Value = row.Cells(7).Value
    
    
    
                            For ia As Integer = 0 To COADataGridView.Rows.Count - 1
                                If COADataGridView.Rows(ia).Cells(5).Value < 0 Then
    
                                    com.Parameters.AddWithValue("@type", "CR")
    
                                Else
    
                                    com.Parameters.AddWithValue("@type", "DR")
    
                                End If
    
    
    
    
    
                                com.ExecuteNonQuery()
    
                            Next
    
                        Next
                    End Using
                End Using
    
            Next

    please check and guide accordingly 

    Thanks in advance 

    Tuesday, July 16, 2019 6:24 AM

All replies

  • Hi,

    see the following link:

    https://docs.microsoft.com/en-us/dotnet/api/system.data.sqlclient.sqlcommand.parameters?view=netframework-4.8

    fix your code:

                com.Parameters.Add("@id", SqlDbType.Int)
                com.Parameters("@id").Value = row.Cells(5).Value
                com.Parameters.Add("@dr", SqlDbType.VarChar)
                com.Parameters("@dr").Value = row.Cells(7).Value
                com.Parameters.Add("@type", SqlDbType.VarChar)
                com.Parameters("@dr").Value = "DR/CR"

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Tuesday, July 16, 2019 8:42 AM
    Moderator
  • thank you alex but will this code help me to check it the balance of chart of account is less then 0 then type to make it CR or if more then 0 then make it DR

    ?

    Tuesday, July 16, 2019 10:55 AM
  • i tried code but only 1st part of the code is working also i mentioned that i need @type in a way that if

    opening_balance goes to 0 or below 0 then type will change to CR else it will remain DR 

    like if i have 10,000 DR and i am paying 5000 so 5000 remaing will remain DR 



       For Each row As DataGridViewRow In CashpaymentDataGridView.Rows
    
                Dim commandText As String = "Update COA set opening_balance = opening_balance - @dr,type=@type where AccountID=@id"
              
    
    
                Using connection As SqlConnection = New SqlConnection(appx.myconnection)
                    Dim com As SqlCommand = New SqlCommand(commandText, connection)
                    com.Parameters.Add("@id", SqlDbType.Int)
                    com.Parameters("@id").Value = row.Cells(3).Value
                    com.Parameters.Add("@dr", SqlDbType.VarChar)
                    com.Parameters("@dr").Value = row.Cells(8).Value
                    com.Parameters.Add("@type", SqlDbType.VarChar)
                    com.Parameters("@type").Value = "DR/CR"
    
    '-----------------------------------PART 2
    
                    Dim commandText1 As String = "Update COA set opening_balance = opening_balance + @dr,type=@type where AccountID=@id"
                    Dim com1 As SqlCommand = New SqlCommand(commandText1, connection)
                        com1.Parameters.Add("@id", SqlDbType.Int)
                        com1.Parameters("@id").Value = row.Cells(5).Value
                        com1.Parameters.Add("@dr", SqlDbType.VarChar)
                        com1.Parameters("@dr").Value = row.Cells(7).Value
                        com1.Parameters.Add("@type", SqlDbType.VarChar)
                        com1.Parameters("@type").Value = "DR/CR"
    
                        Try
    
                            connection.Open()
    
                            Dim rowsAffected As Int32 = com.ExecuteNonQuery()
                        Dim rowsAffected1 As Int32 = com1.ExecuteNonQuery()
                            Console.WriteLine("RowsAffected:  {0}", rowsAffected)
                        Console.WriteLine("RowsAffected:  {0}", rowsAffected1)
                        Catch ex As Exception
                            Console.WriteLine(ex.Message)
                        End Try
                    End Using
    
    
            Next



    • Edited by Omi4u Tuesday, July 16, 2019 1:20 PM
    Tuesday, July 16, 2019 1:14 PM
  • Hello,

    For the record, if you were using a DataTable the CR/DR would be handled in a expression of a DataColumn. Here is a mock up. This would mean no if statements for CR/DR column.

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) _
            Handles MyBase.Load
    
            Dim dt As New DataTable
    
            dt.Columns.Add(New DataColumn() With
                  {
                      .ColumnName = "id", .DataType = GetType(Integer),
                      .AutoIncrement = True,
                      .AutoIncrementSeed = 1
                  })
    
            dt.Columns.Add(New DataColumn() With
                  {
                        .ColumnName = "Balance",
                        .DataType = GetType(Integer)
                  })
    
            dt.Columns.Add(New DataColumn() With
                  {
                        .ColumnName = "BalanceType",
                        .DataType = GetType(String),
                        .Expression = "IIF(Balance <0,'CR','DR')"
                  })
    
            dt.Rows.Add(New Object() {Nothing, 100})
            dt.Rows.Add(New Object() {Nothing, -1})
    
            DataGridView1.DataSource = dt
        End Sub
    End Class
    


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Tuesday, July 16, 2019 1:33 PM
    Moderator
  • Hi karen Ma'am,

    thank you for your code but i am looking for vb.net code and i tried to convert your code but it showing error . 

    i posted the code that already working fine. only i want to add one condition that if balance in chart of accounts is less then 0 then change type to CR else keep it DR ... i want to add that code or modifications in my code that i posted in the beginning of this thread 

    Wednesday, July 17, 2019 4:59 AM
  • alex bro please guide 
    Wednesday, July 17, 2019 4:59 AM
  • Hi karen Ma'am,

    thank you for your code but i am looking for vb.net code and i tried to convert your code but it showing error . 

    i posted the code that already working fine. only i want to add one condition that if balance in chart of accounts is less then 0 then change type to CR else keep it DR ... i want to add that code or modifications in my code that i posted in the beginning of this thread 

    Hi,

    Karen's vb.net  code is no issue:

    Public Class Form1
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
    
    
            Dim dt As New DataTable
    
            dt.Columns.Add(New DataColumn() With
                  {
                      .ColumnName = "id", .DataType = GetType(Integer),
                      .AutoIncrement = True,
                      .AutoIncrementSeed = 1
                  })
    
            dt.Columns.Add(New DataColumn() With
                  {
                        .ColumnName = "Balance",
                        .DataType = GetType(Integer)
                  })
    
            dt.Columns.Add(New DataColumn() With
                  {
                        .ColumnName = "BalanceType",
                        .DataType = GetType(String),
                        .Expression = "IIF(Balance <0,'CR','DR')"
                  })
    
            dt.Rows.Add(New Object() {Nothing, 100})
            dt.Rows.Add(New Object() {Nothing, -1})
    
            DataGridView1.DataSource = dt
        End Sub
    End Class
    

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 17, 2019 5:08 AM
    Moderator
  • but im getting this error while converting from C to .net

    Error converting code
    Error message:
    CONVERSION ERROR: Conversion for TupleType not implemented, please report this issue in '(New DataColumn' at character 184 at ICSharpCode.CodeConverter.VB.NodesVisitor.DefaultVisit(SyntaxNode node) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.VisitTupleType(TupleTypeSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.TupleTypeSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node) at ICSharpCode.CodeConverter.VB.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.VisitTupleType(TupleTypeSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.TupleTypeSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at ICSharpCode.CodeConverter.VB.NodesVisitor.VisitParameter(ParameterSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.ParameterSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node) at ICSharpCode.CodeConverter.VB.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.VisitParameter(ParameterSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.ParameterSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at ICSharpCode.CodeConverter.VB.NodesVisitor.<VisitParameterList>b__50_0(ParameterSyntax p) at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at Microsoft.CodeAnalysis.VisualBasic.SyntaxFactory.SeparatedList[TNode](IEnumerable`1 nodes) at ICSharpCode.CodeConverter.VB.NodesVisitor.VisitParameterList(ParameterListSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.ParameterListSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node) at ICSharpCode.CodeConverter.VB.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.VisitParameterList(ParameterListSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.ParameterListSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at ICSharpCode.CodeConverter.VB.NodesVisitor.VisitMethodDeclaration(MethodDeclarationSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.MethodDeclarationSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node) at ICSharpCode.CodeConverter.VB.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.VisitMethodDeclaration(MethodDeclarationSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.MethodDeclarationSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at ICSharpCode.CodeConverter.VB.NodesVisitor.<VisitClassDeclaration>b__23_0(MemberDeclarationSyntax m) at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at ICSharpCode.CodeConverter.VB.NodesVisitor.VisitClassDeclaration(ClassDeclarationSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.ClassDeclarationSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node) at ICSharpCode.CodeConverter.VB.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.VisitClassDeclaration(ClassDeclarationSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.ClassDeclarationSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at ICSharpCode.CodeConverter.VB.NodesVisitor.<VisitCompilationUnit>b__15_1(MemberDeclarationSyntax m) at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at Microsoft.CodeAnalysis.SyntaxList`1.CreateNode(IEnumerable`1 nodes) at Microsoft.CodeAnalysis.SyntaxList`1..ctor(IEnumerable`1 nodes) at Microsoft.CodeAnalysis.VisualBasic.SyntaxFactory.List[TNode](IEnumerable`1 nodes) at ICSharpCode.CodeConverter.VB.NodesVisitor.VisitCompilationUnit(CompilationUnitSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.CompilationUnitSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node) at ICSharpCode.CodeConverter.VB.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.VisitCompilationUnit(CompilationUnitSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.CompilationUnitSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at ICSharpCode.CodeConverter.VB.CSharpConverter.ConvertCompilationTree(CSharpCompilation compilation, CSharpSyntaxTree tree) at ICSharpCode.CodeConverter.VB.CSToVBConversion.SingleFirstPass(Compilation sourceCompilation, SyntaxTree tree) at ICSharpCode.CodeConverter.Shared.ProjectConversion.SingleFirstPass(SyntaxTree tree, String treeFilePath) at ICSharpCode.CodeConverter.Shared.ProjectConversion.FirstPass()

    Wednesday, July 17, 2019 6:01 AM
  • Hi,

    give me your code, I will check it for you.

    Best Regards,

    Alex


    MSDN Community Support Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, July 17, 2019 6:03 AM
    Moderator
  • Hi karen Ma'am,

    thank you for your code but i am looking for vb.net code and i tried to convert your code but it showing error . 

    i posted the code that already working fine. only i want to add one condition that if balance in chart of accounts is less then 0 then change type to CR else keep it DR ... i want to add that code or modifications in my code that i posted in the beginning of this thread 

    Hello, the code I provided is VB.NET while C# representation is shown here.

    var dt = new DataTable();
    
    dt.Columns.Add(new DataColumn()
    {
        ColumnName = "id",
        DataType = typeof(int),
        AutoIncrement = true,
        AutoIncrementSeed = 1
    });
    
    dt.Columns.Add(new DataColumn()
    {
        ColumnName = "Balance",
        DataType = typeof(int)
    });
    
    dt.Columns.Add(new DataColumn()
    {
        ColumnName = "BalanceType",
        DataType = typeof(string),
        Expression = "IIF(Balance <0,'CR','DR')"
    });
    
    dt.Rows.Add(null, 100);
    dt.Rows.Add(null, -1);
    
    DataGridView1.DataSource = dt;
    The VB.NET code will set CR for less than 0, otherwise will be DR. The idea here is to write less code to achieve your results asked for. 


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, July 17, 2019 10:08 AM
    Moderator
  • i tried to convert your code from c to .net but getting following error 

    Error converting code
    Error message:
    CONVERSION ERROR: Conversion for IncompleteMember not implemented, please report this issue in 'dt.Columns.Add' at character 54 at ICSharpCode.CodeConverter.VB.NodesVisitor.DefaultVisit(SyntaxNode node) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.VisitIncompleteMember(IncompleteMemberSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.IncompleteMemberSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node) at ICSharpCode.CodeConverter.VB.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.VisitIncompleteMember(IncompleteMemberSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.IncompleteMemberSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at ICSharpCode.CodeConverter.VB.NodesVisitor.<VisitClassDeclaration>b__23_0(MemberDeclarationSyntax m) at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at ICSharpCode.CodeConverter.VB.NodesVisitor.VisitClassDeclaration(ClassDeclarationSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.ClassDeclarationSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node) at ICSharpCode.CodeConverter.VB.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.VisitClassDeclaration(ClassDeclarationSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.ClassDeclarationSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at ICSharpCode.CodeConverter.VB.NodesVisitor.<VisitCompilationUnit>b__15_1(MemberDeclarationSyntax m) at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at Microsoft.CodeAnalysis.SyntaxList`1.CreateNode(IEnumerable`1 nodes) at Microsoft.CodeAnalysis.SyntaxList`1..ctor(IEnumerable`1 nodes) at Microsoft.CodeAnalysis.VisualBasic.SyntaxFactory.List[TNode](IEnumerable`1 nodes) at ICSharpCode.CodeConverter.VB.NodesVisitor.VisitCompilationUnit(CompilationUnitSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.CompilationUnitSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node) at ICSharpCode.CodeConverter.VB.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.VisitCompilationUnit(CompilationUnitSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.CompilationUnitSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at ICSharpCode.CodeConverter.VB.CSharpConverter.ConvertCompilationTree(CSharpCompilation compilation, CSharpSyntaxTree tree) at ICSharpCode.CodeConverter.VB.CSToVBConversion.SingleFirstPass(Compilation sourceCompilation, SyntaxTree tree) at ICSharpCode.CodeConverter.Shared.ProjectConversion.SingleFirstPass(SyntaxTree tree, String treeFilePath) at ICSharpCode.CodeConverter.Shared.ProjectConversion.FirstPass()

    Wednesday, July 17, 2019 11:40 AM
  • i tried to convert your code from c to .net but getting following error 

    Error converting code
    Error message:
    CONVERSION ERROR: Conversion for IncompleteMember not implemented, please report this issue in 'dt.Columns.Add' at character 54 at ICSharpCode.CodeConverter.VB.NodesVisitor.DefaultVisit(SyntaxNode node) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.VisitIncompleteMember(IncompleteMemberSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.IncompleteMemberSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node) at ICSharpCode.CodeConverter.VB.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.VisitIncompleteMember(IncompleteMemberSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.IncompleteMemberSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at ICSharpCode.CodeConverter.VB.NodesVisitor.<VisitClassDeclaration>b__23_0(MemberDeclarationSyntax m) at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at System.Collections.Generic.List`1..ctor(IEnumerable`1 collection) at System.Linq.Enumerable.ToList[TSource](IEnumerable`1 source) at ICSharpCode.CodeConverter.VB.NodesVisitor.VisitClassDeclaration(ClassDeclarationSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.ClassDeclarationSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node) at ICSharpCode.CodeConverter.VB.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.VisitClassDeclaration(ClassDeclarationSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.ClassDeclarationSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at ICSharpCode.CodeConverter.VB.NodesVisitor.<VisitCompilationUnit>b__15_1(MemberDeclarationSyntax m) at System.Linq.Enumerable.WhereSelectEnumerableIterator`2.MoveNext() at Microsoft.CodeAnalysis.SyntaxList`1.CreateNode(IEnumerable`1 nodes) at Microsoft.CodeAnalysis.SyntaxList`1..ctor(IEnumerable`1 nodes) at Microsoft.CodeAnalysis.VisualBasic.SyntaxFactory.List[TNode](IEnumerable`1 nodes) at ICSharpCode.CodeConverter.VB.NodesVisitor.VisitCompilationUnit(CompilationUnitSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.CompilationUnitSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.Visit(SyntaxNode node) at ICSharpCode.CodeConverter.VB.CommentConvertingNodesVisitor.DefaultVisit(SyntaxNode node) at Microsoft.CodeAnalysis.CSharp.CSharpSyntaxVisitor`1.VisitCompilationUnit(CompilationUnitSyntax node) at Microsoft.CodeAnalysis.CSharp.Syntax.CompilationUnitSyntax.Accept[TResult](CSharpSyntaxVisitor`1 visitor) at ICSharpCode.CodeConverter.VB.CSharpConverter.ConvertCompilationTree(CSharpCompilation compilation, CSharpSyntaxTree tree) at ICSharpCode.CodeConverter.VB.CSToVBConversion.SingleFirstPass(Compilation sourceCompilation, SyntaxTree tree) at ICSharpCode.CodeConverter.Shared.ProjectConversion.SingleFirstPass(SyntaxTree tree, String treeFilePath) at ICSharpCode.CodeConverter.Shared.ProjectConversion.FirstPass()

    I don't understand why you believe you need to convert code??? I gave you VB.NET code the first time then showed you what it would be in C# which is only there to show that the first reply is VB.NET not C#. So if I convert FROM C# to VB.NET we get the following.

    Dim dt = New DataTable()
    
    dt.Columns.Add(New DataColumn() With {
                      .ColumnName = "id",
                      .DataType = GetType(Integer),
                      .AutoIncrement = True,
                      .AutoIncrementSeed = 1
                      })
    
    dt.Columns.Add(New DataColumn() With {
                      .ColumnName = "Balance",
                      .DataType = GetType(Integer)
                      })
    
    dt.Columns.Add(New DataColumn() With {
                      .ColumnName = "BalanceType",
                      .DataType = GetType(String),
                      .Expression = "IIF(Balance <0,'CR','DR')"
                      })
    
    dt.Rows.Add(Nothing, 100)
    dt.Rows.Add(Nothing, -1)
    
    DataGridView1.DataSource = dt

    Both of the code samples, VB.NET and C# when converted from one to the other compile perfectly.

    One last thing, I compile with Option Infer On which mirrors C#.



    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, July 17, 2019 12:23 PM
    Moderator