473,396 Members | 1,860 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.

HELP! Best practice for lookup data and foreign key values?

I have a question about best practices of how to deal with lookup data from
my C# apps. On a couple of occasions I have come across a problem where I
have to automate inserting a record into a table that has a foreign key
constraint that is linked to a lookup table. E.g. Take the following
database structure:
SQL-Server Database:

Table 1:
Name: CommunicationTypes
Columns: CommunicationID (Primary Key), Description
Table 2:
Name: Communications
Columns: CommunicationID (Primary Key), CommunicationTypeID (Foreign Key),
Sender, Recipient, etc.

...In this example, there is data such as the following in the
CommunicationTypes table:

CommunicationTypeID Description
------------------ ------------
1 Email
2 Letter
3 Phonecall
etc.....
My app needs to log details of communications, but in some cases,
auto-generates emails and letters. When this occurs I need to auto-insert a
record into the communications table. This means my app needs to know about
what the CommunicationTypeID is for the communication being 'auto-recorded'.
Should my app retrieve the CommunicationTypeID from the database by passing
the description? If the list is short, could I store the ID's in the config
file? Should I have a strongly typed class that has static methods to return
the ID from the lookup table? Other suggestions please!!

My question is, what is the best practice for situations like this, where:
a) You have a lookup table that will be added to over time
b) You need to insert a record into a table that has a foreign key related
to the lookup table, but the app needs to know the foreign key value without
the user selecting anything from a list.

Many thanks

JamesE
Sep 7 '05 #1
1 3061
Hi James,

There are several answers to your situation - I use SQL Server stored
procedures in almost all instances like this.

I break down my sp's into common actions like inserting and updating (1 sp),
deleting, etc. For instance, if I wanted to insert a communication record I
would create an sp named "spInsertCommunication" with 3 parameters.
CommunicationType
Sender
Recipient

Within the sp I would lookup the CommunicationTypeID (and probably the
SenderID and RecipientID for that matter) like this.

Declare @CommunicationTypeID as tinyint
SELECT @CommunicationTypeID = CommunicationTypeID FROM CommunicationTypes
WHERE CommunicationType = @CommunicationType

....and while not always needed...
IF ISNULL(@CommunicationTypeID,0) < 1
BEGIN
INSERT INTO CommunicationTypes (CommunicationType) VALUES
(@CommunicationType)
SET @CommunicationTypeID = SCOPE_IDENTITY()
END

--Do your insert to the Communication table here.

There are a lot of examples so I won't bore you with the details, but check
out the System.Data.SQLClient.Command and Parameter objects. These objects
are the best way to communicate to sp's.

Hope this helps,
Mike
"James E" wrote:
I have a question about best practices of how to deal with lookup data from
my C# apps. On a couple of occasions I have come across a problem where I
have to automate inserting a record into a table that has a foreign key
constraint that is linked to a lookup table. E.g. Take the following
database structure:
SQL-Server Database:

Table 1:
Name: CommunicationTypes
Columns: CommunicationID (Primary Key), Description
Table 2:
Name: Communications
Columns: CommunicationID (Primary Key), CommunicationTypeID (Foreign Key),
Sender, Recipient, etc.

...In this example, there is data such as the following in the
CommunicationTypes table:

CommunicationTypeID Description
------------------ ------------
1 Email
2 Letter
3 Phonecall
etc.....
My app needs to log details of communications, but in some cases,
auto-generates emails and letters. When this occurs I need to auto-insert a
record into the communications table. This means my app needs to know about
what the CommunicationTypeID is for the communication being 'auto-recorded'.
Should my app retrieve the CommunicationTypeID from the database by passing
the description? If the list is short, could I store the ID's in the config
file? Should I have a strongly typed class that has static methods to return
the ID from the lookup table? Other suggestions please!!

My question is, what is the best practice for situations like this, where:
a) You have a lookup table that will be added to over time
b) You need to insert a record into a table that has a foreign key related
to the lookup table, but the app needs to know the foreign key value without
the user selecting anything from a list.

Many thanks

JamesE

Sep 7 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

9
by: Tom | last post by:
A question for gui application programmers. . . I 've got some GUI programs, written in Python/wxPython, and I've got a help button and a help menu item. Also, I've got a compiled file made with...
4
by: Sarir Khamsi | last post by:
Is there a way to get help the way you get it from the Python interpreter (eg, 'help(dir)' gives help on the 'dir' command) in the module cmd.Cmd? I know how to add commands and help text to...
6
by: d.warnermurray | last post by:
I am doing a project for school that involves creating help files for a html authoring tool. If you could help me with answers to some questions it would really help. 1. What tasks do you expect...
4
by: dixie | last post by:
Help, I'm really out of my depth here (not unusual I hear you say :-). I have just installed HTML Help in an application. I told it in the Project Properties the path to the help file. I then...
4
by: Fred Flintstone | last post by:
This one baffles me. I'm using VS.Net 2005 and write desktop apps that need built in help. So logically, I figure maybe VS has a help system component built in so I search the help. Hey! ...
8
by: Mark | last post by:
I have loaded Visual Studio .net on my home computer and my laptop, but my home computer has an abbreviated help screen not 2% of the help on my laptop. All the settings look the same on both...
3
by: lord.zoltar | last post by:
I've managed to get a nice little chm help system written. Now I need to display it! I added a HelpProvider to my MDIParent form and set the namespace of the HelpProvider to be the help file. So...
10
by: JonathanOrlev | last post by:
Hello everybody, I wrote this comment in another message of mine, but decided to post it again as a standalone message. I think that Microsoft's Office 2003 help system is horrible, probably...
1
by: trunxnirvana007 | last post by:
'UPGRADE_WARNING: Array has a new behavior. Click for more: 'ms-help://MS.VSCC.v80/dv_commoner/local/redirect.htm?keyword="9B7D5ADD-D8FE-4819-A36C-6DEDAF088CC7"' 'UPGRADE_WARNING: Couldn't resolve...
0
by: hitencontractor | last post by:
I am working on .NET Version 2003 making an SDI application that calls MS Excel 2003. I added a menu item called "MyApp Help" in the end of the menu bar to show Help-> About. The application...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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...

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.