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);
}
}
}
}
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);
}
}
}
}
नमस्ते,
[KaushaL] BlogTwitter [MS MVP 2008 & 2009] [MCC 2011] [MVP Reconnect 2017]
Don't forget to click "Mark as Answer" on the post that helped you
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.
kaushalparik27
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);
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.
नमस्ते,
[KaushaL] BlogTwitter [MS MVP 2008 & 2009] [MCC 2011] [MVP Reconnect 2017]
Don't forget to click "Mark as Answer" on the post that helped you
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.
नमस्ते,
[KaushaL] BlogTwitter [MS MVP 2008 & 2009] [MCC 2011] [MVP Reconnect 2017]
Don't forget to click "Mark as Answer" on the post that helped you
Member
2 Points
11 Posts
Convert DataTable Value to INT and add the numbers
Mar 27, 2017 06:31 PM|hanspam|LINK
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
Thanks beforehand
Regards
All-Star
31362 Points
7055 Posts
Re: Convert DataTable Value to INT and add the numbers
Mar 28, 2017 07:28 AM|kaushalparik27|LINK
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:
[KaushaL] Blog Twitter [MS MVP 2008 & 2009] [MCC 2011] [MVP Reconnect 2017]
Don't forget to click "Mark as Answer" on the post that helped you
Member
2 Points
11 Posts
Re: Convert DataTable Value to INT and add the numbers
Mar 28, 2017 02:01 PM|hanspam|LINK
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.
What would I have to change?
All-Star
31362 Points
7055 Posts
Re: Convert DataTable Value to INT and add the numbers
Mar 29, 2017 06:10 AM|kaushalparik27|LINK
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.
[KaushaL] Blog Twitter [MS MVP 2008 & 2009] [MCC 2011] [MVP Reconnect 2017]
Don't forget to click "Mark as Answer" on the post that helped you
Member
2 Points
11 Posts
Re: Convert DataTable Value to INT and add the numbers
Mar 29, 2017 12:31 PM|hanspam|LINK
I've made sure of that, I got it working now, Idid this
I really didn't understand what you where doing here with the out tempVal because when debugging it was always 0
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
All-Star
31362 Points
7055 Posts
Re: Convert DataTable Value to INT and add the numbers
Mar 29, 2017 12:37 PM|kaushalparik27|LINK
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.
[KaushaL] Blog Twitter [MS MVP 2008 & 2009] [MCC 2011] [MVP Reconnect 2017]
Don't forget to click "Mark as Answer" on the post that helped you