0

I have written a SQL query to find product information in a c# program and it doesn't seem to work at all! I have tested a variety of other queries with my C# code and it works fine, but this throws an exception every time.

SELECT     TOP (1) CONVERT(varchar, tmpRptAssetEvent.EventTimeStamp, 120) AS 'DateTime', ResultEnum.ResultEnumName AS 'Result',
                              AssetItems.AssetSN AS 'SerialNumber', Events.EventAddDate as 'EntryAddDateTime', Events.ExecTime as 'TestTime',
                             ModelEnum.PartName as 'Model', EventEnum.Name AS 'EventName', ProcessEnum.Name AS 'ProcessName'
                              AssetItems ON tmpRptAssetEvent.AssetID = AssetItems.AssetID INNER JOIN
                             ResultEnum ON tmpRptAssetEvent.EventResult = ResultEnum.ResultEnumID INNER JOIN
                             Station ON tmpRptAssetEvent.StationID = Station.StationID INNER JOIN
                             ModelEnum ON tmpRptAssetEvent.ModelEnum = ModelEnum.ModelEnumID INNER JOIN
                             EventEnum ON tmpRptAssetEvent.EventEnumID = EventEnum.EventEnumID INNER JOIN
                             ProcessEnum ON tmpRptAssetEvent.ProcessEnumID = ProcessEnum.ProcessEnumID INNER JOIN
                            Events ON tmpRptAssetEvent.EventID = Events.EventID
                             WHERE     (Station.StationName = '" + station + "')
                             ORDER BY tmpRptAssetEvent.EventTimeStamp desc;

Edited by pritaeas: Added markdown.

4
Contributors
4
Replies
29
Views
2 Years
Discussion Span
Last Post by BitBlt
1

At the risk of reviving a stale thread...

It looks like you aren't including a "FROM" clause. You have a bunch of joins but never include the anchor table. I reformatted your code and included a comment that might make it clearer:

SELECT     TOP (1) CONVERT(varchar, tmpRptAssetEvent.EventTimeStamp, 120) AS 'DateTime', 
ResultEnum.ResultEnumName AS 'Result',
AssetItems.AssetSN AS 'SerialNumber', 
Events.EventAddDate as 'EntryAddDateTime', 
Events.ExecTime as 'TestTime',
ModelEnum.PartName as 'Model', 
EventEnum.Name AS 'EventName', 
ProcessEnum.Name AS 'ProcessName'
-- It looks like you're missing a line here...
-- maybe "from tmpRptAssetEvent" or something like that?
AssetItems ON tmpRptAssetEvent.AssetID = AssetItems.AssetID 
INNER JOIN ResultEnum 
ON tmpRptAssetEvent.EventResult = ResultEnum.ResultEnumID 
INNER JOIN Station 
ON tmpRptAssetEvent.StationID = Station.StationID 
INNER JOIN ModelEnum 
ON tmpRptAssetEvent.ModelEnum = ModelEnum.ModelEnumID 
INNER JOIN EventEnum 
ON tmpRptAssetEvent.EventEnumID = EventEnum.EventEnumID 
INNER JOIN ProcessEnum 
ON tmpRptAssetEvent.ProcessEnumID = ProcessEnum.ProcessEnumID 
INNER JOIN Events ON tmpRptAssetEvent.EventID = Events.EventID
WHERE     (Station.StationName = '" + station + "')
ORDER BY tmpRptAssetEvent.EventTimeStamp desc;

Hope this helps! Happy coding!

1

Yep...but if you check the comment on line 10, it gives a hint that the OP might use to fix his code. I could easily have included the final answer but I would have been making assumptions about what his intentions were.

Oh, and I see someone downvoted your post...I'm upvoting it just because you took the time to look over the answer and respond. Your attention is valuable and your continued assistance is appreciated.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.