Click here to Skip to main content
15,891,184 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have table1 contains 2 columns : itmname - itmcode

all itmname values are shown in listview1 column0

and textbox1 where user input the number he want

I want what user to input a number in textbox1 and data be added not sum to the value that's already in itmcode on table1 where itmname = listview1 column0
and if no data in itmcode just insert it

for all items in listview1

I tried this approach but didn't work :

C++
if (cn.State == ConnectionState.Closed) cn.Open();
            cm.Connection = cn;
            string sql = "UPDATE table1 SET [itmcode] = [itmcode] + @itmcode WHERE [itmname] = @itmname AND [itmname] IS NOT NULL";
            cm.CommandText = sql;
            foreach (ListViewItem item in listView1.Items)
            {
                cm.Parameters.Clear();
                SqlParameter par_itmcode = new SqlParameter("@itmcode", SqlDbType.Int);
                par_itmcode.Value = textBox1.Text;
                cm.Parameters.Add(par_itmcode);

                SqlParameter par_itmname = new SqlParameter("@itmname", SqlDbType.VarChar);
                par_itmname.Value = item.Text;
                cm.Parameters.Add(par_itmname);
                {
                    cm.ExecuteNonQuery();
                }
            }
            cn.Close();
            MessageBox.Show("done");


C#
so it would look like this


 itmname    itmcode

  name1        3

 if user enter 4 in textbox1 then it give me this result :

 itmname    itmcode

   name1       34
Posted
Updated 1-Jan-16 1:19am
v2
Comments
_Asif_ 1-Jan-16 6:58am    
what error are you getting ? what is the datatype of itemcode field?
Ahmed Zoeil 1-Jan-16 7:06am    
no error just itmcode never changed

I would write a stored procedure for this: check if the row exists, and if so UPDATE it. If it doesn't, do an INSERT instead:
SQL
CREATE PROCEDURE spInsertOrUpdate
    @ItemCode INT,
    @ItemName NVARCHAR
AS
BEGIN
UPDATE MyTable SET ItemCode = ItemCode + @ItemCode WHERE ItemName = @ItemName
IF @@ROWCOUNT=0
    INSERT INTO MyTable (ItemCode, ItemName) VALUES (@ItemCode, @ItemName)
END
 
Share this answer
 
Comments
Ahmed Zoeil 1-Jan-16 7:10am    
thank you for your reply but I don't want to insert itmname if itmcode is null I want itmcode only to be inserted if it's value is null and if there's value in itmcode add it next to it

so it would look like this


itmname itmcode

name1 3

if user enter 4 in textbox1 then it give me this result :

itmname itmcode

name1 34
OriginalGriff 1-Jan-16 7:22am    
You can do it with a less efficient SELECT:

IF EXISTS (SELECT * FROM MyTable WHERE @ItemName IS NOT NULL AND ItemName = @ItemName
UPDATE MyTable SET ItemCode = ItemCode + @ItemCode WHERE ItemName = @ItemName
ELSE
INSERT INTO MyTable (ItemCode, ItemName) VALUES (@ItemCode, @ItemName)

As the body of your SP - but I'd do the null check in my C# code instead myself and not bother sending nulls to SQL. Again, it's more efficient that way.
SQL
--Here I am assuming the ItemName column is varchar in your table

Create procedure spInsertName
(
@ItemCode int,
@ItemName varchar (100)
)


If Exists(Select ItemCode from MyTable where ItemCode == @ItemCode)
BEGIN
Update MyTable set ItemName = ItemName + @ItemName
Where ItemCode == @ItemCode
END
Else
BEGIN
Insert into MyTable(ItemName, ItemCode) values(@ItemName, @ItemCode)
END
 
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