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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T21:07:32+00:00 2026-05-14T21:07:32+00:00

public IEnumerable<T> ExecuteStoredProcedure<T>(params object[] parameters) { Type genericType = typeof(T); string commandthing = genericType.Name.Replace(Result,

  • 0
public IEnumerable<T> ExecuteStoredProcedure<T>(params object[] parameters)
        {
            Type genericType = typeof(T);

            string commandthing = genericType.Name.Replace("Result", "");
            //_db is my Linq To Sql database
            return _db.ExecuteQuery<T>(commandthing, parameters).AsEnumerable();
        }

The stored procedure is named GetOrder and has a single int parameter of orderid. I’m calling the above like so:

SqlParameter parm1 = new SqlParameter("@orderid", SqlDbType.Int);
                parm1.Value = 123;
 var results =
                    _session.ExecuteStoredProcedure<GetOrderResult>(parm1).Single();

I’m receiving the following error: A query parameter cannot be of type ‘System.Data.SqlClient.SqlParameter’

Thoughts? Or am I just missing something obvious?

Update: I’m trying to make this as generic as possible…my current thinking is that I’m going to have to do some string trickery to create the ExecuteQuery text and parameters.

Update: Posting below my Session Interface and my Linq to Sql Implementation of the interface…hopefully that will clarify what I’m attempting to do

 public interface ISession : IDisposable
    {
        void CommitChanges();
        void Delete<T>(Expression<Func<T, bool>> expression) where T : class;
        void Delete<T>(T item) where T : class;
        void DeleteAll<T>() where T : class;
        T Single<T>(Expression<Func<T, bool>> expression) where T : class;
        IQueryable<T> All<T>() where T : class;
        void Add<T>(T item) where T : class;
        void Add<T>(IEnumerable<T> items) where T : class;
        void Update<T>(T item) where T : class;
        IEnumerable<T> ExecuteStoredProcedure<T>(params object[] parameters);

    }
public class LinqToSqlSession : ISession
    {
        public readonly Db _db;
        public LinqToSqlSession()
        {
            _db = new Db(ConfigurationManager.ConnectionStrings[Environment.MachineName].ConnectionString);
        }

        public void CommitChanges()
        {
            _db.SubmitChanges();
        }
        /// <summary>
        /// Gets the table provided by the type T and returns for querying
        /// </summary>
        private Table<T> GetTable<T>() where T : class
        {
            return _db.GetTable<T>();
        }


        public void Delete<T>(Expression<Func<T, bool>> expression) where T : class
        {

            var query = All<T>().Where(expression);
            GetTable<T>().DeleteAllOnSubmit(query);
        }

        public void Delete<T>(T item) where T : class
        {
            GetTable<T>().DeleteOnSubmit(item);
        }

        public void DeleteAll<T>() where T : class
        {
            var query = All<T>();
            GetTable<T>().DeleteAllOnSubmit(query);
        }



        public void Dispose()
        {
            _db.Dispose();
        }

        public T Single<T>(Expression<Func<T, bool>> expression) where T : class
        {
            return GetTable<T>().SingleOrDefault(expression);
        }

        public IEnumerable<T> ExecuteStoredProcedure<T>(params object[] parameters)
        {
            Type genericType = typeof(T);

            string commandstring = genericType.Name.Replace("Result", "");
            //_db is my Linq To Sql database

            return _db.ExecuteQuery<T>(commandstring, parameters).AsEnumerable();
        }

        public IQueryable<T> All<T>() where T : class
        {
            return GetTable<T>().AsQueryable();
        }

        public void Add<T>(T item) where T : class
        {
            GetTable<T>().InsertOnSubmit(item);
        }
        public void Add<T>(IEnumerable<T> items) where T : class
        {
            GetTable<T>().InsertAllOnSubmit(items);
        }
        public void Update<T>(T item) where T : class
        {
            //nothing needed here
        }
}
  • 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-05-14T21:07:33+00:00Added an answer on May 14, 2026 at 9:07 pm

    That isn’t how you’re supposed to wire up Stored Procedures with Linq-to-SQL. You should extend the DataContext and use ExecuteMethodCall instead:

    Taken from MSDN:

    public partial class MyDataContext
    {
        [Function()]
        public IEnumerable<Customer> CustomerById(
            [Parameter(Name = "CustomerID", DbType = "NChar(5)")]
            string customerID)
        {
            IExecuteResult result = this.ExecuteMethodCall(this,
                ((MethodInfo)(MethodInfo.GetCurrentMethod())),
                customerID);
            return (IEnumerable<Customer>)(result.ReturnValue);
        }
    }
    

    If you really must execute a sproc as a query (highly not recommended), then you have to preface the command with EXEC, and don’t use SqlParameter either, the call would look like:

    var results = context.ExecuteQuery<MyResult>("EXEC usp_MyProc {0}, {1}",
        custID, custName);
    

    (And I’ll note, pre-emptively, that this is not a SQL injection vector because Linq to SQL turns the curly braces into a parameterized query.)

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

Sidebar

Ask A Question

Stats

  • Questions 444k
  • Answers 444k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Fixed the problem! class Order < ActiveRecord::Base has_many :itemgroups, :dependent… May 15, 2026 at 6:42 pm
  • Editorial Team
    Editorial Team added an answer Are you going to show all the 8000 values at… May 15, 2026 at 6:42 pm
  • Editorial Team
    Editorial Team added an answer flash.utils package has some methods that you might find useful.… May 15, 2026 at 6:42 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.