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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T07:39:36+00:00 2026-05-30T07:39:36+00:00

I have a SQL query I have to perform based on Operator Input. I

  • 0

I have a SQL query I have to perform based on Operator Input.

I am building a form that has a Drop Down box that is populated with the table columns and a text box for them to provide the data.

The SQL statement was built using SQL Management Studio 2008. I have tested it (where columnFilter="WorkOrder='100883'"), and it returns 18 rows of data.

When I pass the same data to the method I wrote below, there is no error, the SQL is exactly the same as that I punched into Management Studio, but the DataTable never has any data in it.

The code could use a little cleaning up because I’ve been trying a few different things, but it is still logically valid …just not the most logical at the moment.

Please! Could someone tell me what I am doing wrong?

I do not generally use the AddWithValue Parameter option (because I think it is sloppy and implies one does not know the data type), but in this case I do not know the data type (?). I suspect this method may have something to do with my problem, but I don’t know how to check it.

First, I’ll show you the code:

public DataTable GetSheetMetalRequest(string columnFilter) {
  DataTable table = new DataTable();
  string sqlText = null;
  string[] split = !String.IsNullOrEmpty(columnFilter) ? columnFilter.Split('=') : new string[] { String.Empty };
  if (split.Length == 2) {
    StringBuilder sb = new StringBuilder("SELECT ");
    sb.Append("V.DateStamp, WorkOrder, PartNumber, Qty, EmployeeID, ");
    sb.Append("CAST(P.ProductionLineID AS VARCHAR(10))+': '+ProductionLine AS 'AcpLine', ");
    sb.Append("MTF, CAST(V.EventStatusID AS VARCHAR(10))+': '+[Status] AS 'AcpStatus', ");
    sb.Append("V.RequestID, ReasonID, E.DateStamp AS 'StartDate' ");
    sb.Append("FROM vwSheetMetalRequestByEvent V ");
    sb.Append("INNER JOIN ProductionLines P ON (V.ProductionLineID=P.ProductionLineID) ");
    sb.Append("INNER JOIN [Event] E ON (V.RequestID=E.RequestID) ");
    sb.Append("WHERE V.RequestID IS NOT NULL AND E.EventStatusID=1"); // EventStatusID=SheetMetalRequest
    sb.Append(string.Format(" AND [{0}]=@{0}", split[0]));
    sb.Append(" ORDER BY E.DateStamp, WorkOrder, PartNumber ");
    sqlText = sb.ToString();
  }
  if (!String.IsNullOrEmpty(sqlText)) {
    using (SqlCommand cmd = new SqlCommand(sqlText, Conn_Tracker)) {
      cmd.CommandType = CommandType.Text;
      cmd.Parameters.AddWithValue(string.Format("@{0}", split[0]), split[1]);
      string test = string.Format("{0}={1}", cmd.Parameters[0].ParameterName, cmd.Parameters[0].Value);
      Console.WriteLine(test); // ""
      try {
        cmd.Connection.Open();
        table.Load(cmd.ExecuteReader());
        if (table.Rows.Count < 1) {
          Console.WriteLine("no data");
        }
      } catch (Exception err) {
        LogError("GetSheetMetalRequest", err);
      } finally {
        cmd.Connection.Close();
      }
    }
  }
  return table;
}

Now, I’ll show you what the SQL statement looks like (formatted to be pretty and readable):

declare @WorkOrder nvarchar(50)
set @WorkOrder='100883'

SELECT
  V.DateStamp, WorkOrder, PartNumber, Qty, EmployeeID, 
  CAST(P.ProductionLineID AS VARCHAR(10))+': '+ProductionLine AS 'AcpLine', 
  MTF, CAST(V.EventStatusID AS VARCHAR(10))+': '+[Status] AS 'AcpStatus', 
  V.RequestID, ReasonID, E.DateStamp AS 'StartDate' 
FROM vwSheetMetalRequestByEvent V 
  INNER JOIN ProductionLines P ON (V.ProductionLineID=P.ProductionLineID) 
  INNER JOIN [Event] E ON (V.RequestID=E.RequestID) 
WHERE
  V.RequestID IS NOT NULL AND 
  E.EventStatusID=1 AND 
  [WorkOrder]=@WorkOrder 
ORDER BY E.DateStamp, WorkOrder, PartNumber 
  • 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-30T07:39:37+00:00Added an answer on May 30, 2026 at 7:39 am

    I suppose the problem is apostrophe signs in split[1]. Database trying to find “‘100883′” string instead “100883”

    Please try this little hack:

    cmd.Parameters.AddWithValue(string.Format("@{0}", split[0]), split[1].Replace("'",""));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an SQL query that takes the following form: UPDATE foo SET flag=true
I have to perform a SQL query to get the top 10 albums according
I have a sql query that runs super fast, around one second, when not
I have a SQL query that takes a very long time to run on
We have a SQL query that pulls a large number of fields from many
Stackoverflowers, I have a resultset from a SQL query in the form of: Category
I have a sql query that performs the type of select I'm after: select
I have a function that generates a dropdown based on a query or array
I have a forum. I want to perform an SQL query so I can
I am trying to perform a SQL query that acts in two parts. First,

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.