Click here to Skip to main content
16,020,459 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
The user will input a value for Quantity in textbox. This value I want to add with the value present in Quantity column in the database. How I perform it ?

What I have tried:

cmd.CommandText = "update Books set Quantity=Quantity + txtUpdateQuantity.Text where Name='"+txtBookName.Text+"' and Author='"+txtBookAuthor.Text+"';";
Posted
Updated 1-Mar-17 23:11pm

Not like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
First, parse the numeric values to number variables:
C#
int qty;
if (!int.TryParse(txtUpdateQuantity.Text, out qty))
    {
    ... report a problem to the user ...
    return;
    }

Then try something like this:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("UPDATE Books SET Quantity = Quantity + @QT WHERE [Name] = @NM AND Author = @AU", con))
        {
        cmd.Parameters.AddWithValue("@QT", qty);
        cmd.Parameters.AddWithValue("@NM", txtBookName.Text);
        cmd.Parameters.AddWithValue("@AU", txtBookAuthor.Text);
        cmd.ExecuteNonQuery();
        }
    }
 
Share this answer
 
Comments
Pallavi 24 3-Mar-17 9:10am    
The ConnectionString property has not been initialized.
I am getting this error.
OriginalGriff 3-Mar-17 9:11am    
And what did you put in strConnect?
Pallavi 24 3-Mar-17 9:22am    
Thank you so much. I made changes and it is working now perfectly.
OriginalGriff 3-Mar-17 9:55am    
You're welcome!
Pallavi 24 3-Mar-17 9:52am    
I did not use connection string.Actually modified ur code and I used following code:
SqlCommand cmd= con.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText="UPDATE Books SET Quantity = Quantity + @QT WHERE Name = @NM AND Author = @AU";
cmd.Parameters.AddWithValue("@QT", qty);
cmd.Parameters.AddWithValue("@NM", txtBookName.Text);
cmd.Parameters.AddWithValue("@AU", txtBookAuthor.Text);
cmd.ExecuteNonQuery();
MessageBox.Show("Updated successfully");
con.Close();
string qty = txtUpdateQuantity.Text;
           string book = txtBookName.Text;
           string author = txtBookAuthor.Text;
           SqlCommand cmd = new SqlCommand();
           cmd.Connection = con;
           cmd.CommandText = "update Books set Quantity=Quantity + @qty  where Name=@book and Author=@author ";
           cmd.Parameters.AddWithValue("@qty", qty);
           cmd.Parameters.AddWithValue("@book", book);
           cmd.Parameters.AddWithValue("@author", author);
           con.Open();
           cmd.ExecuteNonQuery();



Formatting the sql Query string is vulnerable to SQL Injection[^] attacks
always use Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]
 
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