Click here to Skip to main content
15,914,452 members
Home / Discussions / C#
   

C#

 
QuestionList box Pin
User 58385225-Oct-05 20:24
User 58385225-Oct-05 20:24 
AnswerRe: List box Pin
User 58385225-Oct-05 20:34
User 58385225-Oct-05 20:34 
GeneralRe: List box Pin
technomanceraus25-Oct-05 21:08
technomanceraus25-Oct-05 21:08 
GeneralRe: List box Pin
User 58385225-Oct-05 21:14
User 58385225-Oct-05 21:14 
QuestionDataReader Pin
Brendan Vogt25-Oct-05 20:16
Brendan Vogt25-Oct-05 20:16 
AnswerRe: DataReader Pin
leppie25-Oct-05 22:03
leppie25-Oct-05 22:03 
QuestionRe: DataReader Pin
Brendan Vogt25-Oct-05 22:33
Brendan Vogt25-Oct-05 22:33 
AnswerRe: DataReader Pin
Colin Angus Mackay25-Oct-05 22:42
Colin Angus Mackay25-Oct-05 22:42 
If you are going to put the data into your business object (News) then I do not suggest using a SqlDataAdapter to fill a DataTable/DataSet because all you are going is wasting time creating extra copies of the data that you will throw away. (The SqlDataAdapter uses a SqlDataReader internally to populate the DataTable/DataSet)

You could look at the SqlDataReader.HasRows[^] property.

At the top of your method replace
News objNews = new News();
with
News objNews = null;
. Wait until you perform the ExecuteReader() then, inside your try block (replacing the while loop), do something like this:
if (objDr.HasRows)
{
    objNews = new News();
    objDr.Read()
    objNews.NewsID = Convert.ToInt32(objDr["PKNewsID"]);
    objNews.CategoryID = Globals.CheckDBIntForDBNulls(objDr["FKNewsCategoryID"]);
    objNews.Title = objDr["NewsTitle"].ToString();
}
I took out the while because it wasn't really doing anything useful. If you expect only one row to be returned then the above will be fine. If you expect more than one row returned and you are just looking for the results of the last row then your old while loop will work, but it suggests there is something wrong with the SQL that it is returning more than it should.

Now, if no rows are returned from SQL Server, a null is returned from the method.

Does this help?


My: Blog | Photos

"Man who stand on hill with mouth open will wait long time for roast duck to drop in." -- Confucious


QuestionEditing Pin
momoo25-Oct-05 19:45
momoo25-Oct-05 19:45 
AnswerRe: Editing Pin
Christian Graus25-Oct-05 20:07
protectorChristian Graus25-Oct-05 20:07 
QuestionCollections Pin
seee sharp25-Oct-05 19:05
seee sharp25-Oct-05 19:05 
AnswerRe: Collections Pin
Christian Graus25-Oct-05 19:23
protectorChristian Graus25-Oct-05 19:23 
GeneralRe: Collections Pin
seee sharp25-Oct-05 21:06
seee sharp25-Oct-05 21:06 
GeneralRe: Collections Pin
Christian Graus26-Oct-05 10:20
protectorChristian Graus26-Oct-05 10:20 
AnswerRe: Collections Pin
S. Senthil Kumar25-Oct-05 22:32
S. Senthil Kumar25-Oct-05 22:32 
QuestionView Source Code Pin
alok_2k325-Oct-05 18:55
alok_2k325-Oct-05 18:55 
AnswerRe: View Source Code Pin
seee sharp25-Oct-05 19:08
seee sharp25-Oct-05 19:08 
Questioninherited controls Pin
xilefxilef25-Oct-05 18:19
xilefxilef25-Oct-05 18:19 
AnswerRe: inherited controls Pin
Longxe25-Oct-05 18:24
Longxe25-Oct-05 18:24 
Questiondraw above a control? Pin
Longxe25-Oct-05 17:37
Longxe25-Oct-05 17:37 
AnswerRe: draw above a control? Pin
Christian Graus25-Oct-05 17:49
protectorChristian Graus25-Oct-05 17:49 
GeneralRe: draw above a control? Pin
Longxe25-Oct-05 18:07
Longxe25-Oct-05 18:07 
AnswerRe: draw above a control? Pin
Rob Graham25-Oct-05 17:54
Rob Graham25-Oct-05 17:54 
GeneralRe: draw above a control? Pin
Longxe25-Oct-05 18:31
Longxe25-Oct-05 18:31 
Questionunicode Pin
ybasha25-Oct-05 16:15
ybasha25-Oct-05 16:15 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.