locked
Convert DataTable Value to INT and add the numbers RRS feed

  • Question

  • User296452190 posted

    Hi all,

    I need held with the following, I have a table that gets some record from the DB through a DataTable, I included in the proyect a DropDownList to specify the information to be shown in my table in a particular column depending on the date selected in the DDL, Now my issue is this, I haven't sound a way to recover the records after DDL is selected because I haven't been able to change the data type to INT  nor String, and I guess its the same reason it doesn't allow me to add all the results,

    This is my code

                    if (ddlMontoPorMes.SelectedIndex > 0)
                    {
    
                        foreach (DataRow row in dt.Rows)
                        {
    
                            foreach (DataColumn MONTOREC in dt.Columns)
                            {
                                foreach (DataRow dtRow in dt.Rows)
                                {
                                    object fielsd = dtRow["NROACCRUAL"];
                                    //Here I search for the record
                                    DataTable dt1 = Helper_wsWillisBudget.SumaRecibosPorMes(fielsd.ToString(), ddlMontoPorMes.SelectedValue);
                                    //Here I want to add the numbers and assign it to the column
                                    int suma = 0;
                                    suma += dt1;
                                    row.SetField("MONTOREC", dt1);
                                }
                                
                                
                            }
                        }
    
                    }

    Thanks beforehand

    Regards
     

    Monday, March 27, 2017 6:31 PM

All replies

  • User-1509636757 posted

    What I understand is, you need to have total of a particular column inside a DataTable and assign Total to a field in another DataTable Row. If this is correct, then you just need to loop thru DataTable rows to get total, here is somewhat similar code on the scenario:

    if (ddlMontoPorMes.SelectedIndex > 0)
    {
        foreach (DataRow row in dt.Rows)
        {
            foreach (DataColumn MONTOREC in dt.Columns)
            {
                foreach (DataRow dtRow in dt.Rows)
                {
                    object fielsd = dtRow["NROACCRUAL"];
                    //Here I search for the record
                    DataTable dt1 = Helper_wsWillisBudget.SumaRecibosPorMes(fielsd.ToString(), ddlMontoPorMes.SelectedValue);
                    //Here I want to add the numbers and assign it to the column
                    int suma = 0;
                    foreach (DataRow rowItem in dt1.Rows)
                    {
                        int tempVal = 0;
                        if (int.TryParse(rowItem["DataColumnNameToSum"], out tempVal))
                            suma += tempVal;
                    }
                    row.SetField("MONTOREC", suma);
                }
            }
        }
    }

    Tuesday, March 28, 2017 7:28 AM
  • User296452190 posted

    Exactly, for example the column I'm saving in the DataTable ("DataTable dt1 = Helper_wsWillisBudget.SumaRecibosPorMes(fielsd.ToString(), ddlMontoPorMes.SelectedValue);") could contain 3 records like 5, 6,7 so I want to store the sum in my "Suma" variable, meaning that suma = 18.

    The result I am getting actually is 0, using the code you sent me.

    int suma = 0; foreach (DataRow rowItem in dt1.Rows) { int tempVal = 0; if (int.TryParse(rowItem["DataColumnNameToSum"], out tempVal)) suma += tempVal; } row.SetField("MONTOREC", suma);

    What would I have to change?

    Tuesday, March 28, 2017 2:01 PM
  • User-1509636757 posted

    Make sure there are records in dt1 DataTable. Also make sure that you are referring to correct DataColumn here: int.TryParse(rowItem["DataColumnNameToSum"], out tempVal). Try debugging code and see what value you are getting in tempVal for each row in foreach loop. You may post your code here if you still face issue.

    Wednesday, March 29, 2017 6:10 AM
  • User296452190 posted

    I've made sure of that, I got it working now, Idid this

                    if (ddlMontoPorMes.SelectedIndex > 0)
                    {
                        foreach (DataRow row in dt.Rows)
                        {
                            int suma = 0;
                            object fielsd = row["NROACCRUAL"];
                            DataTable dt1 = Helper_wsWillisBudget.SumaRecibosPorMes(fielsd.ToString(), ddlMontoPorMes.SelectedValue);
    
                                    foreach (DataRow rowItem in dt1.Rows)
                                    {
                                        object sumar = rowItem["TComis"];
                                        suma += Convert.ToInt32(sumar);
                                    }
                                        row.SetField("MONTOREC", suma);
                        }
                    }

    I really didn't understand what you where doing here with the out tempVal because when debugging it was always 0

    int.TryParse(rowItem["DataColumnNameToSum"], out tempVal).

    Even though I got it working its that's to your idea and I would like to understand what you were doing in that line of the code.

    Regards

    Wednesday, March 29, 2017 12:31 PM
  • User-1509636757 posted

    Even though I got it working its that's to your idea and I would like to understand what you were doing in that line of the code.

    That is just a safe way of conversion. Int.TryParse converts the string representation of a number to its 32-bit signed integer equivalent. A return value indicates whether the conversion succeeded or not.

    Wednesday, March 29, 2017 12:37 PM