0

I'm practicing c#. So I created 2 database tables namely tbl_applicant and tbl_parent.

tbl_applicant attributes are:

 - AID (primary key)
 - Name
 - PID (foreign key)

tbl_parent attributes are:

 - PID (primary key)
 - FatherName
 - MotherName

So I got a form in MS Visual Studio 2015 that has the textboxes needed to store the data that the database requires. I also used "identity" (on the pk) and "reference" (on the fk) to associate them with being a primary/foreign key. In c#, I tested the database by recording a row (record) in the tables. Primary keys are automatically generated starting with 1.

Here's the problem, I shouldn't have to place a textbox for PID since it's going to automatically generate me a PID on the tbl_parent but why am I getting errors when I don't put a PID on the tbl_applicant manually? Isn't it supposed to have a number that will be copied/passed to the PID in tbl_applicant? What am I doing wrong here?

Amit Kumar Singh
  • 4,393
  • 2
  • 9
  • 22
Marion
  • 13
  • 6
  • MySQL and ms sql server are two different products with different implementations of the auto increment field you are asking about. So, which one do you use? Also, I'm 100% sure that this question has been asked before here on SO (just do not want to hammer with a MySQL duplicate if you are using ms sql server), so pls do some research as well. – Shadow Oct 04 '17 at 10:05
  • I believe MS SQL server is the one I am using. If this question has been asked and answered already then that means I didn't understand what I just read. I've been trying to read for hours on google this website, I even tried a couple of sql codes but didn't work. Maybe the question I've found just have too much variables for me to understand it. Sorry if I asked an already answered question but I am tired of researching. I am a newbie and I have been searching for hours for this simple question. If what I did is bannable then so be it. Sorry. – Marion Oct 04 '17 at 10:11
  • To auto generate the id, make the column identity. First add the record to tbl_parent, get the id of newly added column and add it as pid to tbl_applicant. – Rakesh Burbure Oct 04 '17 at 10:13
  • Thanks for the reply but I am tired of reading and relating to other people's scenario. I've done that and I am unsuccessful. Please help me with my own scenario. – Marion Oct 04 '17 at 10:13
  • It is not a bannable question at all. However, if a question has been asked before here on SO and it has a valid answer, then the community votes to close down the question as a duplicate. – Shadow Oct 04 '17 at 10:14
  • @Marion there is nothing in your question that demonstrates what you have done and where you got stuck, so there is no "own scenario" we could possibly help you with. – Shadow Oct 04 '17 at 10:19
  • @RakeshBurbure The identity part, I've done already. Can I do what you are saying even though I'm only using 1 button which saves the text in the textboxes? – Marion Oct 04 '17 at 10:19
  • @Shadow im sorry for being stupid. – Marion Oct 04 '17 at 10:21
  • @Marion: yes, it is possible. I believe you are going write all these in a stored procedure. – Rakesh Burbure Oct 04 '17 at 10:23
  • @RakeshBurbure Looks like I'll be going to learn this stored procedure. Thanks for being helpful. :) – Marion Oct 04 '17 at 10:25
  • @Marion https://learn.microsoft.com/en-us/sql/t-sql/functions/scope-identity-transact-sql – Keith Oct 04 '17 at 10:29
  • Looks like this is what I really need. thanks @Leonidas199x – Marion Oct 04 '17 at 10:34
  • @Marion no worries - you have a couple of options, either handle it all in stored procs, passing the data from your form to the stored proc to insert into parent, then pass the parent ID to a stored proc to insert into applicant, or, use an output parameter in your stored proc, to pass the ID of the inserted ID back to your application, then pass it into your method to insert into applicant. – Keith Oct 04 '17 at 10:36

0 Answers0