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;

but this throws an exception every time.

What exception exactly?

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!

your script code has not

FROM
commented: Upvote for taking the time to read and comment. +8

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.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.