Click here to Skip to main content
15,885,366 members
Home / Discussions / ASP.NET
   

ASP.NET

 
GeneralRe: Syntax error in store procedure Pin
Zafar Sultan26-Dec-12 3:31
Zafar Sultan26-Dec-12 3:31 
Questionid or guid.. which one? Pin
Jassim Rahma25-Dec-12 7:22
Jassim Rahma25-Dec-12 7:22 
AnswerRe: id or guid.. which one? Pin
Sandeep Mewara25-Dec-12 19:14
mveSandeep Mewara25-Dec-12 19:14 
AnswerRe: id or guid.. which one? Pin
CommDev25-Dec-12 19:25
CommDev25-Dec-12 19:25 
Questionwhy should I care about ExecuteScalar Pin
Jassim Rahma25-Dec-12 5:57
Jassim Rahma25-Dec-12 5:57 
AnswerRe: why should I care about ExecuteScalar Pin
Sandeep Mewara25-Dec-12 19:30
mveSandeep Mewara25-Dec-12 19:30 
AnswerRe: why should I care about ExecuteScalar Pin
CommDev25-Dec-12 19:33
CommDev25-Dec-12 19:33 
AnswerRe: why should I care about ExecuteScalar Pin
CommDev25-Dec-12 19:40
CommDev25-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
Jassim Rahma28-Dec-12 9:40 
GeneralRe: why should I care about ExecuteScalar Pin
Naveen Katta26-Dec-12 0:10
Naveen Katta26-Dec-12 0:10 
Questiona control that should contain html elements Pin
Ali Al Omairi(Abu AlHassan)23-Dec-12 23:01
professionalAli Al Omairi(Abu AlHassan)23-Dec-12 23:01 
QuestionRe: a control that should contain html elements Pin
jkirkerx24-Dec-12 13:09
professionaljkirkerx24-Dec-12 13:09 
AnswerRe: a control that should contain html elements Pin
Ali Al Omairi(Abu AlHassan)25-Dec-12 19:51
professionalAli Al Omairi(Abu AlHassan)25-Dec-12 19:51 
Questioncheckedchange event of gridview in javascript Pin
Ravi_kant23-Dec-12 19:29
Ravi_kant23-Dec-12 19:29 
AnswerRe: checkedchange event of gridview in javascript Pin
Sampath Kumar Sathiya24-Dec-12 0:20
Sampath Kumar Sathiya24-Dec-12 0:20 
GeneralRe: checkedchange event of gridview in javascript Pin
Ravi_kant24-Dec-12 7:40
Ravi_kant24-Dec-12 7:40 
Questionupdated data from gridview Pin
Member 970720723-Dec-12 0:30
Member 970720723-Dec-12 0:30 
AnswerRe: updated data from gridview Pin
Ali Al Omairi(Abu AlHassan)24-Dec-12 4:31
professionalAli Al Omairi(Abu AlHassan)24-Dec-12 4:31 
AnswerRe: updated data from gridview Pin
Sandeep Mewara25-Dec-12 19:35
mveSandeep Mewara25-Dec-12 19:35 
Question.net question Pin
Tarani Dutt Joshi21-Dec-12 23:53
Tarani Dutt Joshi21-Dec-12 23:53 
AnswerRe: .net question Pin
Richard MacCutchan22-Dec-12 0:18
mveRichard MacCutchan22-Dec-12 0:18 
AnswerRe: .net question Pin
MaulikDusara24-Dec-12 21:38
MaulikDusara24-Dec-12 21:38 
AnswerRe: .net question Pin
Sandeep Mewara25-Dec-12 19:33
mveSandeep Mewara25-Dec-12 19:33 
QuestionWebRequest Pin
cdpsource21-Dec-12 14:16
cdpsource21-Dec-12 14:16 
Questionwebpage has resulted in too many redirects Pin
Jassim Rahma21-Dec-12 9:46
Jassim Rahma21-Dec-12 9:46 

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.