Enzo Enzo

Enzo Enzo

  • NA
  • 26
  • 2.6k

Insert into multiple table PM and FkEy mysql database.

Jan 23 2017 11:33 AM
  
 
I want that value tblpetinfo.petid = tblpetowner.petid and tbladdress.address = tblpetowner.addressid
 
Can you add a condition for Terms List Box = lbTerm,   CheckBox I Agree = chkTerm
 
Thank!!! 
 
SOURCE CODE::: 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.ComponentModel;  
  4. using System.Data;  
  5. using System.Drawing;  
  6. using System.Linq;  
  7. using System.Text;  
  8. using System.Threading.Tasks;  
  9. using System.Windows.Forms;  
  10. using MySql.Data.MySqlClient;  
  11. using System.Configuration;  
  12.   
  13.   
  14. namespace gpis  
  15. {  
  16.     public partial class registrationform : Form  
  17.     {  
  18.   
  19.         public registrationform()  
  20.         {  
  21.             InitializeComponent();  
  22.         }  
  23.   
  24.   
  25.         private void picHome_Click(object sender, EventArgs e)  
  26.         {  
  27.             menu frm = new menu();  
  28.             frm.Show();  
  29.   
  30.             this.Hide();  
  31.         }  
  32.   
  33.   
  34.   
  35.   
  36.         private void bRegister_Click(object sender, EventArgs e)  
  37.         {  
  38.             try  
  39.             {  
  40.                 //This is my connection string i have assigned the database file address path    
  41.                 string dbsource = "datasource=localhost;port=3306;username=root;password=admin1234; database=db_gpis";  
  42.                 //This is  MySqlConnection here i have created the object and pass my connection string  
  43.                 MySqlConnection MyConn1 = new MySqlConnection(dbsource);  
  44.   
  45.                 //This is my insert query in which i am taking input from the user through windows forms    
  46.                 string qpowner = "insert into tblpetowner (polname,pofname,pomi,podob,pogen,pocontact,poemail,addressid) values(@ln,@fn,@mi,@dob,@gen,@cn,@em,addressid);";  
  47.   
  48.   
  49.                 //This is command class which will handle the vetinsert and myconn1 object.    
  50.                 MySqlCommand cmd1 = new MySqlCommand(qpowner, MyConn1);  
  51.                 cmd1.Parameters.AddWithValue("@ln", tLastname.Text);  
  52.                 cmd1.Parameters.AddWithValue("@fn", tFirstname.Text);  
  53.                 cmd1.Parameters.AddWithValue("@mi", tMiddleinitial.Text);  
  54.                 cmd1.Parameters.AddWithValue("@dob", tDob.Text);  
  55.                 cmd1.Parameters.AddWithValue("@gen", cmbGender.Text);  
  56.                 cmd1.Parameters.AddWithValue("@cn", tContactnum.Text);  
  57.                 cmd1.Parameters.AddWithValue("@em", tEmailadd.Text);  
  58.                   
  59.   
  60.   
  61.                 MySqlDataReader rdr1;  
  62.                 MyConn1.Open();  
  63.                 rdr1 = cmd1.ExecuteReader(); // Here our query will be executed and data saved into the database.   
  64.                 //while (rdr1.Read())  
  65.                 //{  
  66.   
  67.                 //}  
  68.                 //MyConn1.Close();  
  69.   
  70.                 //NEW CONNECTION  
  71.                 MySqlConnection addconn2 = new MySqlConnection(dbsource);  
  72.                 //This is my insert query in which i am taking input from the user through windows forms    
  73.                 string qaddress = "insert into tbladdress (addressid,hnum,street,brgy,city,region,country,zcode) values(@id,@hn,@st,@brgy,@cty,@re,@coun,@zc);";  
  74.                 //This is  MySqlConnection here i have created the object and pass my connection string.    
  75.   
  76.   
  77.                 //This is command class which will handle the query and connection object.    
  78.                 MySqlCommand cmd2 = new MySqlCommand(qaddress, addconn2);  
  79.                 cmd2.Parameters.AddWithValue("@id"null);  
  80.                 cmd2.Parameters.AddWithValue("@hn", tHousenum.Text);  
  81.                 cmd2.Parameters.AddWithValue("@st", tStreet.Text);  
  82.                 cmd2.Parameters.AddWithValue("@brgy", tBrgy.Text);  
  83.                 cmd2.Parameters.AddWithValue("@cty", cmbCity.Text);  
  84.                 cmd2.Parameters.AddWithValue("@re", cmbRegion.Text);  
  85.                 cmd2.Parameters.AddWithValue("@coun", cmbCountry.Text);  
  86.                 cmd2.Parameters.AddWithValue("@zc", tZipcode.Text);  
  87.   
  88.                 MySqlDataReader rdr2;  
  89.                 addconn2.Open();  
  90.                 rdr2 = cmd2.ExecuteReader();  
  91.                 //while (rdr2.Read())  
  92.                 //{  
  93.   
  94.                 //}  
  95.                 //addconn2.Close();  
  96.   
  97.                 //NEW CONNECTION  
  98.                 MySqlConnection addconn3 = new MySqlConnection(dbsource);  
  99.                 //This is my insert query in which i am taking input from the user through windows forms    
  100.                 string qpet = "insert into tblpetinfo (petid,petname, petcolor, petgen, ptype, pbreed) values(@id,@pn,@pc,@pg,@pt,@pb);";  
  101.                 //This is  MySqlConnection here i have created the object and pass my connection string.    
  102.   
  103.   
  104.                 //This is command class which will handle the query and connection object.    
  105.                 MySqlCommand cmd3 = new MySqlCommand(qpet, addconn3);  
  106.                 cmd3.Parameters.AddWithValue("@id"null);  
  107.                 cmd3.Parameters.AddWithValue("@pn", tPetname.Text);  
  108.                 cmd3.Parameters.AddWithValue("@pc", tPetcolor.Text);  
  109.                 cmd3.Parameters.AddWithValue("@pg", cmbPetgender.Text);  
  110.                 cmd3.Parameters.AddWithValue("@pt", cmbType.Text);  
  111.                 cmd3.Parameters.AddWithValue("@pb", cmbBreed.Text);  
  112.   
  113.   
  114.                 MySqlDataReader rdr3;  
  115.                 addconn3.Open();  
  116.                 rdr3 = cmd3.ExecuteReader();  
  117.                   
  118.   
  119.                 MessageBox.Show("Successful account registered");  
  120.                 menu menuForm = new menu();  
  121.                 menuForm.Show();  
  122.                 this.Hide();  
  123.                   
  124.   
  125.                 while (rdr3.Read())  
  126.                 {  
  127.                       
  128.                 }  
  129.                 addconn3.Close();  
  130.   
  131.   
  132.   
  133.             }  
  134.             catch (Exception ex)  
  135.             {  
  136.                 MessageBox.Show(ex.Message);  
  137.             }  
  138.         }     
  139.     }  
  140. }  

Answers (3)