Click here to Skip to main content
15,879,613 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Given code is not working.Table(AddedItem) is not uppdated

C#
protected void Button1_Click(object sender, EventArgs e)
        {
            int st = 0,st1=0;
            if (IsPostBack)
            {
                try
                {
                    con.Open();
                   
                    st = Convert.ToInt32(TextBox1.Text);
                    SqlCommand cmd2 = new SqlCommand("SELECT Quantity FROM AddedItem where Item_Name='" + DropDownList3.SelectedItem.Text + "'", con);
                    SqlDataReader dr = cmd2.ExecuteReader(CommandBehavior.SingleRow);
                    if (dr.Read())
                    {

                        st1 = Convert.ToInt32(dr.GetValue(0).ToString());
                        dr.Close();
                    }
                    st1 = st1 - st;
                    SqlCommand cmd1 = new SqlCommand("UPDATE AddedItem SET Quantity=st1", con);
                    cmd1.ExecuteNonQuery();
                   
                   
                }
                catch (SqlException ex)
                {

                }
                finally
                {
                    con.Close();

                }
}
}
Posted
Updated 15-Nov-11 22:37pm
v2

Instead of this

st1 = st1 - st;
                    SqlCommand cmd1 = new SqlCommand("UPDATE AddedItem SET Quantity=st1", con);



Write

st1 = st1 - st;
                    SqlCommand cmd1 = new SqlCommand("UPDATE AddedItem SET Quantity="+st1, con);
 
Share this answer
 
Comments
ali_heidari_ 2-Oct-12 5:18am    
well i did your solution but it didnt work at all ... in my application i cant use variable for update my column :s , dnt you know why i cant use variable?
deepakdynamite 5-Oct-12 0:42am    
Try with casting your variable with .toString() for Eg. st1.Tostring()
C#
SqlCommand cmd1 = new SqlCommand("UPDATE AddedItem SET Quantity = " + st1, con);


[Edit] added pre tags
 
Share this answer
 
v2
Hi,

Here is your code:
C#
SqlCommand cmd1 = new SqlCommand("UPDATE AddedItem SET Quantity=st1", con);


But it should be
C#
SqlCommand cmd1 = new SqlCommand("UPDATE AddedItem SET Quantity="+st1, con);


Hope this helps

[Edit] added pre tags and removed superfluous line breaks
 
Share this answer
 
v2
Why don't you combine your code into one sql statement

C#
// logic required to ensure it is actually an int - look up the int try parse method
st = Convert.ToInt32(TextBox1.Text);

SqlCommand cmd1 = new SqlCommand("UPDATE AddedItem SET Quantity = Quantity - " + st + " where Item_Name='" + DropDownList3.SelectedItem.Text + "'" ;

cmd1.ExecuteNonQuery();


You should also consider converting your query into a stored procedure to improve performance and prevent injection attacks
 
Share this answer
 
try in the following way :
C#
st1 = st1 - st;
 SqlCommand cmd1 = new SqlCommand("update AddedItem set Quantity="+st1, con);
 
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