Click here to Skip to main content
15,885,546 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
My table has following field
BranchId Productname Stock

If the table has Given branchid,productname I want update Stock field for particular row

Else I want Insert the Row

How to do it


Please help me
Posted
Comments
Oshtri Deka 27-Mar-12 6:00am    
Why is this tagged with C# tag?
[no name] 28-Mar-12 11:11am    
are you using any Stored proc or any ORM tool ?

Try working it out with this:
SQL
IF EXISTS(SELECT * FROM YourTable WHERE BranchId = @branchID AND  Productname = @productName)
BEGIN
    UPDATE YourTable
    SET Stock = @stock
    WHERE BranchId = @branchID AND  Productname = @productName
END
ELSE
BEGIN
    INSERT INTO YourTable
    VALUES(@branchID, @productName, @stock)
END


C# code can be something like this:
C#
SqlCommand cmd = yourConnection.CreateCommand();
cmd.CommandText = queryFromAbove;
cmd.Parameters.AddWithValue("@branchID", branchid);
cmd.Parameters.AddWithValue("@productName", productName);
cmd.Parameters.AddWithValue("@stock", stock);

try
{
   if(yourConnection.State != ConnectionState.Open)
   {
        yourConnection.Open()
   } 
   cmd.ExecuteNonQuery(); 
}
catch(SqlException sqlex)
{
    //handle your sql exception here
}
catch(exception ex)
{
    //handle your other exception here
}
finally
{
    cmd.Dispose();
}
 
Share this answer
 
v2
Comments
Amir Mahfoozi 27-Mar-12 6:00am    
+5
Oshtri Deka 27-Mar-12 6:09am    
Edit: I've added C# code.
[no name] 28-Mar-12 11:10am    
good once, my +5
You would want to do an upsert, but it doesn't exist in sqlserver. But have a look here: http://www.sergeyv.com/blog/archive/2010/09/10/sql-server-upsert-equivalent.aspx[^]

Good luck!
 
Share this answer
 
you can write following query for inserting new row:


VB
ConnObj.Open()
        Dim mycommand As SqlCommand = New SqlCommand
        mycommand.Connection = ConnObj
        mycommand.CommandText = "insert into TableName" & " (branchId, productName) " & " VALUES (@branchId, @productName)"

        mycommand.Parameters.AddWithValue("@branchId", textBox1.Text)
        mycommand.Parameters.AddWithValue("@productName", textBox2.Text)
        mycommand.ExecuteNonQuery()
ConnObj.Close()



or if you want to update any single row than you can do the following:


VB
Dim myconn As New SqlConnection()
        myconn = New SqlConnection(ConnectionString)
        Dim mycom As SqlCommand = New SqlCommand()
        mycom.Connection = myconn
        myconn.Open()
        mycom.CommandText = "update TableName set productName= @productName where branchId='" + textbox1.Text + "'"
        mycom.Parameters.AddWithValue("@productName", textbox2.Text)
        mycom.ExecuteNonQuery()
        myconn.Close()




hope it will help you.
 
Share this answer
 
v2
Use Merge statment...Its easy and do this for u

Regards
 
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