473,434 Members | 1,506 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,434 software developers and data experts.

Table Relationships

20
hi abit of a noob question but here goes. i've using visual studio 2008 and sql 2005 express. i have a users table(part of asp.net 3.5 membership framework) and a userfunds table that records account balance. i have set up the primary key and foreign key constraint between the 2 tables with the UserId column and set Enforce Foriegn Key constriant to true. this means a 1 to 1 relationship right?

next i have a user deposits table which i want to record every deposit made by a user so i will have a UserId column (and amount and date). this will have multiple records showing deposits for each user so will be a 1 to many relationship with multiple entries for UserId. how do i set this up? do i create a primary key for UserId and set up a relationship but not set it to be enforced? thanks for any help.
May 16 '10 #1

✓ answered by ck9663

Here, read this...

Happy Coding!!!

~~ CK

10 2385
ck9663
2,878 Expert 2GB
Try creating a unique key in your deposit table (ie Transaction Number), that will be your primary key. You're going to need a UserId foreign key to link it your users datable. A foreign key means you may have duplicate on the child table but must have a unique on the parent table.

Happy Coding!!!

~~ CK
May 17 '10 #2
pinman
20
hi mate. have just changed the depsoits table to add a transaction number as the primary key just like you said but about 10 minutes before i read your post lol. i've got userId but haven't yet set the foriegn key in it.(does primary key mean no duplicate entries)?
in visual studio i create the foriegn key constraint but am unsure as to what it means. there is an option that says "enforce Foriegn Key Constraint" does this stop you from a 1 to many realtionship? or does that mean any changes in the parent table will delete the entry in the child table(if you set it yes)? but set to no will not alter this child table? cheers
May 18 '10 #3
ck9663
2,878 Expert 2GB
Here, read this...

Happy Coding!!!

~~ CK
May 18 '10 #4
pinman
20
cool thanks. i've set up the foriegn keys and written a stored procedure and code to execute it in asp.net but it's not working. not sure if it's my procedure or the asp.net code. any chance i could get some pointers please. i'm using the aspnet membership framework to store and retriee user information. is it possible to put a breakpoint in the SQL stored procedure so execution stops when the procedure runs?

Expand|Select|Wrap|Line Numbers
  1. ALTER PROCEDURE dbo.Desposit_Funds
  2.     (
  3.     @UserName varchar (256),
  4.     @Amount money,
  5.     @DepositId uniqueidentifier
  6.     )
  7.  
  8. AS
  9.  
  10. DECLARE @UserId uniqueidentifier
  11. SELECT @UserId = UserId FROM aspnet_Users WHERE UserName = @UserName
  12.  
  13. DECLARE @currentbalance money
  14. SELECT @currentbalance = AccountBalance FROM UserFunds WHERE @userId = UserId
  15.  
  16. UPDATE UserFunds
  17. SET LastDepositDate = GETDATE(),
  18.     AccountBalance = (@currentbalance + @Amount)
  19.     WHERE UserId = @UserId
  20.  
  21. INSERT INTO UserDeposits(DepositId, UserId, DepositDate, Amount)
  22. VALUES(@DepositId, @UserId, GETDATE(), @Amount)
  23.  
and my code in asp.net c# to run this procedure is below. i've tried the 2 ways below to get my parameter objects sorted out(first way i've commented out
Expand|Select|Wrap|Line Numbers
  1. try{
  2.             ConnectionStringSettings constring = ConfigurationManager.ConnectionStrings["ConnectionDB"];
  3.  
  4.             SqlConnection connect = new SqlConnection(constring.ConnectionString);
  5.             using(connect)
  6.             {
  7.                 SqlCommand deposit = connect.CreateCommand();
  8.                 deposit.CommandType = CommandType.StoredProcedure;
  9.                 deposit.CommandText = "Deposit_Funds";
  10.  
  11.                 //SqlParameter paramet1 = deposit.CreateParameter();
  12.                 //paramet1.ParameterName = "@UserName";
  13.                 //paramet1.Value = current.UserName;
  14.  
  15.                 //SqlParameter paramet2 = deposit.CreateParameter();
  16.                 //paramet2.ParameterName = "@Amount";
  17.                 decimal t;
  18.                 decimal.TryParse(TextBox1.Text, out t);
  19.                 //paramet2.Value = t;
  20.  
  21.                 Guid ident = Guid.NewGuid();
  22.  
  23.                 //SqlParameter paramet3 = deposit.CreateParameter();
  24.                 //paramet3.ParameterName = "@DepositId";
  25.                 //paramet3.Value = ident;
  26.  
  27.  
  28.                 deposit.Parameters.Add("@UserName", SqlDbType.VarChar).Value = current.UserName.ToString();
  29.                 deposit.Parameters.Add("@Amount", SqlDbType.Money).Value = t;
  30.                 deposit.Parameters.Add("@DepositId", SqlDbType.UniqueIdentifier).Value = ident;
  31.                 connect.Open();
  32.                 int count = deposit.ExecuteNonQuery();
  33.  
  34.             }
  35.         }
  36.  
May 18 '10 #5
pinman
20
sweet jesus. i've spelt the name of stored procedure wrong. chalk that one up to a simple typo lol
May 18 '10 #6
ck9663
2,878 Expert 2GB
Does the stored proc execute in Query window? If it does, it could be on your front-end.

Good Luck!!!

~~ CK
May 18 '10 #7
pinman
20
it does run in query window in visual studio. what do you mean by front end? cheers
May 18 '10 #8
Frinavale
9,735 Expert Mod 8TB
To me front end would mean your ASPX page :)
May 18 '10 #9
pinman
20
i get you now. you mean the fault is on the front end. but i just said it was working :) cheers. any pointers for the code itself structure of store proc etc...?
May 18 '10 #10
ck9663
2,878 Expert 2GB
If it's running in SQL Query Analyzer, the problem is on your front-end code. Post it on the java/c forum. They might be able to help you more...

Good Luck!!!

~~ CK
May 19 '10 #11

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

Similar topics

33
by: Joe | last post by:
I'm designing a company website. I'm relatively new to CSS and I'm having trouble creating what seems to me a very simple design: - body background: fixed full page image - banner: fixed, 100...
18
by: Jeremy Weiss | last post by:
I'm trying to build a database that will handle the monthly billing needs of a small company. I'm charting everything out and here's what I see: table for customers sub table to track payments...
4
by: Jenni | last post by:
Hopefully someone out there can help. I am currently trying to write some code to allow me to delete a table, then recreate it and re-establish the relationships. I seem to have hit a snag in the...
0
by: Megan | last post by:
Hi Everybody- I know that this is a really, really long post, but I wanted to try to give you as much background as possible. So here's a quick overview of the issues I'm asking for help with:...
2
by: Max | last post by:
Hi. I really hope someone can help me. Going slowly insane with this problem. I have a two Access 2000 databases. One is the backend containing tables and some admin queries. The other is the...
2
by: Deano | last post by:
Just had a minor mishap which has meant starting a new database and importing all objects. I did have the option to import relationships ticked but these have not come over. The prospect of...
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...
7
by: davegb | last post by:
I'm totally new to relational database design. My boss has asked me to create a database of information on the employees in our group. Seemed to me like a simple application to learn the ropes. A...
45
by: salad | last post by:
I'm curious about your opinion on setting relationships. When I designed my first app in Access I'd go to Tools/Relationships and set the relationships. Over time I'd go into the window and see...
5
by: Kosmos | last post by:
Hey :) hopefully someone can help me with this...I decided to take on the task of programming an access database for my legal co-op/internship...I'm studying law and music production on the...
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:
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
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...
1
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
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,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.