Who closed my datareader?
-
Just spotted this on MSDN forum. Not a very subtle bug but can be quite subtle for beginners:
using ( drUser = cmdUser.ExecuteReader());
{
drUser.Read();
myUser.User_Code = Convert.ToInt32(drUser[0]);
myUser.User_Name = drUser[1].ToString();
myUser.Password = drUser[2].ToString();
myUser.Type = (UserType)drUser.GetByte(drUser.GetOrdinal("User_Type"));
}
drUser.Close();
cnnUser.Close();It generates an error at Read()
Giorgi Dalakishvili #region signature my articles #endregion
-
Just spotted this on MSDN forum. Not a very subtle bug but can be quite subtle for beginners:
using ( drUser = cmdUser.ExecuteReader());
{
drUser.Read();
myUser.User_Code = Convert.ToInt32(drUser[0]);
myUser.User_Name = drUser[1].ToString();
myUser.Password = drUser[2].ToString();
myUser.Type = (UserType)drUser.GetByte(drUser.GetOrdinal("User_Type"));
}
drUser.Close();
cnnUser.Close();It generates an error at Read()
Giorgi Dalakishvili #region signature my articles #endregion
Ah, yes, yet another place that can happen.
-
Just spotted this on MSDN forum. Not a very subtle bug but can be quite subtle for beginners:
using ( drUser = cmdUser.ExecuteReader());
{
drUser.Read();
myUser.User_Code = Convert.ToInt32(drUser[0]);
myUser.User_Name = drUser[1].ToString();
myUser.Password = drUser[2].ToString();
myUser.Type = (UserType)drUser.GetByte(drUser.GetOrdinal("User_Type"));
}
drUser.Close();
cnnUser.Close();It generates an error at Read()
Giorgi Dalakishvili #region signature my articles #endregion
The rogue semicolon strikes again! Doesn't C# throw a warning for empty block statements?
Please don't bother me... I'm hacking right now. Don't look at me like that - doesn't anybody remember what "hacking" really means? :sigh:
-
The rogue semicolon strikes again! Doesn't C# throw a warning for empty block statements?
Please don't bother me... I'm hacking right now. Don't look at me like that - doesn't anybody remember what "hacking" really means? :sigh:
Maybe it should give a warning on
;{
-
Just spotted this on MSDN forum. Not a very subtle bug but can be quite subtle for beginners:
using ( drUser = cmdUser.ExecuteReader());
{
drUser.Read();
myUser.User_Code = Convert.ToInt32(drUser[0]);
myUser.User_Name = drUser[1].ToString();
myUser.Password = drUser[2].ToString();
myUser.Type = (UserType)drUser.GetByte(drUser.GetOrdinal("User_Type"));
}
drUser.Close();
cnnUser.Close();It generates an error at Read()
Giorgi Dalakishvili #region signature my articles #endregion
-
Giorgi Dalakishvili wrote:
It generates an error at Read()
And at drUser.Close() ;P (well it should, if the original bug gets worked around)
xacc.ide - now with IronScheme support
IronScheme - 1.0 alpha 2 out nowIn this case, it would throw an exception since the reader object is already closed. Wouldn't it?
Vasudevan Deepak Kumar Personal Homepage
Tech Gossips
A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson -
In this case, it would throw an exception since the reader object is already closed. Wouldn't it?
Vasudevan Deepak Kumar Personal Homepage
Tech Gossips
A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis Levinson -
Maybe it should give a warning on
;{
-
Just spotted this on MSDN forum. Not a very subtle bug but can be quite subtle for beginners:
using ( drUser = cmdUser.ExecuteReader());
{
drUser.Read();
myUser.User_Code = Convert.ToInt32(drUser[0]);
myUser.User_Name = drUser[1].ToString();
myUser.Password = drUser[2].ToString();
myUser.Type = (UserType)drUser.GetByte(drUser.GetOrdinal("User_Type"));
}
drUser.Close();
cnnUser.Close();It generates an error at Read()
Giorgi Dalakishvili #region signature my articles #endregion
Well don't look at me, I didn't do it.... I've been too busy working on this -> http://www.codeproject.com/KB/WPF/ribboncontrol.aspx[^]
-
Just spotted this on MSDN forum. Not a very subtle bug but can be quite subtle for beginners:
using ( drUser = cmdUser.ExecuteReader());
{
drUser.Read();
myUser.User_Code = Convert.ToInt32(drUser[0]);
myUser.User_Name = drUser[1].ToString();
myUser.Password = drUser[2].ToString();
myUser.Type = (UserType)drUser.GetByte(drUser.GetOrdinal("User_Type"));
}
drUser.Close();
cnnUser.Close();It generates an error at Read()
Giorgi Dalakishvili #region signature my articles #endregion
ouch!
We are a big screwed up dysfunctional psychotic happy family - some more screwed up, others more happy, but everybody's psychotic joint venture definition of CP
blog: TDD - the Aha! | Linkify!| FoldWithUs! | sighist -
Just spotted this on MSDN forum. Not a very subtle bug but can be quite subtle for beginners:
using ( drUser = cmdUser.ExecuteReader());
{
drUser.Read();
myUser.User_Code = Convert.ToInt32(drUser[0]);
myUser.User_Name = drUser[1].ToString();
myUser.Password = drUser[2].ToString();
myUser.Type = (UserType)drUser.GetByte(drUser.GetOrdinal("User_Type"));
}
drUser.Close();
cnnUser.Close();It generates an error at Read()
Giorgi Dalakishvili #region signature my articles #endregion
The Real WTF (TM) is that it's closing the drUser outside the supposed using block.
-
In this case, it would throw an exception since the reader object is already closed. Wouldn't it?
Vasudevan Deepak Kumar Personal Homepage
Tech Gossips
A pessimist sees only the dark side of the clouds, and mopes; a philosopher sees both sides, and shrugs; an optimist doesn't see the clouds at all - he's walking on them. --Leonard Louis LevinsonNot necessarily - Close() is often an alias for Dispose() and that never throws exceptions when called more times than necessary (as per the disposable pattern recommendations -
Dispose()
should under no circumstances whatsoever be dangerous to call - regardless of state). Any other method should throw an ObjectDisposedException, however. I may be wrong about DataReaders, but it's definitely so (Close and Dispose are the same - or actually, one calls the other) for Streams (as per the documentation).-- Peter