How would this vb code be in c #

0

I need to show data that is in a database. I have something similar done in vb.net but now I have to do it in c #

 For i = 0 To dt.Rows.Count - 1
            Dim codigo As Integer = dt.Rows(i).Item("CodDepartamento")

            html.Append("<tr>")
            html.Append("<td>")
            html.Append(dt.Rows(i).Item("CodDepartamento"))
            html.Append("</td><td>")
    
asked by Elias Quintanilla 09.06.2017 в 08:08
source

3 answers

1

I understand that the variable dt is a DataTable and html a StringBuilder.

It would be something like this:

    for (int i = 0; i < dt.Rows.Count; i++)
    {
        var codigo = (int) dt.Rows[i]["CodDepartamento"];

        html.Append("<tr>");
        html.Append("<td>");
        html.Append(dt.Rows[i]["CodDepartamento"]);
        html.Append("</td><td>");
    
answered by 09.06.2017 / 08:14
source
0

You can do it in a fairly simple way:

foreach(DataRow dr in dt.Rows){
    int codigo = Convert.ToInt32(dr["CodDepartamento"]);

    html.Append("<tr><td>").Append(codigo).Append("</td><td>");
}

This assuming that dt is a DataTable and that html is a StringBuilder .

    
answered by 09.06.2017 в 16:19
0

More than a solution to your question is a piece of advice, there are several tools that have the purpose of converting the code from VB to C # and vice versa.

Personally I use this: link but there are several ..

It is possible that you need to convert code again then there you will have a tool that does it for you taking into account that you understand the logic to program. Returning to your question the code would be as follows:

for (int i = 0; i <= dt.Rows.Count - 1; i++) {
    int codigo = dt.Rows(i).Item("CodDepartamento");

    html.Append("<tr>");
    html.Append("<td>");
    html.Append(dt.Rows(i).Item("CodDepartamento"));
    html.Append("</td><td>");
}
    
answered by 12.06.2017 в 15:27