I am working with SQL Server 2008 R2, C# and ASP.Net. I have a table that has a composite Primary key consisting of a ticket number, and the number of times that ticket appears in the table. The ticket frequency is calculated by the C# Code:
VTTTickets.InsertParameters[0].DefaultValue = VTTTTicketNoBox.Text;
string CommString = "SELECT COUNT(*) FROM [Tickets] WHERE [Ticket_No] = " +
VTTTTicketNoBox.Text;
string ConnString = ConfigurationManager.ConnectionStrings[1].ConnectionString;
OdbcConnection Conn = new OdbcConnection(ConnString);
Conn.Open();
OdbcCommand FooCommand = newOdbcCommand(CommString,Conn);
int FooVal = Convert.ToInt32(FooCommand.ExecuteScalar()) + 1;
VTTTickets.InsertParameters[1].DefaultValue = Convert.ToString(FooVal);
VTTTTicketNoBox.Text = "";
Conn.Close();
My tables constraint/etc code
CONSTRAINT [PK_Tickets] PRIMARY KEY CLUSTERED
([Ticket_No] ASC, [Ticket_Sub_No] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = ON,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
CONSTRAINT [Unique_Ticket_No] UNIQUE NONCLUSTERED
([Ticket_No] ASC)
WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = ON,
ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY],
) ON [PRIMARY]
GO
The reason for the unique constraint is because that part of the primary composite key is also a foreign key for two other tables. The error I keep getting is when it yells at me for there being a duplicate values present in the unique constrained column. This may occur once everything moves to production from development, as a ticket may be re-submitted.
Is there any way to make the foreign key without the unique key constraint? If not, how do I get around this problem?
