Click here to Skip to main content
11,575,635 members (61,252 online)
   

ASP.NET

 
GeneralRe: Syntax error in store procedure Pin
Zafar Sultan26-Dec-12 3:31
memberZafar Sultan26-Dec-12 3:31 
Questionid or guid.. which one? Pin
Jassim Rahma25-Dec-12 7:22
memberJassim Rahma25-Dec-12 7:22 
AnswerRe: id or guid.. which one? Pin
Sandeep Mewara25-Dec-12 19:14
mvpSandeep Mewara25-Dec-12 19:14 
AnswerRe: id or guid.. which one? Pin
CommDev25-Dec-12 19:25
memberCommDev25-Dec-12 19:25 
Questionwhy should I care about ExecuteScalar Pin
Jassim Rahma25-Dec-12 5:57
memberJassim Rahma25-Dec-12 5:57 
AnswerRe: why should I care about ExecuteScalar Pin
Sandeep Mewara25-Dec-12 19:30
mvpSandeep Mewara25-Dec-12 19:30 
AnswerRe: why should I care about ExecuteScalar Pin
CommDev25-Dec-12 19:33
memberCommDev25-Dec-12 19:33 
AnswerRe: why should I care about ExecuteScalar Pin
CommDev25-Dec-12 19:40
memberCommDev25-Dec-12 19:40 
Hi,

ExecuteReader

Do not use: when database query is going to provide for sure exactly 1 record. It may be getting record by its id (which is PK in the database) - GetOrderById and such. In this case use ExecuteNonQuery with output parameters.

Use: when database query is going to provide a set of records. It may be search or report.

ExecuteNonQuery

Use: when we are talking about a single database record - in Update, Insert, Delete and Get by Id. In all these cases we can use input/output/input-output parameters. Please note that from the application architecture point of view it is also good practices when your Insert and Update stored procedure returns changed record exactly like Get By Id method does.

ExecuteScalar

Do not use: when database query returns a single value and this value can be defined as parameter in T-SQL. ExecuteNonQuery with output parameter(s) is always preferred in this case since it is more flexible, tomorrow there will be 2 values therefore having ExecuteNonQuery we do not need to change method signatures.

Use: when database query returns a single value and this value cannot be defined as output parameter, because of T-SQL type limitation for variables. For example type image cannot be output parameter in MSSQL.

The most common example for ExecuteScalar is fetching a single image stored in the database and converting it to array of bytes. If you google it - most examples will demonstrate using of ExecuteReader to accomplish image handler, however ExecuteScalar will be more scalable and faster.

Conclusion

Always use ExecuteNonQuery except: when you have a set of records - use ExecuteReader and when you have a single output value that cannot be defined as a parameter - use ExecuteScalar. Hope this helped to clarify something. Enjoy Smile | :)
Comm100 - Leading Live Chat Software Provider

GeneralRe: why should I care about ExecuteScalar Pin
Jassim Rahma28-Dec-12 9:40
memberJassim Rahma28-Dec-12 9:40 
GeneralRe: why should I care about ExecuteScalar Pin
Naveen Katta26-Dec-12 0:10
memberNaveen Katta26-Dec-12 0:10 
Questiona control that should contain html elements Pin
Ali Al Omairi(Abu AlHassan)23-Dec-12 23:01
memberAli Al Omairi(Abu AlHassan)23-Dec-12 23:01 
QuestionRe: a control that should contain html elements Pin
jkirkerx24-Dec-12 13:09
memberjkirkerx24-Dec-12 13:09 
AnswerRe: a control that should contain html elements Pin
Ali Al Omairi(Abu AlHassan)25-Dec-12 19:51
memberAli Al Omairi(Abu AlHassan)25-Dec-12 19:51 
Questioncheckedchange event of gridview in javascript Pin
Ravi_kant23-Dec-12 19:29
memberRavi_kant23-Dec-12 19:29 
AnswerRe: checkedchange event of gridview in javascript Pin
Sampath Kumar Sathiya24-Dec-12 0:20
memberSampath Kumar Sathiya24-Dec-12 0:20 
GeneralRe: checkedchange event of gridview in javascript Pin
Ravi_kant24-Dec-12 7:40
memberRavi_kant24-Dec-12 7:40 
Questionupdated data from gridview Pin
Member 970720723-Dec-12 0:30
memberMember 970720723-Dec-12 0:30 
AnswerRe: updated data from gridview Pin
Ali Al Omairi(Abu AlHassan)24-Dec-12 4:31
memberAli Al Omairi(Abu AlHassan)24-Dec-12 4:31 
AnswerRe: updated data from gridview Pin
Sandeep Mewara25-Dec-12 19:35
mvpSandeep Mewara25-Dec-12 19:35 
Question.net question Pin
Tarani Dutt Joshi21-Dec-12 23:53
memberTarani Dutt Joshi21-Dec-12 23:53 
AnswerRe: .net question Pin
Richard MacCutchan22-Dec-12 0:18
mvpRichard MacCutchan22-Dec-12 0:18 
AnswerRe: .net question Pin
MaulikDusara24-Dec-12 21:38
memberMaulikDusara24-Dec-12 21:38 
AnswerRe: .net question Pin
Sandeep Mewara25-Dec-12 19:33
mvpSandeep Mewara25-Dec-12 19:33 
QuestionWebRequest Pin
cdpsource21-Dec-12 14:16
membercdpsource21-Dec-12 14:16 
Questionwebpage has resulted in too many redirects Pin
Jassim Rahma21-Dec-12 9:46
memberJassim Rahma21-Dec-12 9:46 

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

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


Advertise | Privacy | Mobile
Web03 | 2.8.150624.2 | Last Updated 3 Jul 2015
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid