The problem is in the SQL query. You are selecting from both NEWS20p
and NEWSCAT20p
tables, which can lead to incorrect results.
To fix this issue, you should select only one table that contains the required columns. Assuming that NEWSCAT20p
has a foreign key relationship with NEWS20p
, you can use the following query:
@"SELECT TOP(5) * FROM (SELECT * , ROW_NUMBER() OVER(ORDER BY newsid DESC) as RowNum
FROM NEWS20p, NEWSCAT20p WHERE NEWS20P.sabcat = @HiddenField1Value AND NEWS20P.newscatid = NEWSCAT20P.newscatid
and NEWS20P.newscatid IN (SELECT newscatid FROM NEWSCAT20p WHERE sabcat = @HiddenField1Value)) as NEWS20p"
However, a better approach would be to use joins instead of subqueries:
@"SELECT TOP(5) * FROM NEWS20p
INNER JOIN NEWSCAT20p ON NEWS20P.sabcat = NEWSCAT20P.sabcat AND NEWS20P.newscatid = NEWSCAT20P.newscatid
WHERE ROW_NUMBER() OVER(ORDER BY newsid DESC) >= @PageIndex"
This query will correctly join the two tables based on their common columns, and then select only rows where newsid
is within the specified page range.
In your C# code, you should also adjust the parameter binding to match the corrected SQL query:
SqlCommand cmd = new SqlCommand(strCmd, con);
cmd.Parameters.AddWithValue("@PageIndex", intPage.ToString());
cmd.Parameters.AddWithValue("@HiddenField1Value", HiddenField1.Value);
// ...
da.Fill(ds, strTableName);
Please note that you should also handle exceptions and errors properly in your production code.
Last modified on 2025-02-25