Click here to Skip to main content
15,893,190 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
C#
protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("Data Source=SHANTANU\\SQLEXPRESS;Initial Catalog=Inventry;Integrated Security=True");
    SqlDataAdapter myadp = new SqlDataAdapter();

    myadp.UpdateCommand = new SqlCommand();
    myadp.UpdateCommand.Connection = con;

    con.Open();

    myadp.UpdateCommand.CommandText = "update purchase set quantity= quantity-'"+(qnt1.Text+qnt2.Text)+"' where item='"+(item1.Text+item2.Text)+"'";

    int i = myadp.UpdateCommand.ExecuteNonQuery();
    if (i ==1)
    {
        Response.Write("<script>alert('updated')</script>");
    }
    else
    {
        Response.Write("<script>alert('Failed')</script>");
    }

    con.Close();
}
Posted
Updated 2-Jan-14 2:43am
v3
Comments
Kornfeld Eliyahu Peter 2-Jan-14 8:41am    
What the message is?
Shantanu sinha 2-Jan-14 8:56am    
actually i have made a table with name "item" and "quantity" i want to update the database when i am selling the product where the query will search the item from the "itm.text" (textbox) and will update (it will minus) the present quantity by the number in the qnt.text(textbox).
it is working fine for 1 item but if i m using it for more than 1 product it is not getting updated. rather i am getting the error massage "failed" which i have put in last line of the code.
Thomas Daniels 2-Jan-14 8:42am    
What's the error message?
Shantanu sinha 2-Jan-14 8:49am    
the value is not getting subtracted
Thomas Daniels 2-Jan-14 8:52am    
At which line do you get the error?

Hi Shantanu,

If you use ExecuteNonQuery(), then

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command.

So I think the number of rows updated may be greater than 1 so its error in your condition.
Try this
C#
if (i > -1)
           {
               Response.Write("<script>alert('updated')</script>");
           }
           else
           {
               Response.Write("<script>alert('Failed')</script>");
           }


Updated solution

I hope this is something wrong with concatenation,So try this
SQL
"quantity= quantity-'"+(Convert.ToInt32(qnt1.Text)+Convert.ToInt32(qnt2.Text))+"' where item='"+(Convert.ToInt32(item1.Text)+Convert.ToInt32(item2.Text))+"'";

Hope this helps you a bit.

Regards,
RK
 
Share this answer
 
v2
Comments
Shantanu sinha 2-Jan-14 9:02am    
not working boss
i m getting the msg "updated" but the values are not getting updated,
txs
where item='"+(item1.Text+item2.Text)+"'
That looks very strange. I would not expect the identifier of the item to be split over two textboxes.
Do you want to update two rows (i.e. the data for two items)? Then send two individual update requests, in one transaction.
 
Share this answer
 
Comments
Shantanu sinha 2-Jan-14 9:03am    
how can i do that
Shantanu sinha 2-Jan-14 9:04am    
it means that i have to call two update commands. i tried that too but the value which was at last command got updated.
Bernhard Hiller 2-Jan-14 9:14am    
myadp.UpdateCommand.CommandText = "update purchase set quantity= quantity-'"+qnt1.Text+"' where item='"+item1.Text+"'";
int i = myadp.UpdateCommand.ExecuteNonQuery();
myadp.UpdateCommand.CommandText = "update purchase set quantity= quantity-'"+qnt2.Text+"' where item='"+item2.Text+"'";
i += myadp.UpdateCommand.ExecuteNonQuery();
if (i == 2) ...
Shantanu sinha 2-Jan-14 9:30am    
int i+= myadp.UpdateCommand.ExecuteNonQuery(); here i am getting error
Error 1 Invalid expression term '+=' C:\the who software\WebSite2\sale.aspx.cs 37 18 C:\the who software\WebSite2\
Shantanu sinha 4-Jan-14 21:52pm    
it helped little and finally i made my logic.really txs a lot
try this


C#
int value = Convert.ToInt32( qnt1.Text) + Convert.ToInt32( qnt2.Text);
           int item = Convert.ToInt32( item1.Text) + Convert.ToInt32( item2.Text);
           myadp.UpdateCommand.CommandText = "update purchase set quantity= quantity-"+ value+" where item='"+ item+"'";
 
Share this answer
 
Comments
Shantanu sinha 2-Jan-14 10:04am    
sir ji i dont have to add the values..
C#
protected void Button1_Click(object sender, EventArgs e)
{
    SqlConnection con = new SqlConnection("Data Source=SHANTANU\\SQLEXPRESS;Initial Catalog=Inventry;Integrated Security=True");
    SqlDataAdapter myadp = new SqlDataAdapter();
      myadp.UpdateCommand = new SqlCommand();
      myadp.UpdateCommand.Connection = con;
      con.Open();
      myadp.UpdateCommand.CommandText = "update purchase set quantity= quantity-'"+qnt1.Text+"' where item='"+item1.Text+"'";
      int i = myadp.UpdateCommand.ExecuteNonQuery();
        if (i==1)
        {
            Response.Write("<script>alert('updated')</script>");
        }
        else
        {
            Response.Write("<script>alert('Failed')</script>");
        }
        con.Close();
        con.Open();
        myadp.UpdateCommand.CommandText = "update purchase set quantity= quantity-'" + qnt2.Text + "' where item='" + item2.Text+ "'";
        int j = myadp.UpdateCommand.ExecuteNonQuery();
        if (j == 1)
        {
           // Response.Write("<script>alert('updated')</script>");
        }
        else
        {
            Response.Write("<script>alert('Failed')</script>");
        }
        con.Close();
        con.Open();
        myadp.UpdateCommand.CommandText = "update purchase set quantity= quantity-'" + qnt3.Text + "' where item='" + item3.Text + "'";
        int k = myadp.UpdateCommand.ExecuteNonQuery();
        if (k == 1)
        {
           // Response.Write("<script>alert('updated')</script>");
        }
        else
        {
           // Response.Write("<script>alert('Failed')</script>");
        }
        con.Close();
        con.Open();
        myadp.UpdateCommand.CommandText = "update purchase set quantity= quantity-'" + qnt4.Text + "' where item='" + item4.Text + "'";
        int l = myadp.UpdateCommand.ExecuteNonQuery();
        if (l == 1)
        {
          //  Response.Write("<script>alert('updated')</script>");
        }
        else
        {
          //  Response.Write("<script>alert('Failed')</script>");
        }
        con.Close();
        con.Open();
        myadp.UpdateCommand.CommandText = "update purchase set quantity= quantity-'" + qnt5.Text + "' where item='" + item5.Text + "'";
        int m = myadp.UpdateCommand.ExecuteNonQuery();
        if (m == 1)
        {
            Response.Redirect("sale.aspx");
            //Response.Write("<script>alert('updated')</script>");
        }
        else
        {
           // Response.Write("<script>alert('Failed')</script>");
        }
        con.Close();
        Response.Redirect("sale.aspx");



code block corrected
 
Share this answer
 
v3
Comments
CHill60 2-Jan-14 10:55am    
what??
♥…ЯҠ…♥ 2-Jan-14 23:05pm    
Your working solution?
Shantanu sinha 2-Jan-14 23:14pm    
yes
i m getting the desired result

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