Khodam A

Khodam A

  • NA
  • 12
  • 0

creating new record in child table

Mar 5 2007 11:30 AM
I have 2 tables Patient(parent) , Battery(Child). when i gave input to it, in parent table the new record will be added but in child table the new record will not be added although I'm sending the FK to child table and also i set the automatic identity generator for it. why it will not add the new record to child table?
string connection = "Data Source=.\\SQLEXPRESS;AttachDbFilename=\"C:\\Program Files\\Microsoft SQL Server\\MSSQL.1\\MSSQL\\Data\\AHP.mdf\";Integrated Security=True;User Instance=True";
            SqlConnection sqlConnection = new SqlConnection(connection);
            SqlCommand insertCommand = sqlConnection.CreateCommand();
 
            insertCommand.Connection.Open ();
           
            // PATIENT
            insertCommand.CommandText = "SET IDENTITY_INSERT PATIENT ON; ";
            insertCommand.ExecuteNonQuery();
 
            insertCommand.CommandText = "INSERT INTO PATIENT ( P_NAME, P_DOB, P_ADDRESS, P-PHONE, P_MOBILE, P_DOCTOR) VALUES " +
                "( @P_NAME, @P_DOB, @P_ADDRESS, @P-PHONE, @P_MOBILE, @P_DOCTOR);";
           
            SqlParameter pNameParameter = new SqlParameter ("@P_NAME", SqlDbType.VarChar, 50);
            pNameParameter.Direction = ParameterDirection.Input;
            pNameParameter.Value = p_NAMETextBox.Text;
           
            SqlParameter pDOBParameter = new SqlParameter("@P_DOB", SqlDbType.SmallDateTime);
            pDOBParameter.Direction = ParameterDirection.Input;
            pDOBParameter.Value = p_DOBDateTimePicker.Text;
 
            SqlParameter pAddressParameter = new SqlParameter("@P_ADDRESS", SqlDbType.VarChar, 300);
            pAddressParameter.Direction = ParameterDirection.Input;
            pAddressParameter.Value = p_ADDRESSTextBox.Text;
 
            SqlParameter pPhoneParameter = new SqlParameter("@P_PHONE", SqlDbType.Int);
            pPhoneParameter.Direction = ParameterDirection.Input;
            pPhoneParameter.Value = p_PHONETextBox.Text;
 
            SqlParameter pMobileParameter = new SqlParameter( "@P_MOBILE", SqlDbType.Int);
            pMobileParameter.Direction = ParameterDirection.Input;
            pMobileParameter.Value = p_MOBILETextBox.Text;
 
            SqlParameter pDoctorParameter = new SqlParameter("@P_DOCTOR", SqlDbType.VarChar, 50);
            pDoctorParameter.Direction = ParameterDirection.Input;
            pDoctorParameter.Value = p_DOCTORTextBox.Text;
 
            insertCommand.CommandText = "SET IDENTITY_INSERT PATIENT OFF; ";
            insertCommand.ExecuteNonQuery();
 
         
            // BATTERY
 
            insertCommand.CommandText = "SET IDENTITY_INSERT BATTERY ON; ";
            insertCommand.ExecuteNonQuery();
 
            insertCommand.CommandText = "INSERT INTO BATTERY (B_BRAND, B_FDU, B_DOE, P_ID)"
            + " VALUES ( @B_BRAND, @B_FDU, @B_DOE, @P_ID);";
 
 
            SqlParameter bBrandParameter = new SqlParameter("@B_BRAND", SqlDbType.VarChar, 50);
            bBrandParameter.Direction = ParameterDirection.Input;
            bBrandParameter.Value = b_BRANDTextBox.Text;
           
            SqlParameter bFDUParameter = new SqlParameter( "@B_FDU", SqlDbType.SmallDateTime);
            bFDUParameter.Direction = ParameterDirection .Input;
            bFDUParameter.Value = b_FDUDateTimePicker.Text;
 
            SqlParameter bDOEParameter = new SqlParameter("@B_DOE", SqlDbType.SmallDateTime);
            bDOEParameter.Direction = ParameterDirection.Input;
            bDOEParameter.Value = b_DOEDateTimePicker.Text;
 
            SqlParameter pIDParameter = new SqlParameter("@P_ID", SqlDbType.Int );
            pIDParameter.Direction = ParameterDirection.InputOutput;
            pIDParameter.Value = p_IDComboBox.Text;
            MessageBox.Show(p_IDComboBox.Text, "show", MessageBoxButtons.OK, MessageBoxIcon.Information);
           
            insertCommand.CommandText = "SET IDENTITY_INSERT BATTERY OFF ;";
            insertCommand.ExecuteNonQuery();
 
 
            //insert add
 
            insertCommand.Parameters.Add(pNameParameter);
            insertCommand.Parameters.Add(pDOBParameter);
            insertCommand.Parameters.Add(pAddressParameter);
            insertCommand.Parameters.Add(pPhoneParameter);
            insertCommand.Parameters.Add(pMobileParameter);
            insertCommand.Parameters.Add(pDoctorParameter);
 
            insertCommand.Parameters.Add(bBrandParameter);
            insertCommand.Parameters.Add(bFDUParameter);
            insertCommand.Parameters.Add(bDOEParameter);
            insertCommand.Parameters.Add(pIDParameter);
 
            insertCommand.ExecuteNonQuery ();
 
            insertCommand.Connection.Close();