Skip to content

Doubt in adding data in database in c#

raj leathu

Asked Feb 14, 2014, 7:26 PM EST1 answer
Share
LinkedInX
Hi... I am new in C#. I am doing a billing project. I have one doubt. In my form i have text box. for sales entry. when i enter in that text box it have to be added to my database in the field of sales. For example in my database for an item pen it already have sales value 5 in database. if i enter again 5 in text box my database value should be 10. I don't know what to do... so please help me

1 Answer

Know the answer? Post it. Somebody with the same question will find it here.

  • Biswa Pujarini Mohapatra
    0
    <%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="WebApplication1.WebForm1" %>

    <!DOCTYPE html>

       
       
       
           
       
       

    ASpx.cs

    C#
     protected void Button1_Click(object sender, EventArgs e)
            {
                string connectionString = "Persist Security Info=False;User ID=sa;Password=123;Initial Catalog=SalesDB";//TODO change your connection string
    
    
                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    string SqlQuery ="";
                    SqlCommand cmd = new SqlCommand("Insert into table (sale) values(SUM(@sale))", con);//To DO change your table and columna name
                   con.Open();
    
    
                if (con.State == ConnectionState.Open)
                {
                    cmd.Parameters.Add("@sale", SqlDbType.Int).Value = txtSales.Text;
    
    
    
                    try
                    {
                        cmd.ExecuteNonQuery();
    
                        con.Close();
                    }
                    catch(SqlException ex)
                    {
                    throw ex;
                    }
                }
    
    
            }

    0