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 8647069
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T12:58:24+00:00 2026-06-12T12:58:24+00:00

I wrote C# code which returns a DataTable . All parameters i.e. Connection, sQuery

  • 0

I wrote C# code which returns a DataTable. All parameters i.e. Connection, sQuery have proper values.

Still I am getting an error:

In the name the owner of a procedure must be specified, this improves performance

I googled it but found nothing.

This is my code:

public static DataTable getATM(ref SqlConnection Connection)
{
            DataTable dtReturn;
            string sQuery = "";
            try
            {
                sQuery = "Select ATM From ATM Where Bank=1";
                using (SqlStoredProcedure sspObj = new SqlStoredProcedure(sQuery, Connection, CommandType.Text))
                {
                    dtReturn = sspObj.ExecuteDataTable();

                    sspObj.Dispose();
                }
            }
            catch (Exception xObj)
            {
                dtReturn = new DataTable();
            }

            return dtReturn;
}

SqlStoredProcedure.cs:

using System;
using System.Data;
using System.Data.SqlClient;
using System.Xml;
using System.Diagnostics;

namespace ReflectionIT.Common.Data.SqlClient {

    public class SqlStoredProcedure : IDisposable {
        public static readonly TraceSource TraceSource = new TraceSource("SqlStoredProcedure");
        private static int _eventId = 0;

        private const string _returnValue = "ReturnValue";
        private SqlCommand _command;
        private bool _connectionOpened = false;

        public SqlStoredProcedure(string name, SqlConnection connection, CommandType comtype)
            : this(name, connection, null, comtype) {
        }

        public SqlStoredProcedure(string name, SqlConnection connection, SqlTransaction transaction, CommandType comtype) {
            if (name.IndexOf('.') == -1) {
                throw new ArithmeticException("In the name the owner of a procedure must be specified, this improves performance");
            }
            _command = new SqlCommand(name, connection, transaction);
            _command.CommandTimeout = 0;
            _command.CommandType = comtype;
            AddReturnValue();
        }

        public void Dispose() {
            if (_command != null) {
                _command.Dispose();
                _command = null;
            }
        }

        virtual public string Name {
            get { return _command.CommandText; }
            set { _command.CommandText = value; }
        }

               virtual public int Timeout {
            get { return _command.CommandTimeout; }
            set { _command.CommandTimeout = value; }
        }
        virtual public SqlCommand Command {
            get { return _command; }
        }

        virtual public SqlConnection Connection {
            get { return _command.Connection; }
            set { _command.Connection = value; }
        }

        virtual public SqlTransaction Transaction {
            get { return _command.Transaction; }
            set { _command.Transaction = value; }
        }

        virtual public SqlParameterCollection Parameters {
            get { return _command.Parameters; }
        }

        virtual public int ReturnValue {
            get { return (int)_command.Parameters[_returnValue].Value; }
        }

        virtual public SqlParameter AddParameter(string parameterName,
            SqlDbType dbType,
            int size,
            ParameterDirection direction) {
            SqlParameter p;

            if (size > 0) {
                p = new SqlParameter(parameterName, dbType, size);
            } else {
                // size is automacally detected using dbType
                p = new SqlParameter(parameterName, dbType);
            }

            p.Direction = direction;

            Parameters.Add(p);
            return p;
        }

        virtual public SqlParameter AddParameterWithValue(string parameterName,
            SqlDbType dbType,
            int size,
            ParameterDirection direction,
            object value) {

            SqlParameter p = this.AddParameter(parameterName, dbType, size, direction);

            if (value == null) {
                value = DBNull.Value;
            }

            p.Value = value;

            return p;
        }

        virtual public SqlParameter AddParameterWithStringValue(string parameterName,
            SqlDbType dbType,
            int size,
            ParameterDirection direction,
            string value,
            bool emptyIsDBNull) {

            SqlParameter p = this.AddParameter(parameterName, dbType, size, direction);

            if (value == null) {
                p.Value = DBNull.Value;
            } else {
                value = value.TrimEnd(' ');
                if (emptyIsDBNull && value.Length == 0) {
                    p.Value = DBNull.Value;
                } else {
                    p.Value = value;
                }
            }

            return p;
        }

        virtual protected SqlParameter AddReturnValue() {
            SqlParameter p = Parameters.Add(
                new SqlParameter(_returnValue,
                    SqlDbType.Int,
                /* int size */ 4,
                    ParameterDirection.ReturnValue,
                /* bool isNullable */ false,
                /* byte precision */ 0,
                /* byte scale */ 0,
                /* string srcColumn */ string.Empty,
                    DataRowVersion.Default,
                /* value */ null));

            return p;
        }

        virtual public int ExecuteNonQuery() {
            int rowsAffected = -1;

            try {
                Prepare("ExecuteNonQuery");

                rowsAffected = _command.ExecuteNonQuery();

                TraceResult("RowsAffected = " + rowsAffected.ToString());
            } catch (SqlException e) {
                throw TranslateException(e);
            } finally {
                CloseOpenedConnection();
            }
            return rowsAffected;
        }

        virtual public SqlDataReader ExecuteReader() {
            SqlDataReader reader;
            try {
                Prepare("ExecuteReader");

                reader = _command.ExecuteReader();

                TraceResult(null);
            } catch (SqlException e) {
                throw TranslateException(e);
            } finally {
                CloseOpenedConnection();
            }
            return reader;
        }

        virtual public SqlDataReader ExecuteReader(CommandBehavior behavior) {
            SqlDataReader reader;
            try {
                Prepare("ExecuteReader");

                reader = _command.ExecuteReader(behavior);

                TraceResult(null);
            } catch (SqlException e) {
                throw TranslateException(e);
            } finally {
                CloseOpenedConnection();
            }
            return reader;
        }

