Click here to Skip to main content
15,911,785 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I try to sum each item in listview to table but I always get empty cells

What I have tried:

C#
string sql = "UPDATE itmscat1 SET [itmcount] = [itmcount] + @itmcount WHERE [itmcode] = @itmcode";
                cm.CommandText = sql;
                foreach (ListViewItem item in listView1.Items)
                {
                    cm.Parameters.Clear();
                    cm.Parameters.Clear();
                    SqlParameter par_ItmCount = new SqlParameter("@itmcount", SqlDbType.Int);
                    par_ItmCount.Value = item.SubItems[4].Text;
                    cm.Parameters.Add(par_ItmCount);

                    SqlParameter par_ItmCode = new SqlParameter("@itmcode", SqlDbType.Int);
                    par_ItmCode.Value = item.SubItems[1].Text;
                    cm.Parameters.Add(par_ItmCode);
                    {
                        cm.ExecuteNonQuery();
                    }

                }
Posted
Updated 15-Aug-16 9:39am
v2
Comments
ZurdoDev 15-Aug-16 15:21pm    
And the question is?
medo0- 15-Aug-16 15:33pm    
it's not working I always get empty cells instead of the sum of the values
Karthik_Mahalingam 16-Aug-16 2:35am    
better update all at once

1 solution

Your parameters are set as int yet you are passing string values. Make sure the object type are the same (int).

convert the list items to int.

ie
C#
par_ItmCode.Value = Convert.ToInt16(Item.SubItems[4].Text);
 
Share this answer
 

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