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
I suppose the problem is apostrophe signs in split[1]. Database trying to find “‘100883′” string instead “100883”
Please try this little hack: