472,984 Members | 2,004 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,984 software developers and data experts.

bad sql error message

I have a datagrid that updates the table using a stored procedure. The stored procedure is confirmed to complete correctly, yet the sql data adapter is returning an error that my application is catching.

Everything within my stored procedure is contained within a transaction. When utilizing the sql profiler, we can see that the transaction successfully begins and commits. The data in the sql table is updated at this point.

My data adapter, daOnCall, returns an error on update. The error has to do with a primary key violation that does not exist. It is pointing to one of the records that is being updated within the stored proc, but there is no reason for that to be returned, granted that particular update statement can alter part of the primary key, being a date field. However, the data being passed to it is not being updated because it does not meet the where clause.

Here is the stored procedure:
<code>
CREATE Procedure procUpdateOnCallSchedule
@GroupId VARCHAR(25),
@StartDate DATETIME,
@EndDate DATETIME = NULL,
@BusinessHoursUser VARCHAR(30),
@AfterHoursUser VARCHAR(30) = NULL,
@Notes VARCHAR(1000) = NULL,
@OldGroupId VARCHAR(25),
@OldStartDate DATETIME,
@OldEndDate DATETIME = NULL,
@OldBusinessHoursUser VARCHAR(30),
@OldAfterHoursUser VARCHAR(30) = NULL,
@OldNotes VARCHAR(1000) = NULL
AS
BEGIN
BEGIN TRAN mainTran
-- IF THE EndDate is null, then make the end date the end of the day(11:59:59 PM)
-- one year out from the start date
IF @EndDate IS NULL
SET @EndDate = DATEADD(s, 86399, DATEADD(yy, 1, @StartDate))

-- update the record first and then make all of the necessary changes
-- this will keep from changing our record before we can update it
-- and possibly loosing the record all together.
UPDATE irOnCall
SET GroupId = @GroupId,
StartDate = @StartDate,
EndDate = @EndDate,
BusinessHoursUser = @BusinessHoursUser,
AfterHoursUser = @AfterHoursUser,
Notes = @Notes
WHERE GroupId = @OldGroupId
AND StartDate = @OldStartDate
AND (EndDate = @OldEndDate OR (@OldEndDate IS NULL AND EndDate IS NULL))
AND BusinessHoursUser = @OldBusinessHoursUser
AND (AfterHoursUser = @OldAfterHoursUser OR (@OldAfterHoursUser IS NULL AND AfterHoursUser IS NULL))
AND (Notes = @OldNotes OR (@OldNotes IS NULL AND Notes IS NULL))

-- check for oncall schedules that end after this schedule begins
PRINT 'OLD ENDS AFTER NEW STARTS'

UPDATE irOncall
SET EndDate = DATEADD(s, -1, @StartDate)
WHERE @StartDate BETWEEN StartDate AND EndDate
AND StartDate <> CONVERT(DATETIME, @StartDate)
AND GroupId = @GroupId

-- check for oncall schedules that start before the new schedule ends
PRINT 'NEW STARTS BEFORE OLD ENDS'

UPDATE irOnCall
SET StartDate = DATEADD(s, 1, @EndDate)
WHERE @EndDate BETWEEN StartDate AND EndDate
AND StartDate <> CONVERT(DATETIME, @StartDate)
AND StartDate <> CONVERT(DATETIME, @OldStartDate)
AND GroupId = @GroupId

IF @@ERROR = 0
COMMIT TRAN mainTran
ELSE
ROLLBACK TRAN mainTran
END
</code>

Here is the code behind that is generating the errors:
<code>
Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles DataGrid1.ItemCommand
Select Case e.CommandName
Case "Edit"
DataGrid1.EditItemIndex = e.Item.ItemIndex
DataGrid1.ShowFooter = False
Case "Cancel"
DataGrid1.EditItemIndex = -1
DataGrid1.ShowFooter = True
Case "Insert"
' insert command logic
Case "Update"
Try
LoadOnCallDataAdapter()
Dim dr As dsOnCall.irOnCallRow = DsOnCall1.irOnCall.Item(e.Item.DataSetIndex)

SqlUpdateCommand1.Parameters("@OldGroupId").Value = ddlGroup.SelectedValue
SqlUpdateCommand1.Parameters("@GroupId").Value = ddlGroup.SelectedValue
SqlUpdateCommand1.Parameters("@OldStartDate").Valu e = dr("StartDate")
SqlUpdateCommand1.Parameters("@OldEndDate").Value = dr("EndDate")
SqlUpdateCommand1.Parameters("@OldBusinessHoursUse r").Value = dr("BusinessHoursUser")
SqlUpdateCommand1.Parameters("@OldAfterHoursUser") .Value = dr("AfterHoursUser")
SqlUpdateCommand1.Parameters("@OldNotes").Value = dr("Notes")

Dim myText As TextBox
myText = e.Item.FindControl("txtStartDateEdit")
dr.StartDate = myText.Text + " 12:00:00 AM"
SqlUpdateCommand1.Parameters("@StartDate").Value = myText.Text + " 12:00:00 AM"

myText = e.Item.FindControl("txtEndDateEdit")
If myText.Text <> "" Then
dr.EndDate = myText.Text + " 11:59:59 PM"
SqlUpdateCommand1.Parameters("@EndDate").Value = myText.Text + " 11:59:59 PM"
Else
dr.EndDate = Nothing
SqlUpdateCommand1.Parameters("@EndDate").Value = Nothing
End If

Dim myList As DropDownList
myList = e.Item.FindControl("ddlOfficeHoursEdit")
dr.BusinessHoursUser = myList.SelectedValue
SqlUpdateCommand1.Parameters("@BusinessHoursUser") .Value = myList.SelectedValue

myList = e.Item.FindControl("ddlAfterHoursEdit")
If myList.SelectedValue <> "" Then
dr.AfterHoursUser = myList.SelectedValue
SqlUpdateCommand1.Parameters("@AfterHoursUser").Va lue = myList.SelectedValue
Else
dr.AfterHoursUser = Nothing
SqlUpdateCommand1.Parameters("@AfterHoursUser").Va lue = Nothing
End If

myText = e.Item.FindControl("txtNotesEdit")
If myText.Text <> "" Then
dr.Notes = myText.Text
SqlUpdateCommand1.Parameters("@Notes").Value = myText.Text
Else
dr.Notes = Nothing
SqlUpdateCommand1.Parameters("@Notes").Value = Nothing
End If

daOnCall.Update(DsOnCall1, "irOnCall")
DsOnCall1.Clear()
DataGrid1.EditItemIndex = -1
DataGrid1.ShowFooter = True
lblMessage.Text = "Record Updated"
Catch ex As Exception
lblMessage.Text = "Error Updating: " & ex.Message
End Try
End Select
BindDetailData()
End Sub
</code>

Here is the error message being caught in the code-behind page:
Column 'GroupId, StartDate' is constrained to be unique. Value 'EBIZ, 9/12/2005 12:00:00 AM' is already present.

When the page loads after the exception handling, this is the error that is returned:
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

Let me know if you want some test data to work with as well. This is really killing me. I have been working hand-in-hand with my dba to get this going.

Nov 19 '05 #1
1 1556
you have a constaint that says 'GroupId, StartDate' must be unique. in:

UPDATE irOnCall
SET GroupId = @GroupId,
StartDate = @StartDate,
EndDate = @EndDate,
BusinessHoursUser = @BusinessHoursUser,
AfterHoursUser = @AfterHoursUser,
Notes = @Notes
WHERE GroupId = @OldGroupId
AND StartDate = @OldStartDate
AND (EndDate = @OldEndDate OR (@OldEndDate IS NULL AND EndDate IS NULL))
AND BusinessHoursUser = @OldBusinessHoursUser
AND (AfterHoursUser = @OldAfterHoursUser OR (@OldAfterHoursUser IS NULL AND AfterHoursUser IS NULL))
AND (Notes = @OldNotes OR (@OldNotes IS NULL AND Notes IS NULL))
you never check if the new startdate is in already in use.

also you only check @@error at the end, but you must after every statement, as it only applies to the last statement. so if your first statement fails, but you continue on, run the other updates and commit.

also if your rollback will fail, if an auto rollback happens previously. you should check @@trancount before doing the commit or rollback.
-- bruce (sqlwork.com)

"Jeremy Ames" <an*******@whereever.com> wrote in message news:%2****************@TK2MSFTNGP14.phx.gbl...
I have a datagrid that updates the table using a stored procedure. The stored procedure is confirmed to complete correctly, yet the sql data adapter is returning an error that my application is catching.

Everything within my stored procedure is contained within a transaction. When utilizing the sql profiler, we can see that the transaction successfully begins and commits. The data in the sql table is updated at this point.

My data adapter, daOnCall, returns an error on update. The error has to do with a primary key violation that does not exist. It is pointing to one of the records that is being updated within the stored proc, but there is no reason for that to be returned, granted that particular update statement can alter part of the primary key, being a date field. However, the data being passed to it is not being updated because it does not meet the where clause.

Here is the stored procedure:
<code>
CREATE Procedure procUpdateOnCallSchedule
@GroupId VARCHAR(25),
@StartDate DATETIME,
@EndDate DATETIME = NULL,
@BusinessHoursUser VARCHAR(30),
@AfterHoursUser VARCHAR(30) = NULL,
@Notes VARCHAR(1000) = NULL,
@OldGroupId VARCHAR(25),
@OldStartDate DATETIME,
@OldEndDate DATETIME = NULL,
@OldBusinessHoursUser VARCHAR(30),
@OldAfterHoursUser VARCHAR(30) = NULL,
@OldNotes VARCHAR(1000) = NULL
AS
BEGIN
BEGIN TRAN mainTran
-- IF THE EndDate is null, then make the end date the end of the day(11:59:59 PM)
-- one year out from the start date
IF @EndDate IS NULL
SET @EndDate = DATEADD(s, 86399, DATEADD(yy, 1, @StartDate))

-- update the record first and then make all of the necessary changes
-- this will keep from changing our record before we can update it
-- and possibly loosing the record all together.
UPDATE irOnCall
SET GroupId = @GroupId,
StartDate = @StartDate,
EndDate = @EndDate,
BusinessHoursUser = @BusinessHoursUser,
AfterHoursUser = @AfterHoursUser,
Notes = @Notes
WHERE GroupId = @OldGroupId
AND StartDate = @OldStartDate
AND (EndDate = @OldEndDate OR (@OldEndDate IS NULL AND EndDate IS NULL))
AND BusinessHoursUser = @OldBusinessHoursUser
AND (AfterHoursUser = @OldAfterHoursUser OR (@OldAfterHoursUser IS NULL AND AfterHoursUser IS NULL))
AND (Notes = @OldNotes OR (@OldNotes IS NULL AND Notes IS NULL))

-- check for oncall schedules that end after this schedule begins
PRINT 'OLD ENDS AFTER NEW STARTS'

UPDATE irOncall
SET EndDate = DATEADD(s, -1, @StartDate)
WHERE @StartDate BETWEEN StartDate AND EndDate
AND StartDate <> CONVERT(DATETIME, @StartDate)
AND GroupId = @GroupId

-- check for oncall schedules that start before the new schedule ends
PRINT 'NEW STARTS BEFORE OLD ENDS'

UPDATE irOnCall
SET StartDate = DATEADD(s, 1, @EndDate)
WHERE @EndDate BETWEEN StartDate AND EndDate
AND StartDate <> CONVERT(DATETIME, @StartDate)
AND StartDate <> CONVERT(DATETIME, @OldStartDate)
AND GroupId = @GroupId

IF @@ERROR = 0
COMMIT TRAN mainTran
ELSE
ROLLBACK TRAN mainTran
END
</code>

Here is the code behind that is generating the errors:
<code>
Private Sub DataGrid1_ItemCommand(ByVal source As Object, ByVal e As System.Web.UI.WebControls.DataGridCommandEventArgs ) Handles DataGrid1.ItemCommand
Select Case e.CommandName
Case "Edit"
DataGrid1.EditItemIndex = e.Item.ItemIndex
DataGrid1.ShowFooter = False
Case "Cancel"
DataGrid1.EditItemIndex = -1
DataGrid1.ShowFooter = True
Case "Insert"
' insert command logic
Case "Update"
Try
LoadOnCallDataAdapter()
Dim dr As dsOnCall.irOnCallRow = DsOnCall1.irOnCall.Item(e.Item.DataSetIndex)

SqlUpdateCommand1.Parameters("@OldGroupId").Value = ddlGroup.SelectedValue
SqlUpdateCommand1.Parameters("@GroupId").Value = ddlGroup.SelectedValue
SqlUpdateCommand1.Parameters("@OldStartDate").Valu e = dr("StartDate")
SqlUpdateCommand1.Parameters("@OldEndDate").Value = dr("EndDate")
SqlUpdateCommand1.Parameters("@OldBusinessHoursUse r").Value = dr("BusinessHoursUser")
SqlUpdateCommand1.Parameters("@OldAfterHoursUser") .Value = dr("AfterHoursUser")
SqlUpdateCommand1.Parameters("@OldNotes").Value = dr("Notes")

Dim myText As TextBox
myText = e.Item.FindControl("txtStartDateEdit")
dr.StartDate = myText.Text + " 12:00:00 AM"
SqlUpdateCommand1.Parameters("@StartDate").Value = myText.Text + " 12:00:00 AM"

