Click here to Skip to main content
15,882,113 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more: , +
Hi ,

im unable to read data from sql server view

im getting this error : Invalid attempt to read when there no data is present.

getting error from reading data from column which is although not present in table when view is created that column in generated and add spcific data in that column . It means that column is available when i open view data in sql server management studio.

but im getting error in getting values from that column

following is the query which displays data , i saved it as a view in sql server but unable to get column value


SELECT R2.category, R2.subcategory, R2.keywords, tblCategories.formtype, tblCategories.metadescription
FROM tblCategories INNER JOIN
(SELECT [category], [subcategory], STUFF
((SELECT ',' + [subcategory] AS 'text()'
FROM tblCategories t2
WHERE t2.[category] = t1.[category] FOR XML PATH('')), 1, 1, '') AS [keywords]
FROM tblCategories t1
GROUP BY category, subcategory) R2 ON tblCategories.subcategory = R2.subcategory
Posted
Updated 8-May-12 9:09am
Comments
OriginalGriff 8-May-12 11:57am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind.
Try showing use what you are doing at the moment, or give an example. Relevant code fragments only, please!
Use the "Improve question" widget to edit your question and provide better information.
[no name] 8-May-12 15:09pm    
hi , thanks
i have improved it , please view the lastest version
Thanks

1 solution

dr.Read() loops to the next record and returns true or false depending on if there's a record to be read or not. When the data reader is initialized, the first record is not selected. It has to be selected by calling dr.Read() which will then return true if a first row is found, and indeed will return true until Read() is called when currently on the last row - i.e. there's no more rows left to read.

dr.HasRows is just a property that tells you if the datareader contains rows... which if it does will keep having rows from here until eternity. For instance, you would use this if you wanted to do something in the event it has rows and something else in the event it doesn't:

C#
if (dr.HasRows)
{
    while (dr.Read())
    {
        /* Display data for current row */
    }
}
else
{
    Console.WriteLine("I didn't find any relevant data.");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900