Question

I am trying to get my head around ASP.NET 4.0 using C# as my programming language of choice. I am a good SQL developer and so plan to do most of my business logic in SQL. The application will only interact with the database through stored procedures.

In the aspx page I have a datasource for populating a dataview which calls sp 'season_get_byID' and for the update 'season_update' which takes a number of parameters,

The aspx source shows

    <asp:SqlDataSource ID="dsDetail" runat="server" 
    ConnectionString="<%$ ConnectionStrings:ADR %>" 
        SelectCommand="season_get_by_ID" SelectCommandType="StoredProcedure" 
        UpdateCommand="season_Update" UpdateCommandType="StoredProcedure">
        <SelectParameters>
            <asp:ControlParameter ControlID="GridView1" Name="ID" 
        PropertyName="SelectedValue" Type="Int32" />
        </SelectParameters>
        <UpdateParameters>
            <asp:Parameter Name="ID" Type="Int32" />
            <asp:Parameter Name="Code" Type="String" />
            <asp:Parameter Name="Description" Type="String" />
            <asp:Parameter DbType="Datetime" Name="StartDate" />
            <asp:Parameter DbType="Datetime" Name="EndDate" />
            <asp:Parameter Name="isActive" Type="Byte" />
            <asp:Parameter Name="isCurrent" Type="Byte" />
        </UpdateParameters>
    </asp:SqlDataSource>

Where each asp:Parameter maps to an @Param argument in the stored procedure - so far so good

The stored proc does some validation before trying to do the update. If the validations fail then a RAISERROR(@errmsg,10,1) is raised.

What I can't work out is where in the aspx.cs code behind file I would try to capture the errors and what the syntax should be: should it be in the after_update event handler and if so it does not appear to be part of e.exception. I know that some of the validation in this routine could be done using the validation classes but this is a learning excercise in the manipulating the environment rather than final production code i.e. I am trying to understand what is possible, rather than what is 'correct'

text for stored proc is below.

ALTER Procedure [dbo].[season_Update] 
    (
        @ID int,
        @Code nvarchar(10), 
    @Description nvarchar(50), 
    @StartDate date, 
    @EndDate date, 
    @isActive tinyint, 
    @isCurrent tinyint
)
as
DECLARE @ERR nvarchar(max) = ''

IF (SELECT count(*) FROM season WHERE ID = @ID) = 0 
BEGIN
SET @ERR = @ERR + '|Season Doesn''''t exist'
END

/*validate that season code and description are not blank*/
IF (@Code is null 
or
ltrim(rtrim(@Code)) = ''
)
BEGIN
set @ERR =+ '|Season Code cannot be blank'
END

IF @ERR = ''
BEGIN
IF (@Description is null 
    or
    ltrim(rtrim(@Description)) = ''
    )
BEGIN
    set @ERR =+ '|Season Description cannot be blank'
END

/*validate that the season code does not already exist on a different ID*/
IF (SELECT count(*) FROM season WHERE Code = ltrim(rtrim(upper(@CODE))) and 
       ID <> @ID) > 0 
BEGIN
    SET @ERR =+ '|Season Code ' + @Code + 'already exists'
END

/*validate that the start date is before the end date*/
IF @Startdate > @Enddate
BEGIN
    SET @ERR =+ '|Start Date cannot be Before End Date'
END
END

IF @ERR = ''
BEGIN
BEGIN TRY
UPDATE Season
    SET Code = ltrim(rtrim(upper(@Code))), 
        Description = Ltrim(rtrim(@Description)), 
        StartDate = @StartDate, 
        EndDate = @EndDate, 
        isActive = @isActive, 
        isCurrent = @isCurrent
WHERE
        ID = @ID    
END TRY
BEGIN CATCH
RAISERROR(N'There was a problem',10,1)
END CATCH
END

IF @ERR <> ''
BEGIN
RAISERROR(@ERR,10,1)
END
Was it helpful?

Solution

Microsoft have an article on this, How to Retrieve Values in SQL Server Stored Procedures, albeit in Visual Basic. This should give you an idea on how to resolve your problem.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top