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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T17:25:18+00:00 2026-06-03T17:25:18+00:00

In my programming task I’ve gone down a dark alley and wished I hadn’t,

  • 0

In my programming task I’ve gone down a dark alley and wished I hadn’t, but there is no turning back now.

I’m building up a SQL statement where the table name, column name and id value are retrieved from query string parameters i.e. ("SELECT [{0}] FROM [{1}] WHERE [Id] = {2};", c, t, id)

But it isn’t as bad as it looks, I’m protected:

  • Only authenticated users (i.e. signed in users) can execute the Page_Load
  • I’m checking that both the table and the column exists beforehand
    (using GetSchema etc.)
  • I’m checking that the Id is an integer beforehand
  • All my tables have Id columns
  • The database connection is reasonably secure

The field value is expected to be of type NVARCHAR(4000) or NVARCHAR(MAX) so I’m avoiding ExecuteScalar and I’m trying out LINQ ExecuteQuery because I like LINQ. But I’m a bit out of my depth again.

I’ve got this far:

    Dim db As New MyDataContext

    Dim result = db.ExecuteQuery(Of ITable)("SELECT [{0}] FROM [{1}] WHERE [Id] = {2};", c, t, id)
  • Is this the right way to go?
  • How do I get first row and first column value?
  • Is there a better alternative?

P.S. It’s a SQL Server 2005 database

Any help appreciated.
Thanks.

  • 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-03T17:25:20+00:00Added an answer on June 3, 2026 at 5:25 pm

    SQL Server requires the tables ans columns to be statically known. You can’t provide them using command parameters. You can’t say

    select * from @tableName
    

    because the table name can’t be a variable.

    You need to build the SQL string with C# ensuring proper escaping of identifiers. Escaping works like this:

    var escaped = "[" + rawUntrustedUserInput.Replace("]", "]]") + "]";
    

    This is safe.

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

Sidebar

Related Questions

I was just wondering if there's any field which concerns the task-control programming (or
Let me say we have a simple programming task. But for the sake of
I'm very new in programming. The following task looks very simple but I don't
I've found a task on some Java programming competition. One must create class Sentence
When programming an OO program using a SQL database back-end, do objects' attributes correspond
I'm given some nasty task in concurrent programming. I need to continuously read data
In the C programming language, DO TASK always runs because a is assigned the
I have a little problem in object programming in javascript There is a class
I am faced with the task of building a new component to be integrated
I'm new to web programming, but I had an idea I could use as

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.