473,407 Members | 2,326 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,407 software developers and data experts.

How can I INSERT w/Constraints in other tables?

This:

string strSQL2 = "INSERT INTO tblTravelDetailMember(memberId, " +
" TravelDetailUplineId, " +
" rankId, " +
" TravelDetailId, " +
" CreatedDateTime, " +
" Operator) " +
"VALUES ('" + insertString2 + "', " +
" '" + insertString3 + "', " +
" '" + insertString4 + "', " +
" '" + insertString5 + "', " +
" '" + insertString6 + "', " +
" '" + insertString7 + "')";

Here is the tblTravelDetailMember:
CREATE TABLE [tblTravelDetailMember] (
[TravelDetailUplineId] [int] NOT NULL ,
[TravelDetailId] [int] NOT NULL ,
[TravelDetailMemberTypeId] [int] NOT NULL ,
[memberId] [bigint] NOT NULL ,
[rankId] [int] NOT NULL ,
[CreatedDateTime] [datetime] NOT NULL ,
[Operator] [nvarchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
CONSTRAINT [PK_tblTravelDetailMember] PRIMARY KEY CLUSTERED
(
[TravelDetailUplineId]
) ON [PRIMARY] ,
CONSTRAINT [FK_tblTravelDetailMember_tblTravelDetail] FOREIGN KEY
(
[TravelDetailId]
) REFERENCES [tblTravelDetail] (
[TravelDetailId]
),
CONSTRAINT [FK_tblTravelDetailMember_tblTravelDetailMemberType]
FOREIGN KEY
(
[TravelDetailMemberTypeId]
) REFERENCES [tblTravelDetailMemberType] (
[TravelDetailMemberTypeId]
)
) ON [PRIMARY]
GO

I get these errors:

Additional information: INSERT statement conflicted with COLUMN FOREIGN
KEY
constraint 'FK_tblTravelDetailMember_tblTravelDetailMemberTyp e'. The
conflict
occurred in database 'tsNess', table 'tblTravelDetailMemberType',
column
'TravelDetailMemberTypeId'.

Additional information: Cannot insert the value NULL into column
'TravelDetailMemberTypeId', table 'tsNess.dbo.tblTravelDetailMember';
column does not allow nulls. INSERT fails.

Here is the table I am not doing an entry on:
CREATE TABLE [tblTravelDetailMemberType] (
[TravelDetailMemberTypeId] [int] NOT NULL ,
[Description] [nvarchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS
NOT NULL ,
[CreatedDateTime] [datetime] NOT NULL ,
[Operator] [nvarchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
CONSTRAINT [PK_tblTravelDetailMemberType] PRIMARY KEY CLUSTERED
(
[TravelDetailMemberTypeId]
) ON [PRIMARY]
) ON [PRIMARY]
GO

How can I do an INSERT into the tblTravelDetailMember table if it won't
let me and what will I have to do in order to do this please?
Thanks,
Trint

Nov 17 '05 #1
7 1928
trint wrote:
This would probably be better on one of the SQL groups but

See In-line
This:

string strSQL2 = "INSERT INTO tblTravelDetailMember(memberId, " +
" TravelDetailUplineId, " +
" rankId, " +
" TravelDetailId, " +
" CreatedDateTime, " +
" Operator) " +
"VALUES ('" + insertString2 + "', " +
" '" + insertString3 + "', " +
" '" + insertString4 + "', " +
" '" + insertString5 + "', " +
" '" + insertString6 + "', " +
" '" + insertString7 + "')";

You have a constraint that says that the
tblTravelDetailMember.TravelDetailMemberTypeId must exist in the
tblTravelDetailMemberType.TravelDetailMemberTypeId column.
You are not specifying a value for this column in your insert statement
therefore it will use the default value or null if no default is defined.

Either specify a value for TravelDetailMemberTypeId (recommended) in
your insert statement or define a default for this column.

HTH
JB
Here is the tblTravelDetailMember:
CREATE TABLE [tblTravelDetailMember] (
[TravelDetailUplineId] [int] NOT NULL ,
[TravelDetailId] [int] NOT NULL ,
[TravelDetailMemberTypeId] [int] NOT NULL ,
[memberId] [bigint] NOT NULL ,
[rankId] [int] NOT NULL ,
[CreatedDateTime] [datetime] NOT NULL ,
[Operator] [nvarchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
CONSTRAINT [PK_tblTravelDetailMember] PRIMARY KEY CLUSTERED
(
[TravelDetailUplineId]
) ON [PRIMARY] ,
CONSTRAINT [FK_tblTravelDetailMember_tblTravelDetail] FOREIGN KEY
(
[TravelDetailId]
) REFERENCES [tblTravelDetail] (
[TravelDetailId]
),
CONSTRAINT [FK_tblTravelDetailMember_tblTravelDetailMemberType]
FOREIGN KEY
(
[TravelDetailMemberTypeId]
) REFERENCES [tblTravelDetailMemberType] (
[TravelDetailMemberTypeId]
)
) ON [PRIMARY]
GO

I get these errors:

Additional information: INSERT statement conflicted with COLUMN FOREIGN
KEY
constraint 'FK_tblTravelDetailMember_tblTravelDetailMemberTyp e'. The
conflict
occurred in database 'tsNess', table 'tblTravelDetailMemberType',
column
'TravelDetailMemberTypeId'.

Additional information: Cannot insert the value NULL into column
'TravelDetailMemberTypeId', table 'tsNess.dbo.tblTravelDetailMember';
column does not allow nulls. INSERT fails.

Here is the table I am not doing an entry on:
CREATE TABLE [tblTravelDetailMemberType] (
[TravelDetailMemberTypeId] [int] NOT NULL ,
[Description] [nvarchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS
NOT NULL ,
[CreatedDateTime] [datetime] NOT NULL ,
[Operator] [nvarchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
CONSTRAINT [PK_tblTravelDetailMemberType] PRIMARY KEY CLUSTERED
(
[TravelDetailMemberTypeId]
) ON [PRIMARY]
) ON [PRIMARY]
GO

How can I do an INSERT into the tblTravelDetailMember table if it won't
let me and what will I have to do in order to do this please?
Thanks,
Trint

Nov 17 '05 #2
John,
I've got most of it figured out now except this:

@@IDENTITY AS 'Identity'
How do I get the 'Identity' into a string?
Thanks,
Trint

.Net programmer
tr***********@gmail.com

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #3
Trint Smith wrote:
John,
I've got most of it figured out now except this:

@@IDENTITY AS 'Identity'
How do I get the 'Identity' into a string?
Thanks,
Trint


@@IDENTITY will select the last identity (this is database wide and not
limited to a specific table (AFAIK).
Why do you need this?
What you want is a TravelDetailMemberTypeId from the
tblTravelDetailMemberType table.

Lets say you are filling these details out on a form.
You create controls for all the values.
You will probably have a combo box that lists all the
TravelDetailMemberType names from the tblTravelDetailMemberType table.

Then when the user presses save, you will want to get the
TravelDetailMemberTypeId from the combo box and pass that as part of
your insert statement so it saves the correct TravelDetailMemberType value.

Also, TravelDetailMemberTypeId is not defined as an IDENTITY column
(AutoIncrement) so @@IDENTITY will have no bearing on this.

JB
Nov 17 '05 #4
John,
It is defined in another table:
CREATE TABLE [tblTravelDetail] (
[TravelDetailId] [int] IDENTITY (1, 1) NOT NULL ,
[MemberId] [bigint] NOT NULL ,
[Comments] [nvarchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[TravelEventId] [int] NOT NULL ,
[OrderId] [int] NULL ,
[ItemID] [nvarchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[PeriodID] [int] NULL ,
[CreatedDateTime] [datetime] NOT NULL ,
[Operator] [nvarchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
CONSTRAINT [PK_tblTravelDetail] PRIMARY KEY CLUSTERED
(
[TravelDetailId]
) ON [PRIMARY] ,
CONSTRAINT [FK_tblTravelDetail_tblTravelEvent] FOREIGN KEY
(
[TravelEventId]
) REFERENCES [tblTravelEvent] (
[TravelEventId]
)
) ON [PRIMARY]
GO

And here is my code on this table so far (That don't get the identity I
might add):

string strSQL5 = "INSERT INTO tblTravelDetail(MemberId, " +
" Comments, " +
" TravelEventId, " +
" OrderId, " +
" ItemID, " +
" PeriodID, " + //here is where we do the PeriodID lookup
calculation
" CreatedDateTime, " +
" Operator) " +
"VALUES ('" + deiinsertString2 + "', " +
" '" + deiinsertString3 + "', " +
" '" + eiinsertString1 + "', " +
" '" + deiinsertString5 + "', " +
" '" + deiinsertString6 + "', " +
" '" + deiinsertString7 + "', " +
" '" + deiinsertString8 + "', " +
" '" + insertString7 + "') " +
"SELECT @@IDENTITY AS 'Identity' ";

displayIdentity = Convert.ToString(rs5.Fields["Identity"].Value);

Thanks,
Trint

..Net programmer
tr***********@gmail.com

*** Sent via Developersdex http://www.developersdex.com ***
Nov 17 '05 #5
Trint Smith wrote:

This is a totally different table definition from your first post.
What is the question?

If it is the same error as earlier,
You must supply a value that exists in your foreign key table/column for
any foreign key columns.
tblTravelDetail.TravelEventId is your foreign key column.
You must supply a value for this column that exists in the
tblTravelEvent.TravelEventId column.

JB

John,
It is defined in another table:
CREATE TABLE [tblTravelDetail] (
[TravelDetailId] [int] IDENTITY (1, 1) NOT NULL ,
[MemberId] [bigint] NOT NULL ,
[Comments] [nvarchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[TravelEventId] [int] NOT NULL ,
[OrderId] [int] NULL ,
[ItemID] [nvarchar] (15) COLLATE SQL_Latin1_General_CP1_CI_AS NULL ,
[PeriodID] [int] NULL ,
[CreatedDateTime] [datetime] NOT NULL ,
[Operator] [nvarchar] (100) COLLATE SQL_Latin1_General_CP1_CI_AS NOT
NULL ,
CONSTRAINT [PK_tblTravelDetail] PRIMARY KEY CLUSTERED
(
[TravelDetailId]
) ON [PRIMARY] ,
CONSTRAINT [FK_tblTravelDetail_tblTravelEvent] FOREIGN KEY
(
[TravelEventId]
) REFERENCES [tblTravelEvent] (
[TravelEventId]
)
) ON [PRIMARY]
GO

And here is my code on this table so far (That don't get the identity I
might add):

string strSQL5 = "INSERT INTO tblTravelDetail(MemberId, " +
" Comments, " +
" TravelEventId, " +
" OrderId, " +
" ItemID, " +
" PeriodID, " + //here is where we do the PeriodID lookup
calculation
" CreatedDateTime, " +
" Operator) " +
"VALUES ('" + deiinsertString2 + "', " +
" '" + deiinsertString3 + "', " +
" '" + eiinsertString1 + "', " +
" '" + deiinsertString5 + "', " +
" '" + deiinsertString6 + "', " +
" '" + deiinsertString7 + "', " +
" '" + deiinsertString8 + "', " +
" '" + insertString7 + "') " +
"SELECT @@IDENTITY AS 'Identity' ";

displayIdentity = Convert.ToString(rs5.Fields["Identity"].Value);

Thanks,
Trint

.Net programmer
tr***********@gmail.com

*** Sent via Developersdex http://www.developersdex.com ***

Nov 17 '05 #6
This doesn't answer your immediate question, but it'll make your life a
whole lot easier when embedding SQL Statements into your CSharp code:

string strSQL5 = @"INSERT INTO tblTravelDetail
(
MemberID
, Comments
, TravelEventID
, ...
)
VALUES
(
@MemberID
, @Comments
, @TravelEventID
)";
cmd.Parameters.Add("@MemberID", deiinsertString2);
cmd.Parameters.Add("@Comments", deiinsertString3);
cmd.Parameters.Add("@TravelEventID", deiinsertString3);

The @ literal string operator prefix allows you to write multiline
strings. Very useful if you must embed SQL statements in your C# code.
Also, using parameterized SQL Statements is very important, not only
for security (it prevents SQL injection), but also for performance as
it increases the chance of your execution plan being reused by SQL
Server. See SQL Server's Books Online for sp_executesql, which is what
ADO.NET uses behind the scenes.

-Alan

Nov 17 '05 #7
Thanks Alan,
The only thing I need though at this point, is how can I get the last
Identity after an insert, using my c# app?
Thanks,
Trint

Alan Samet wrote:
This doesn't answer your immediate question, but it'll make your life a whole lot easier when embedding SQL Statements into your CSharp code:

string strSQL5 = @"INSERT INTO tblTravelDetail
(
MemberID
, Comments
, TravelEventID
, ...
)
VALUES
(
@MemberID
, @Comments
, @TravelEventID
)";
cmd.Parameters.Add("@MemberID", deiinsertString2);
cmd.Parameters.Add("@Comments", deiinsertString3);
cmd.Parameters.Add("@TravelEventID", deiinsertString3);

The @ literal string operator prefix allows you to write multiline
strings. Very useful if you must embed SQL statements in your C# code. Also, using parameterized SQL Statements is very important, not only
for security (it prevents SQL injection), but also for performance as
it increases the chance of your execution plan being reused by SQL
Server. See SQL Server's Books Online for sp_executesql, which is what ADO.NET uses behind the scenes.

-Alan


Nov 17 '05 #8

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

Similar topics

2
by: Tom Gao | last post by:
I have a problem. The project that I'm working on requires me to duplicate records. As in a series of records are entered into the system the user then click on a button to make these as...
3
by: trint | last post by:
Three tables (all new, no data) will receive data from dBase and be transposed into them...All three have auto generated IDENTITY columns and pk and fk constraints. Can someone provide me with an...
10
by: serge | last post by:
I am doing a little research on Google about this topic and I ran into this thread: ...
2
by: trint | last post by:
I am trying to insert a record, but I have to get an automatically created identity field at the same time to do this. string strSQL2 = "INSERT INTO tblTravelDetail(MemberID, " + "...
1
by: Philippe Lang | last post by:
Hello, What is the best method to debug a database that *sometimes* gets locked in the INSERT mode? ('ps -afx | grep postgres' shows me a process that is freezed in INSERT mode.) Thanks ...
20
by: John Bailo | last post by:
I have a c# program that loops through a table on a DB2 database. On each iteration it assigns data to values in the SqlParameter collection. The command text is an INSERT statement to a Sql...
8
by: nano2k | last post by:
Hi Shortly, I keep invoices in a table. Occasionally, someone will fire the execution of a stored procedure (SP) that performs several UPDATEs against (potentially) all invoices OLDER than a...
7
by: annecarterfredi | last post by:
I need to increase the length of CD column from char(5) to char(7) in DB2 V8.2. I am doing it this way: 1. create new_table with CD char(7) 2. do 'insert into new_table select * from...
10
by: teddysnips | last post by:
SQL Server 2000 (DDL below) If I try to run this code in QA: SET IDENTITY_INSERT tblAdminUsers ON INSERT INTO tblAdminUsers (fldUserID, fldUsername, fldPassword, fldFullname,
2
by: mivey4 | last post by:
Okay I have 2 tables: Table A - holds a list of new hardware serial numbers and their corresponding model (no constraints or indexes) Table B - holds a distinct list of current serial numbers...
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: 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
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
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
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.