I am trying to get data from table with query below, basically trying to get int val from Value field for each month as V1, V2, etc
SELECT
(CASE When Month = 'January' Then Value Else 0 End) AS V1,
(CASE When Month = 'February' Then Value Else 0 End) AS V2,
(CASE When Month = 'March' Then Value Else 0 End) AS V3,
(CASE When Month = 'April' Then Value Else 0 End) AS V4,
(CASE When Month = 'May' Then Value Else 0 End) AS V5,
(CASE When Month = 'June' Then Value Else 0 End) AS V6,
(CASE When Month = 'July' Then Value Else 0 End) AS V7,
(CASE When Month = 'August' Then Value Else 0 End) AS V8,
(CASE When Month = 'September' Then Value Else 0 End) AS V9,
(CASE When Month = 'October' Then Value Else 0 End) AS V10,
(CASE When Month = 'November' Then Value Else 0 End) AS V11,
(CASE When Month = 'December' Then Value Else 0 End) AS V12
FROM tblOmOra WHERE Year=@CurrentYear;
What is wrong with this query?

Brahma Prakash ShuklaPosted Oct 22, 2024, 1:32 PM
The issue seems to be caused by your stored procedure returning multiple result sets. You are executing two separate
SELECTstatements in your stored procedure, and both return results: one with columnsM1,M2, etc., and another with columnsV1,V2, etc. Since you are accessing the first row of the first result set in your C# code, you are trying to accessV1from the first result set, which does not containV1.To solve this, you need to handle multiple result sets in your C# code. You can achieve this by using
SqlDataReaderand itsNextResult()method to iterate through each result set.Please try below code -
Marius VasilePosted Oct 22, 2024, 1:47 PM
Ok, I saw late your C#, it is working fine now I got the values for V1, V2...V12 as well but I have to do some math in C3 outside the reader if's
and having 2 if's is not working. Error The name 'acc1' does not exist in the current context
Brahma Prakash ShuklaPosted Oct 22, 2024, 1:38 PM
Have you tried my code of c# ?
Marius VasilePosted Oct 22, 2024, 1:33 PM
I verified it, the result set returned by
SqlDataAdapter.Fill(resultTable) contains only first query result, without second query. How do I manage that?Brahma Prakash ShuklaPosted Oct 22, 2024, 1:29 PM
Hi, The error you're encountering (
"Column 'V1' does not belong to table") suggests that the query executed by your stored procedure (RALAM) is not returning the expected columns (V1,V2, etc.) in the result set. This can happen for a few reasons:Stored Procedure Not Returning the Correct Columns
Column Aliases Not Matching
Result Set Schema: The result set returned by
SqlDataAdapter.Fill(resultTable)may not contain columnsV1,V2, etc., if the stored procedure doesn't return them. You can verify this by logging or debugging the structure ofresultTable.Columns.Marius VasilePosted Oct 22, 2024, 1:25 PM
Full stored procedure here
Marius VasilePosted Oct 22, 2024, 1:23 PM
Thank you Brahma, I got the same error message (Column 'V1' does not belong to table). I try to get data with
Brahma Prakash ShuklaPosted Oct 22, 2024, 1:16 PM
The issue with your query is that it will return multiple rows, each containing a value for a specific month, but the columns
V1throughV12will always return0for all months except the current one in the row.You are using a
CASEstatement to assign values toV1,V2, etc., based on the month. However, since SQL processes rows individually, each row will only match one month at a time. To pivot this data into a single row with columns for each month, you will need to aggregate the results.Please try this one -