Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8173515
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T22:11:03+00:00 2026-06-06T22:11:03+00:00

This function is in a Class file in App_Code folder Public Shared Function CRUD(ByVal

  • 0

This function is in a Class file in App_Code folder

 Public Shared Function CRUD(ByVal _sql As String, ByVal _parameterNames() As String, ByVal _parameterVals() As String) As Integer
        Dim _noOfRowsAffected As Integer
        Dim _connection As SqlConnection = Global.Connection.GetDbConnection()
        Dim _command As New SqlCommand(_sql, _connection)

        Try
            If _parameterNames IsNot Nothing Then
                For i = 0 To _parameterNames.Length - 1
                    _command.Parameters.AddWithValue(_parameterNames(i), _parameterVals(i))
                Next
            End If

            _noOfRowsAffected = _command.ExecuteNonQuery()
        Catch ex As Exception
            'MsgBox(ex.Message)
            _noOfRowsAffected = -1
        Finally
            If _connection.State = ConnectionState.Open Then
                _connection.Close()
                _connection.Dispose()
                _command.Dispose()
            End If
        End Try

        Return _noOfRowsAffected
    End Function

This code is in aspx.vb page

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        Dim _parameterNames(6), _parameterVals(6) As String
        _parameterNames(0) = "@Name"
        _parameterVals(0) = txtCoachingName.Text
        _parameterNames(1) = "@Address"
        _parameterVals(1) = txtCoachingAddress.Text
        _parameterNames(2) = "@Mob1"
        _parameterVals(2) = txtCoachingMob1.Text
        _parameterNames(3) = "@Mob2"
        _parameterVals(3) = txtCoachingMob2.Text
        _parameterNames(4) = "@LLine"
        _parameterVals(4) = txtCoachingLLine.Text
        _parameterNames(5) = "@Established"
        _parameterVals(5) = ddlCoachingEstablished.SelectedValue
        _parameterNames(6) = "@DemoVideo"
        _parameterVals(6) = txtCoachingDemoVideo.Text
        _parameterNames(7) = "@Password"
        _parameterVals(7) = txtCoachingPassword.Text
         Try
        DataAccess.CRUD("UPDATE CoachingDetails SET Name=@Name,Address=@Address,Mob1=@Mob1,Mob2=@Mob2,L_Line=@LLine,Established=@Established,Demo_Video=@DemoVideo,Password=@Password,Step_Completed='True',Time_Stamp='" & Date.Now & "'", _parameterNames, _parameterVals)
    Catch ex As Exception

    End Try

Now i want to catch the Primary Key Violation exception in my aspx.vb page..but i do not get the exception in the aspx.vb page as the error is caught by the class function.So, how do i get the exception from the class file to the aspx.vb file??
End Sub

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-06T22:11:04+00:00Added an answer on June 6, 2026 at 10:11 pm

    Rethrow. (http://www.selfelected.com/rethrow/)

    Try
        ....
    Catch ex As Exception 
        do some stuff with ex
        Throw    ' important to not use ex as parameter as that would start a new exception stack.
    Finally
        do some other stuff
    End Try
    

    Since you don’t do anything in the catch just skip it.

    Try
        ....
    Finally
        do some other stuff
    End Try
    

    To make the code nicer read up on Using. With Using you can have the compiler insert the Try/Finally implicitly for you. The code will be something like:

    Using connection As SqlConnection = Global.Connection.GetDbConnection()
        Using command As New SqlCommand(sql, connection)
            If parameterNames IsNot Nothing Then
                For i = 0 To parameterNames.Length - 1
                    command.Parameters.AddWithValue(parameterNames(i), parameterVals(i))
                Next
            End If
            noOfRowsAffected = _command.ExecuteNonQuery()
        End Using
    End Using
    

    Please note that I made all your variables local. This might not be possible for you since you know a lot more about your code than I do… (but the code suggested that your variables where class variables but you treated them as local anyway.)

    Also check out Dapper. Dapper is what Stack overflow runs on. It is a nice small lib for making writing sql calls easier to write and read.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know the prototype and arguements this function takes id class_createInstance(Class cls, size_t extraBytes)
I have an inline member function defined under class MyClass int MyClass::myInlineFunction(); This function
With this code function someFunction(classParam:Class):Boolean { // how to know if classParam implements some
I have a class: function RustEditor() { this.init = function() { var saveButton =
This is somewhat similiar to this : pthread function from a class But the
When I define this function, template<class A> set<A> test(const set<A>& input) { return input;
I am creating a class for an application backbone and I need this function
This recursive function ( search_Bases ) would hopefully iterate through each base class and
Before Consider to have a class and a global function: This is, for example,
In my code, I use a public load_snippet function of a class when I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.