Relationship in XAMPP between two tables and inserting data into each in c#
<p>I have a question that I can't seem to figure out. I'm creating an application that adds a client's information then relate it to a service that is being provided. I created the relationship between the two tables in xampp database. The tables are tclientinfo and tserviceinfo. The columns in the tclientinfo table are Client_ID(PK), First_Name, Last_Name, Phone, and Email. In the tserviceinfo, Service_Order, Date, Tech, Brand, Model, OS, Type, PC_Issue, Service, Service_Price, Total_Price, Service_Notes, Client_ID. Client_ID is what I'm using as the relationship, but I am unable to add information to the tserviceinfo table. I'm getting the SQL exception: Cannont add or update a child row: a foreign key constraint fails('dbocomputerinformation'.'tserviceinfo',CONSTRAINT 'tserviceinfo_ibfk_3' FOREIGN KEY('Client_ID') REFERENCES 'tclientinfo' ('Client_ID') ON DELETE CASCADE ON UPDATE CASCADE).</p>

<p>My add button to insert information into tclientinfo is:</p>

<p>private void btnAdd_Click(object sender, EventArgs e)
        {
            MySqlConnection conn = new MySqlConnection("server=localhost;uid=root;password=Passwordwhatever;database=dbocomputerinformation;");
            conn.Open();</p>

<pre><code>        string email = tbEmail.Text;
        string date = tbDate.Text;

        //try statement for validation before sending to database. 
        try
        {
            if (tbFirstName.Text.Length == 0)
            {
                MessageBox.Show("Please enter in customer's first name.");
                this.tbFirstName.Focus();
                return;
            }
            if (tbLastName.Text.Length == 0)
            {
                MessageBox.Show("Please enter in customer's last name.");
                this.tbLastName.Focus();
                return;
            }
            if (tbPhone.Text.Length &lt; 10)
            {
                MessageBox.Show("Phone number must be ten digits.");
                this.tbPhone.Focus();
                return;
            }
            if (tbPhone.Text.Length == 0)
            {
                MessageBox.Show("Please enter in customer's phone number.");
                this.tbPhone.Focus();
                return;
            }
            if (tbPhone.Text != string.Empty &amp;&amp; !Regex.IsMatch(tbPhone.Text, @"^[0-9]+$"))
            {
                MessageBox.Show("Please enter in numbers only.");
                this.tbPhone.Focus();
                return;
            }
            if (tbEmail.Text.Length == 0)
            {
                MessageBox.Show("Please enter in customer's email.");
                this.tbEmail.Focus();
                return;
            }
            if (email.IndexOf('@') == -1 || email.IndexOf('.') == -1)
            {
                MessageBox.Show("Please enter a valid email address.");
                this.tbEmail.Focus();
                return;
            }

        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        try
        {
            MySqlCommand command = new MySqlCommand("Insert into tclientinfo (First_Name, Last_Name, Phone, Email) values (?First_Name, ?Last_Name, ?Phone, ?Email)", conn);
            command.Parameters.AddWithValue("?First_Name", tbFirstName.Text);
            command.Parameters.AddWithValue("?Last_Name", tbLastName.Text);
            command.Parameters.AddWithValue("?Phone", tbPhone.Text);
            command.Parameters.AddWithValue("?Email", tbEmail.Text);

            command.ExecuteNonQuery();
            conn.Close();
            MessageBox.Show("Client Added Successfully.");

        }
        catch (MySqlException ex)
        {
            MessageBox.Show("Can't connect to database\n" + ex.ToString());
        }
    }
</code></pre>

<p>Then my insert into tserviceinfo is:</p>

<p>private void btnCreate_Click(object sender, EventArgs e)
        {
            //string connString = "server=localhost;uid=root;password=Passwordwhatever;database=dbocomputerinfo;";
            MySqlConnection conn = new MySqlConnection("server=localhost;uid=root;password=Passwordwhatever;database=dbocomputerinfo;");
            conn.Open();</p>

<pre><code>        string date = tbDate.Text;

        //try statement for validation before sending to database. 
        try
        {
            if (tbDate.Text.Length == 0)
            {
                MessageBox.Show("Please enter in date of service.");
                this.tbDate.Focus();
                return;
            }
            if (date.IndexOf('-') == -2) 
            {
                MessageBox.Show("Please enter correct date. mm-dd-yy.");
                this.tbDate.Focus();
                return;
            }
            if (tbTech.Text.Length == 0)
            {
                MessageBox.Show("Please enter in service technician.");
                this.tbTech.Focus();
                return;
            }
            if (tbBrand.Text.Length == 0)
            {
                MessageBox.Show("Please enter in customer's brand of computer.");
                this.tbBrand.Focus();
                return;
            }
            if (tbModel.Text.Length == 0)
            {
                MessageBox.Show("Please enter in computer model.");
                this.tbModel.Focus();
                return;
            }
            if (tbOS.Text.Length == 0)
            {
                MessageBox.Show("Please enter in computer's operating system.");
                this.tbOS.Focus();
                return;
            }
            if (rbDesktop.Checked == false &amp;&amp; rbNotebook.Checked == false)
            {
                MessageBox.Show("Please select device type.");
                this.rbDesktop.Focus();
                return;
            }
            if (lvOrdered.Items.Count == 0)
            {
                MessageBox.Show("Please select a service.");
                this.lvServices.Focus();
                return;
            }
            if (tbIssue.Text.Length == 0)
            {
                MessageBox.Show("Please describe the issue with the computer.");
                this.tbIssue.Focus();
                return;
            }
        }
        catch (Exception ex)
        {
            MessageBox.Show(ex.Message);
        }

        try
        {
            MySqlCommand command = new MySqlCommand("Insert into tserviceinfo (Date, Tech, Brand, Model, OS, Type, PC_Issue, Service, Service_Price, Total_Price) values (?Date, ?Tech, ?Brand, ?Model, ?OS, ?Type, ?PC_Issue, ?Service, ?Service_Price, ?Total_Price)", conn);
            command.Parameters.AddWithValue("?Date", tbDate.Text);
            command.Parameters.AddWithValue("?Tech", tbTech.Text);
            command.Parameters.AddWithValue("?Brand", tbBrand.Text);
            command.Parameters.AddWithValue("?Model", tbModel.Text);
            command.Parameters.AddWithValue("?OS", tbOS.Text);
            command.Parameters.AddWithValue("?PC_Issue", tbIssue.Text);

            /**
             * Was having issues inserting a single service, it would dublicate itself in the db, this was the solution
             * this adds just a single service with nothing else, and it reads back properly as well. 
            **/
            if (lvOrdered.Items.Count == 1)
            {
                string singleService = string.Join(" ", services);
                command.Parameters.AddWithValue("?Service", singleService);
            }
            else
            {
                /**
                * Combine all the services into one string seperated by commas
                * and add that to the database
                * */
                string serviceList = string.Join(", ", services);
                command.Parameters.AddWithValue("?Service", serviceList);

            }
            /**
             * Combine all the prices into one string seperated by commas
             * and add that to the database
             * */
            string servicePriceList = string.Join(", ", servicePrice);
            command.Parameters.AddWithValue("?Service_Price", servicePriceList);
            command.Parameters.AddWithValue("?Total_Price", lblPrice.Text);

            //clears lists so dublicates don't happen when creating more work orders than just 1.
            services.Clear();
            servicePrice.Clear();

            if (rbDesktop.Checked)
            {
                command.Parameters.AddWithValue("?Type", "Desktop");
            }
            else if (rbNotebook.Checked)
            {
                command.Parameters.AddWithValue("?Type", "Notebook");
            }

            command.ExecuteNonQuery();
            conn.Close();

            tbFirstName.Clear();
            tbLastName.Clear();
            tbDate.Clear();
            tbEmail.Clear();
            tbBrand.Clear();
            tbIssue.Clear();
            tbTech.Clear();
            tbModel.Clear();
            tbPhone.Clear();
            tbOS.Clear();
            lvOrdered.Items.Clear();
            lblPrice.Text = "$0.00";

        }
        catch (MySqlException ex)
        {
            MessageBox.Show("Can't connect to database\n" + ex.ToString());
        }
    }
</code></pre>

<p>So needless to say, I'm not positive on what to do from here. Any help would be awesome or any ideas. If you need anymore information, let me know. Thanks!</p>
