When you're building text lists or JSON arrays inside an ASP.NET loop, you usually end up with an annoying extra comma on the very last item. This VB.NET script fixes that. It loops through your database rows, numbers them on the fly, and removes trailing commas perfectly.
[ASP.NET - VB.NET Comma Delimited Numbered List]
CFFCS | CarrzSynEdit: | ASP.NET (VB/C#)
Dim getID as Integer = request.querystring("ID")
Dim queryYear As String = "SELECT Year from MyTable where ColID = @ColID"

Using conn As New SqlConnection(ConfigurationManager.ConnectionStrings("MyConnection").ConnectionString)
    Using cmd As New SqlCommand(queryMonth, conn)
        cmd.Parameters.AddWithValue("@ColID", getID)
        conn.Open()
        Using readerYear As SqlDataReader = cmd.ExecuteReader()
            
            Dim isFirst As Boolean = True
			Dim positionCounter As Integer = 1
            
            While readerYear.Read()
                ' If it is NOT the first item, append a comma to the PREVIOUS item before writing the next one
                If Not isFirst Then
                    jsonBuilder.AppendLine(",")
                End If
                
                ' Write the JSON object without a trailing comma or newline at the end
                jsonBuilder.Append("    { ""@type"": ""ListItem"", ""position"": " & positionCounter & ", ""name"": """ & game & " " & readerYear("Year").ToString() & " Archive"" }")
                
				positionCounter += 1
                isFirst = False
            End While
            
            ' Add a final newline after the loop finishes if any records were read
            If Not isFirst Then
                jsonBuilder.AppendLine()
            End If
            
        End Using
    End Using
End Using

[JavaScript - JSON Example]
CFFCS | CarrzSynEdit: | JS (JavaScript)
<script type="application/ld+json">
{
  "@context": "https://schema.org",
  "@type": "ItemList",
  "name": "MyList Years History Archive",
  "description": "Browse past history for MyList grouped by year.",
  "itemListElement": [
    { "@type": "ListItem", "position": 1, "name": "MyList 2010 Archive" },
    { "@type": "ListItem", "position": 2, "name": "MyList 2011 Archive" },
    { "@type": "ListItem", "position": 3, "name": "MyList 2012 Archive" },
    { "@type": "ListItem", "position": 4, "name": "MyList 2013 Archive" },
    { "@type": "ListItem", "position": 5, "name": "MyList 2014 Archive" },
    { "@type": "ListItem", "position": 6, "name": "MyList 2015 Archive" },
    { "@type": "ListItem", "position": 7, "name": "MyList 2016 Archive" },
    { "@type": "ListItem", "position": 8, "name": "MyList 2017 Archive" },
    { "@type": "ListItem", "position": 9, "name": "MyList 2018 Archive" },
    { "@type": "ListItem", "position": 10, "name": "MyList 2019 Archive" },
    { "@type": "ListItem", "position": 11, "name": "MyList 2020 Archive" },
    { "@type": "ListItem", "position": 12, "name": "MyList 2021 Archive" },
    { "@type": "ListItem", "position": 13, "name": "MyList 2022 Archive" },
    { "@type": "ListItem", "position": 14, "name": "MyList 2023 Archive" },
    { "@type": "ListItem", "position": 15, "name": "MyList 2024 Archive" },
    { "@type": "ListItem", "position": 16, "name": "MyList 2025 Archive" },
    { "@type": "ListItem", "position": 17, "name": "MyList 2026 Archive" }
  ]
}
</script>

Other Articles Related to this Entry.ASP.NET (C#.NET) List comma-delimited numbered list with no comma at the end«