Click here to Skip to main content
15,895,807 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
Hello all,

I am writing a class to create a policy with the stored procedure below:
SQL
CREATE PROCEDURE CreatePolicy   
    @CategoryID int,
    @PolicyTitle nvarchar(100),
    @PolicyContent varchar(1024),
    @retvalue varchar(100) output
   
    AS
   
    IF EXISTS(Select * FROM Policy WHERE CategoryID = @CategoryID)
   
    BEGIN
   
    Set @retvalue = 'Category Policy already exist'
    END
    ELSE
    BEGIN
   
    INSERT INTO Policy   
    (      
        CategoryID,
        PolicyTitle,
        PolicyContent       
    )      
    VALUES   
    (   
        @CategoryID,
        @PolicyTitle,
        @PolicyContent       
    )
    Set @retvalue = 'Record added'
   
    END

And the class below:
VB
Public Shared Function CreateAuditPolicy(ByVal title As String, ByVal content As String, _
                                                ByVal policycategory As Int32, ByVal retmsg As String) As Boolean

        Dim result As Boolean
        Using con As SqlConnection = New SqlConnection(My.Settings.myConnectionStrings)
            Using cmd As SqlCommand = New SqlCommand()
                With cmd
                    .CommandText = "CreatePolicy"
                    .CommandType = CommandType.StoredProcedure
                    .Parameters.AddWithValue("@PolicyTitle", title)
                    .Parameters.AddWithValue("@PolicyContent", content)
                    .Parameters.AddWithValue("@CategoryID", policycategory)
                    .Parameters.Add("@retvalue", SqlDbType.VarChar, 100)
                    .Parameters("@retvalue").Direction = ParameterDirection.Output
                    .Connection = con
                    .Connection.Open()

                End With
                Dim mytransaction As SqlTransaction = con.BeginTransaction()
                Try

                    cmd.Transaction = mytransaction
                    cmd.ExecuteNonQuery()
                    mytransaction.Commit()
                    result = True
                    retmsg = cmd.Parameters("@retvalue").SqlValue.ToString()

                Catch ex As Exception
                    mytransaction.Rollback()
                    result = False

                    Throw
                End Try
            End Using
        End Using

        Return result
    End Function

Then here is how I called the class in business layer:
SQL
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click
Dim msg As String = ""
        If AdmonAuditClass.CreateAuditPolicy(Me.txtTitle.Text.Trim(), Me.txtContent.Text.Trim(), Me.PolicyCategoryComboBox.SelectedValue, msg) Then
            '  Dim obj As AdmonAuditClass = New AdmonAuditClass()
            ' " Your data was successfullt saved"
            Me.txtContent.Text = ""
            Me.txtTitle.Text = ""
            AdmonUtilityClass.ShowErrorProvider(btnSave, True, msg, My.Resources.Information, ErrorIconAlignment.MiddleRight)

        Else
            AdmonUtilityClass.ShowErrorProvider(btnSave, True, msg, My.Resources.ErrorIcon, ErrorIconAlignment.MiddleRight)
        End If
    End Sub

My problem is that it doesn't return the error message at all but it does when I use code behind - without class.

Somebody please help!
Posted
Updated 10-Jun-10 6:52am
v2
Comments
Sandeep Mewara 10-Jun-10 12:53pm    
Pre tags are your friend, use it for formatting code next time you post question.

1 solution

Hi there
As far as I can tell the CreateAuditPolicy Function only executes one query. Therefore it is not necessary to use a transaction.I also do not think it necessary for the procedure to return anything. If there is an error the Using directive should ensure that the connection is closed properly and the error passed on to the calling procedure. So this is how I would set it up :
VB.NET
Public Shared Sub CreateAuditPolicy(ByVal title As String, ByVal content As String, ByVal policycategory As Int32)
        Using con As SqlConnection = New SqlConnection("myConnectionStrings")
            Using cmd As SqlCommand = New SqlCommand()
                With cmd
                    .CommandText = "CreatePolicy"
                    .CommandType = CommandType.StoredProcedure
                    .Parameters.AddWithValue("@PolicyTitle", title)
                    .Parameters.AddWithValue("@PolicyContent", content)
                    .Parameters.AddWithValue("@CategoryID", policycategory)
                    .Parameters.Add("@retvalue", SqlDbType.VarChar, 100)
                    .Parameters("@retvalue").Direction = ParameterDirection.Output
                    .Connection = con
                End With
                'No need of transaction for single query
                con.Open()
                cmd.ExecuteNonQuery()
            End Using
        End Using
    End Sub

Then in your presentation layer your button Save would use a Try,Catch End Try Block to catch the error and inform the user:
VB.NET
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        Dim msg As String = ""
        Try
            AdmonAuditClass.CreateAuditPolicy(Me.txtTitle.Text.Trim(), Me.txtContent.Text.Trim(), Me.PolicyCategoryComboBox.SelectedValue)
            MsgBox(" Your data was successfully saved") 'If error occurs it will not reach here
            Me.txtContent.Text = ""
            Me.txtTitle.Text = ""
        Catch ex As SqlException
            AdmonUtilityClass.ShowErrorProvider(btnSave, True, msg, " My.Resources.ErrorIcon", ErrorIconAlignment.MiddleRight)
        End Try

    End Sub


Anyway there is so many different opinions on error handling between layers. This is just how I would do it

happy Coding
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900