myText = e.Item.FindControl("txtEndDateEdit")
If myText.Text <> "" Then
dr.EndDate = myText.Text + " 11:59:59 PM"
SqlUpdateCommand1.Parameters("@EndDate").Value = myText.Text + " 11:59:59 PM"
Else
dr.EndDate = Nothing
SqlUpdateCommand1.Parameters("@EndDate").Value = Nothing
End If

Dim myList As DropDownList
myList = e.Item.FindControl("ddlOfficeHoursEdit")
dr.BusinessHoursUser = myList.SelectedValue
SqlUpdateCommand1.Parameters("@BusinessHoursUser") .Value = myList.SelectedValue

myList = e.Item.FindControl("ddlAfterHoursEdit")
If myList.SelectedValue <> "" Then
dr.AfterHoursUser = myList.SelectedValue
SqlUpdateCommand1.Parameters("@AfterHoursUser").Va lue = myList.SelectedValue
Else
dr.AfterHoursUser = Nothing
SqlUpdateCommand1.Parameters("@AfterHoursUser").Va lue = Nothing
End If

myText = e.Item.FindControl("txtNotesEdit")
If myText.Text <> "" Then
dr.Notes = myText.Text
SqlUpdateCommand1.Parameters("@Notes").Value = myText.Text
Else
dr.Notes = Nothing
SqlUpdateCommand1.Parameters("@Notes").Value = Nothing
End If

daOnCall.Update(DsOnCall1, "irOnCall")
DsOnCall1.Clear()
DataGrid1.EditItemIndex = -1
DataGrid1.ShowFooter = True
lblMessage.Text = "Record Updated"
Catch ex As Exception
lblMessage.Text = "Error Updating: " & ex.Message
End Try
End Select
BindDetailData()
End Sub
</code>

Here is the error message being caught in the code-behind page:
Column 'GroupId, StartDate' is constrained to be unique. Value 'EBIZ, 9/12/2005 12:00:00 AM' is already present.

When the page loads after the exception handling, this is the error that is returned:
Failed to enable constraints. One or more rows contain values violating non-null, unique, or foreign-key constraints.

Let me know if you want some test data to work with as well. This is really killing me. I have been working hand-in-hand with my dba to get this going.

Nov 19 '05 #2

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

Similar topics

10
by: | last post by:
I am accessing the same error-containing ASP page on an ISP server using w2k IE6 but with different effect. On the first computer I get several line of HTML outputed by ASP, shown correctly by...
9
by: Mairhtin O'Feannag | last post by:
Hello, We have two machines we wish to use DPF. They are both RH ES 2.1, with DB2 8.2. I read the documentation CAREFULLY, and added the following line to my db2nodes.cfg file : 1 egret 0
6
by: Squirrel | last post by:
I have a command button on a subform to delete a record. The only statement in the subroutine is: DoCmd.RunCommand acCmdDeleteRecord The subform's recordsource is "select * from tblVisit order...
8
by: Rod | last post by:
I have been working with ASP.NET 1.1 for quite a while now. For some reason, opening some ASP.NET applications we wrote is producing the following error message: "The Web server reported...
10
by: Shawn | last post by:
JIT Debugging failed with the following error: Access is denied. JIT Debugging was initiated by the following account 'PLISKEN\ASPNET' I get this messag in a dialog window when I try to open an...
16
by: | last post by:
Hi all, I have a website running on beta 2.0 on server 2003 web sp1 and I keep getting the following error:- Error In:...
2
by: f rom | last post by:
----- Forwarded Message ---- From: Josiah Carlson <jcarlson@uci.edu> To: f rom <etaoinbe@yahoo.com>; wxpython-users@lists.wxwidgets.org Sent: Monday, December 4, 2006 10:03:28 PM Subject: Re: ...
2
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
hyperpau
by: hyperpau | last post by:
Before anything else, I am not a very technical expert when it comes to VBA coding. I learned most of what I know by the excellent Access/VBA forum from bytes.com (formerly thescripts.com). Ergo, I...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
tracyyun
by: tracyyun | last post by:
Hello everyone, I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
4
NeoPa
by: NeoPa | last post by:
Hello everyone. I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report). I know it can be done by selecting :...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM) Please note that the UK and Europe revert to winter time on...
0
NeoPa
by: NeoPa | last post by:
Introduction For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
0
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
4
by: GKJR | last post by:
Does anyone have a recommendation to build a standalone application to replace an Access database? I have my bookkeeping software I developed in Access that I would like to make available to other...

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.