        virtual public object ExecuteScalar() {
            object val = null;

            try {
                Prepare("ExecuteScalar");

                val = _command.ExecuteScalar();

                TraceResult("Scalar Value = " + Convert.ToString(val));
            } catch (SqlException e) {
                throw TranslateException(e);
            } finally {
                CloseOpenedConnection();
            }
            return val;
        }

        virtual public XmlReader ExecuteXmlReader() {
            XmlReader reader;

            try {
                Prepare("ExecuteXmlReader");

                reader = _command.ExecuteXmlReader();

                TraceResult(null);
            } catch (SqlException e) {
                throw TranslateException(e);
            } finally {
                CloseOpenedConnection();
            }
            return reader;
        }

        virtual public DataSet ExecuteDataSet() {
            DataSet dataset = new DataSet();

            this.ExecuteDataSet(dataset);

            return dataset;
        }

        virtual public DataSet ExecuteDataSet(DataSet dataSet) {
            try {
                Prepare("ExecuteDataSet");

                SqlDataAdapter a = new SqlDataAdapter(this.Command);
                a.Fill(dataSet);

                TraceResult("# Tables in DataSet = " + dataSet.Tables.Count);
            } catch (SqlException e) {
                throw TranslateException(e);
            } finally {
                CloseOpenedConnection();
            }
            return dataSet;
        }

        virtual public DataTable ExecuteDataTable() {
            DataTable dt = null;
            try {
                Prepare("ExecuteDataTable");

                SqlDataAdapter a = new SqlDataAdapter(this.Command);
                dt = new DataTable();
                a.Fill(dt);

                TraceResult("# Rows in DataTable = " + dt.Rows.Count);
            } catch (SqlException e) {
                throw TranslateException(e);
            } finally {
                CloseOpenedConnection();
            }

            return dt;
        }

        protected Exception TranslateException(SqlException ex) {
            Exception dalException = null;

            SqlStoredProcedure.TraceSource.TraceEvent(TraceEventType.Error, _eventId, "{0} throwed exception: {1}", this.Name, ex.ToString());

            foreach (SqlError error in ex.Errors) {
                if (error.Number >= 50000) {
                    dalException = new DalException(error.Message, ex);
                }
            }

            if (dalException == null) {
                switch (ex.Number) {
                    case 17:                    
                    case 4060:                    
                    case 18456:              
                        dalException = new DalLoginException(ex.Message, ex);
                        break;
                    case 547:                      
                        dalException = new DalForeignKeyException(ex.Message, ex);
                        break;
                    case 1205:                  
                        dalException = new DalDeadLockException(ex.Message, ex);
                        break;
                    case 2627:
                    case 2601:                        
                        dalException = new DalUniqueConstraintException(ex.Message, ex);
                        break;
                    default:                  
                        dalException = new DalException(ex.Message, ex);
                        break;
                }
            }         
            return dalException;
        }

        protected void Prepare(string executeType) {
            _eventId++;
            if (_eventId > ushort.MaxValue) {
                _eventId = 0;
            }

            SqlStoredProcedure.TraceSource.TraceEvent(TraceEventType.Information, _eventId, "{0}: {1}", executeType, this.Name);

            TraceParameters(true);

            if (_command.Connection.State != ConnectionState.Open) {
                _command.Connection.Open();
                _connectionOpened = true;
            }
        }

        private void TraceParameters(bool input) {
            if (SqlStoredProcedure.TraceSource.Switch.ShouldTrace(TraceEventType.Verbose) && this.Parameters.Count > 0) {
                foreach (SqlParameter p in this.Parameters) {
                    bool isInput = p.Direction != ParameterDirection.ReturnValue && p.Direction != ParameterDirection.Output;
                    bool isOutput = p.Direction != ParameterDirection.Input;
                    if ((input && isInput) || (!input && isOutput)) {
                        SqlStoredProcedure.TraceSource.TraceEvent(TraceEventType.Verbose, _eventId, "SqlParamter: Name = {0}, Value = '{1}', Type = {2}, Size = {3}", p.ParameterName, p.Value, p.DbType, p.Size);
                    }
                }
            }
        }

        protected void CloseOpenedConnection() {
            if ((_command.Connection.State == ConnectionState.Open) & _connectionOpened)
                _command.Connection.Close();
        }

        protected void TraceResult(string result) {
            if (result != null) {
                SqlStoredProcedure.TraceSource.TraceEvent(TraceEventType.Verbose, _eventId, "Result: {0}", result);
            }

            TraceParameters(false);
        }

    }
}
  • 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-12T12:58:25+00:00Added an answer on June 12, 2026 at 12:58 pm

    SqlStoredProcedure() is checking that there is a dot in your stored procedure name and if there isn’t throwing that exception, so prefix your stored procedure name with “dbo.” or whatever it is e.g.:

    dbo.myProcedureName
    

    It improves performance in the sense that SQL doesn’t have to search all the users to find the proc first.

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

Sidebar

Related Questions

I have wrote the code below which was taken from Java How to program
I have been refactoring throwaway code which I wrote some years ago in a
I wrote an extension method which returns me 2-dimensional array of YUV values from
I have already developed an application which returns DataTable everywhere. Now my client wants
I wrote my bind function which returns a nullary functor because I don't have
I have the following code which assigns values to a datarow and and adds
I wrote some code which monitors the output of Stopwatch using a tight loop.
So, I wrote a code which uses some Microsoft Sql server dlls, these dlls
I'm working with the jQuery Validation plugin and I wrote the following code which
I wrote code to get name of printer which is installed in my system.but

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.