Click here to Skip to main content
16,020,519 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
There is a column named Quantity in the table. the quantity of available books in the inventory is stored here. When a book is de-assigned from a user the value in Quantity column should increase by 1. This de-assign is performed on a click of a button. How do I perform this?

What I have tried:

I have not tried anything. I thought of creating a procedure to do this. But I do not know how to call the procedure in button click..
Posted
Updated 8-Feb-17 0:06am
Comments
[no name] 8-Feb-17 6:06am    
You can use a stored procedure, or a SELECT and UPDATE from your code. What is the problem?
Pallavi 24 13-Feb-17 5:01am    
Conversion failed when converting the varchar value 'System.Windows.Forms.DataGridViewSelectedRowCollection' to data type int.
I get the above error when I use Update statement..
I used the below code,.....
cmd.CommandText = "Update IssueBooks set BookStatus='Returned' where IssueId='"+dataGridView1.SelectedRows+"' ";
I have used a grid view. Based on selection that the user does the table has to be updated..
[no name] 13-Feb-17 5:17am    
Firstly, do not use string concatenation to create SQL statements; use parameters. And secondly, look at the data types you are referring to, they must be the same.
Pallavi 24 8-Feb-17 23:47pm    
If I create a procedure then how will the procedure be called when the button is called ?

1 solution

Try:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("UPDATE myTable SET Quantity = Quantity + 1 WHERE BookID = @BI", con))
        {
        cmd.Parameters.AddWithValue("@BI", IdValueForTheRowToSayWhichBookItIs);
        cmd.ExecuteNonQuery();
        }
    }

[EDIT]Forgot the WHERE condition...[/EDIT]
 
Share this answer
 
v2
Comments
Pallavi 24 8-Feb-17 23:41pm    
The Quantity value should not exceed. I mean if Quantity has been entered as 10 by the librarian then after each increment Quantity value should not exceed 10.
10 is the limit, right.

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