How to subtract quantity from parent table and put that amount into child table in c# forms

Studnet_1999 1 Reputation point
2022-02-23T00:38:31.217+00:00

Hello guys I'm a student on 3th year of IT and I've found so much help from this forum, I am stuck with my project creating program in C# so I need your help I have 2 tables in my Data Base :

Materials Table wich has : idmaterial(INT) , name_material(VARCHAR), quantity_material(FLOAT), mesuring_unit(VARCHAR), code_material(INT)

Scraps Table (Scraps from Material) wich has : idscrap(INT), Materials_idmaterial(INT) .

They are connected with Non-Identifying Relation 1:1. Now I have a Combo Box in my C# Form presented down below. I need to take from table Materials (name_material , quantity_material , mesuring_unit , code_material) and subtract the quantity that I insert in the textBox on my Form. For example I have 120 kg of wool in my Material table and I want to put 2 kg to scrap table , how do I do it in C# ...
Pictures :

https://prnt.sc/hdvGuXJWNI0Q - My Diagram in mysql (It is on my language but I translated every cell name for you guys)
https://prnt.sc/lgIRVTCOe670 - Materials Panel
https://prnt.sc/t1IGxpgJ8GHl - Choosing material via combo box in scraps panel
https://prnt.sc/6InfCPpezZga - Example on how I want to subtract

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,891 questions
Azure Database for MySQL
Azure Database for MySQL
An Azure managed MySQL database service for app development and deployment.
827 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,930 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Jack J Jun 24,491 Reputation points Microsoft Vendor
    2022-02-24T09:07:01.253+00:00

    @Studnet_1999 , based on your description, I almost understand your requirement about your questions.

    Here is a code example you could refer to.

    public partial class Form1 : Form  
    {  
        public Form1()  
        {  
            InitializeComponent();  
        }  
        static string connstr = "str";  
        SqlConnection connection=new SqlConnection(connstr);  
    
        private void Form1_Load(object sender, EventArgs e)  
        {  
            loaddata();  
            connection.Open();  
            string sql = "select *from Materials";  
            SqlCommand sqlCommand = new SqlCommand(sql, connection);  
            SqlDataReader reader = sqlCommand.ExecuteReader();  
            while (reader.Read())  
            {  
                comboBox1.Items.Add(reader["name_material"].ToString());  
    
            }  
            connection.Close();  
            comboBox1.SelectedIndexChanged += ComboBox1_SelectedIndexChanged;  
        }  
    
        private void ComboBox1_SelectedIndexChanged(object sender, EventArgs e)  
        {  
            string value=comboBox1.SelectedItem.ToString();  
            connection.Open();  
            string sql =string.Format("select idmaterial from Materials where name_material='{0}'",value);  
    
            SqlCommand sqlCommand = new SqlCommand(sql, connection);  
            int idmaterial =(int) sqlCommand.ExecuteScalar();  
            sql = string.Format("select idscrap,Materials_idmaterial from Scraps where Materials_idmaterial={0}", idmaterial);  
            SqlDataAdapter adapter = new SqlDataAdapter(sql,connection);  
            DataSet ds = new DataSet();  
            adapter.Fill(ds);  
            dataGridView2.DataSource= ds.Tables[0];  
            connection.Close();  
    
        }  
    
        private void tabControl1_Click(object sender, EventArgs e)  
        {  
            if(tabControl1.SelectedTab.Name== "tabPage1")  
            {  
                loaddata();  
            }  
            if(tabControl1.SelectedTab.Name == "tabPage2")  
            {  
                textBox1.Clear();  
                comboBox1.Text = "";  
            }  
            
    
        }  
    
    
        public void loaddata()  
        {  
            connection.Open();  
            string sql = "select *from Materials";  
            DataSet ds = new DataSet();  
            SqlDataAdapter adapter = new SqlDataAdapter(sql, connection);  
            adapter.Fill(ds);  
            dataGridView1.DataSource = null;  
            dataGridView1.DataSource = ds.Tables[0];  
            connection.Close();  
        }  
    
        private void button4_Click(object sender, EventArgs e)  
        {  
            string value=comboBox1.SelectedItem.ToString();   
            int quantity=Convert.ToInt32(textBox1.Text);  
            connection.Open();  
            string sql = string.Format("select quantity_material from Materials where name_material='{0}'", value);  
            SqlCommand sqlCommand = new SqlCommand(sql, connection);  
            double getquantity =Convert.ToDouble(sqlCommand.ExecuteScalar());  
            double newquantity = getquantity - quantity;  
            sql = String.Format("update Materials set quantity_material={0} where name_material='{1}'", newquantity, value);  
            sqlCommand = new SqlCommand(sql,connection);  
            sqlCommand.ExecuteNonQuery();  
            Console.WriteLine(getquantity);  
            connection.Close();  
    
        }  
    }  
    

    Note: I don't have mysql database, I used sql database to replace it. You could make some changes based on my code.

    Here is my tested result:

    177472-6.gif

    Best Regards,
    Jack


    If the answer is the right solution, please click "Accept Answer" and upvote it.If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our [documentation][3] to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.