473,396 Members | 1,608 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Creating new record in child table

13
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:\\Progra m 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();
Mar 5 '07 #1
1 1553
radcaesar
759 Expert 512MB
The Executenonquery() method will be (to be pasted) after the Insert command text. It should be at last (After Command Text, Passed all the parameters etc)

:)
Mar 5 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: Grim Reaper | last post by:
I know this should be easy (I think), but I can not figure out how to do it. Basically, I have 4 tables (2 tables with 2 subordinate tables) that have an INQ_ID key that was created by using...
5
by: Ilan Sebba | last post by:
When it comes to adding records in related tables, Access is really smart. But when I try to do the same using ADO, I am really stupid. Say I have two parent tables (eg Course, Student) and one...
5
by: Mike Turco | last post by:
What is the difference between creating relationships in the front-end vs. the back-end database? I was trying to create a relationship in a database front-end and noticed that I could not check...
4
by: | last post by:
This is blowing my mind because I thought this setup was working.... How is a new record generated in the table of a Child subform whenever a new record is generated in the table of the Master...
2
by: Swinky | last post by:
I hope someone can help...I feel like I'm walking in the dark without a flashlight (I'm NOT a programmer but have been called to task to do some work in Access that is above my head). I have...
7
by: john | last post by:
In my form I have a master table and a details table linked 1xM. I can search through the whole parent table but I also like to be able to search through the child table fields to find parent...
1
by: Tony Dunn | last post by:
I have inherited another developer's database (Access 2003) and with it a problem I'm struggling with. The problem is this. I have a parent-child form pair, which is populated by a table (the...
0
by: emalcolm_FLA | last post by:
Hello and TIA for any help with this non profit Christmas assistance project. I have an applicant (app history) and child (child history) tables (4 total). I need to grab the next available (in...
2
by: adwest | last post by:
Forgive me if this is a foolish question. I "play" in Access and have only created a few rather simple relational databases. My knowledge and experience is limited. I have no formal training, just...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.