Click here to Skip to main content
15,891,529 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear all

i have problem with this bit of coding


C#
dr["DataColumn1"] += dtshort.Rows[0][0];


when i pass value to dtshort.Rows[0][0]=1, it ll store dr["DataColumn1"] it fine but my prob is when i am using loop i want continuously add value to dr["DataColumn1"]

How to do this,...
Posted
Updated 7-Dec-14 22:34pm
v2

1. The values get from the data source by using DataReader are in fact boxed objects and they should be unboxed to their original types like in the next example:
C#
string name = (string)dr["DataColumn1"];
double number = (double)dr["DataColumn2"];


2.For more details about boxing and unboxing see in MSDN:http://msdn.microsoft.com/en-us/library/yz2be5wk.aspx[^]
 
Share this answer
 
v2
Comments
prasanna.raj 8-Dec-14 4:52am    
Thanks for replay and my query is i want add values to dr["DataColumn1"]
for example

dr["DataColumn1"] += dtshort.Rows[0][0] this dtshort.Rows[0][0] returns value 1,2,3, so how to add this value(6) to dr["DataColumn1"]
Raul Iloc 8-Dec-14 6:31am    
You should use a temp value, of type "int", in your loop to accumulate the sum of all your rows, then to save the final result into your "dr" object.
dr["DataColumn1"] can handle different types of data, to do that its type set to object (after all everything is an object).
However object do not implement the operator of += as this is a common type and += operator can be very different for different actual types (imagine 1+1 and '1'+'1')...
What you can do is convert dr["DataColumn1"] to it's expected type and than apply += operator:
C#
int n = Convert.ToInt32(dr["DataColumn1"]);
dr["DataColumn1"] = n + Convert.ToInt32(dtshort.Rows[0][0]);
 
Share this answer
 
v2
Comments
prasanna.raj 8-Dec-14 4:57am    
Hi, i got this error ! The left-hand side of an assignment must be a variable, property or indexer
Kornfeld Eliyahu Peter 8-Dec-14 5:01am    
Updated...
prasanna.raj 8-Dec-14 7:57am    
Thanks i got it...
Kornfeld Eliyahu Peter 8-Dec-14 8:01am    
You are welcome!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900