How to add values every time when entering data to the SQL database with C # and win forms

1

How to add values every time I insert the database and remember the Total values of previous inserts. I want to add field Name in Total field. And when I restart the application I want you to remember the last Total.

This is my class clans code ..

public class clans

    public string Name{ get; set; }
    public string Craft { get; set; }
    public int Month { get; set; }
    public int Year { get; set; }
    public decimal Price { get; set; }   
    public decimal Total {get; set; }

This code is to insert in SQL Table obb:

        cl.Name = txtInsertName.Text;
        cl.Craft = txtInsertCraft.Text;
        cl.Month = Convert.ToInt32(txtInsertMounth.Text);
        cl.Year = Convert.ToInt32(txtInsertYear.Text);
        cl.Price = Convert.ToDecimal(txtInsertPrice.Text);

      cl.Total += cl.Price;//en esta manera hago suma pero cuando reinicio la aplicacion mo me recuerda el ultimo Total

        SqlCommand cmd;

            string query = "insert into obb(Name,Craft,Month,Year,Price,Total) values(@p1,@p2,@p3,@p5,@p6,@p7)";
        cmd = new SqlCommand(query, db);
        cmd.Parameters.AddWithValue("@p1", cl.Name);
        cmd.Parameters.AddWithValue("@p2", cl.Craft);
        cmd.Parameters.AddWithValue("@p3", cl.Month);
        cmd.Parameters.AddWithValue("@p5", cl.Year);
        cmd.Parameters.AddWithValue("@p6", cl.Price);
        cmd.Parameters.AddWithValue("@p7", cl.Total);

        cmd.CommandType = CommandType.Text;
        int i = cmd.ExecuteNonQuery();
        //Los codigos para datagridview
        string query1 = @" SELECT * FROM obb order by Name ASC, Year ASC";
        clansBindingSource1.DataSource = db.Query<clans>(query1, commandType: CommandType.Text);
    
asked by user85747 05.05.2018 в 16:57
source

1 answer

1

There may be many ways to do this, but here I show you a way to implement it, regardless of the code you have done, I will only show you how to save the value and recover it when you load the application again, it is something like this:

1- First in your Visual Studio of Click in the menu in Proyecto (Project) and then go down to Propiedades del Proyecto (Properties) :

2- Second go to the section Configuración (Settings) and create your variable, Nombre , TipoDato etc .:

3- To save your Total in this variable, do the following in the event of Form FormClosed or FormClosing you want:

Properties.Settings.Default.Total_Nombres = cl.Total;
Properties.Settings.Default.Save();

4- To recover this value when starting the application, you must do the following in the event Load of the form:

cl.Total = Properties.Settings.Default.Total_Nombres;

This would be everything, as I said there are more ways to do it, this is the one that suddenly occurs to me, I hope it will help you.

    
answered by 05.05.2018 в 17:36