Click here to Skip to main content
15,909,205 members
Please Sign up or sign in to vote.
1.00/5 (3 votes)
See more:
hi my friends.
in my table of database , i have field that it name is count.
in my program , i have a button.
when i click button , i want reduce one number from field [count] in database.
for example , value of field [count] is 5.
i want value become 4 when i click button.

how can i do that?
Posted
Comments
goathik 11-Jun-14 10:00am    
There are some things we need to know.
- ASP.Net or Windows Application?
- Linq or ADO.Net?
- On your table, do you only have always one row with that [Count] column, or more than one row?
- If more than one row, what is the WHERE condition?
Mohammad Hasanpoor 11-Jun-14 10:05am    
in windows application and ado.net.
i have two column. [id] and [count]
condition is my textbox=[id]
then reduce that
TheUnknownCoder 11-Jun-14 10:51am    
you have to execute a UPDATE command (update tablename set fieldname = value where id = idoftherecordtoupdate)


SQL
Update MyTable 
Set MyCount = (MyCount - 1)
WHERE MyTableID = 23
 
Share this answer
 
Missng a few details, but presume you mean in sql it could be

SQL
update mytable
set [count] = [count] - 1


but that of cause is a bit odd to use a table for, because a table with just one row?
other wise there must be a key column as well to define the row in your table where there column [count] should be reduced.

if from C# instead, because of the tag on the question you could issue whatever command you want from system.data.sqlclient namespace

C#
public static void DecrementMyTable()
{
    using (var cn = new System.Data.SqlClient.SqlConnection("myconnectionstring"))
    {
       using (var cmd = new System.Data.SqlClient.SqlCommand("my update query"), cn)
         {
              md.ExecuteNonQuery();
         }
     }
}


You could then call a method like the outlined one on your button 'onclick' event, presuming it has runat set to 'server'
 
Share this answer
 
v2
Comments
Mohammad Hasanpoor 11-Jun-14 10:12am    
sorry .
i have two column. [id] and [count]
condition is my textbox=[id]
then reduce that
click twice on your button and it will send you to .cs file with a blank event. Paste this code into that event:

C#
            try
            { 
                SqlConnection con = new SqlConnection("yourconnectionstringhere");
                con.Open(); 

                SqlCommand cmd = new SqlCommand("UPDATE [nameofthetable] SET [Count] = [Count] - 1 WHERE ID = @ID", con);
//EDIT: protects against sql injection
                cmd.Parameters.Add(new SqlParameter("@ID", txtIDFrom.Text));
                cmd.ExecuteNonQuery(); 
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally
            {
                con.Close();
            }
 
Share this answer
 
v4
Comments
goathik 11-Jun-14 10:24am    
let me know if you need to know how to build your connection string

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