The following code raises the error "Specified cast is not valid."
MembershipUser user = Membership.GetUser(userId);
DataSet ds = DataLayer.ExecQueryData("SELECT * FROM mc_Clients WHERE
UserId='" + userId.ToString() + "'");
if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
return null;
ClientEx client = new ClientEx();
client.ClientData = new Client();
client.ClientData.Email = user.UserName;
client.ClientData.UserID = user.ProviderUserKey;
client.ClientData.LastLogin = user.LastLoginDate;
DataRow dr = ds.Tables[0].Rows[0];
// ...
if (!Convert.IsDBNull(dr["Sex"]))
client.Sex = (int)dr["Sex"]; <<== ERROR HERE!
// ...
In the database table, Sex has a data type=smallint and nullable=True, and
client.Sex has a data type of int.
If I attempt to examine the contents of dr["Sex"] in the debugger, it shows
a value of 0x0000. If I attempt to examine the contents of (int)dr["Sex"] in
the debugger, it shows an error about not being able to unbox (sorry I no
longer have the exact text).
Any suggestions?
--
Jonathan Wood
SoftCircuits Programming http://www.softcircuits.com 38 1992
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl...
The following code raises the error "Specified cast is not valid."
Yes, it would do...
if (!Convert.IsDBNull(dr["Sex"]))
if (dr["Sex"] != DBNull.Value)
In the database table, Sex has a data type=smallint
So the Sex field can have a range of values from -32,768 to 32,767...? http://msdn2.microsoft.com/en-us/lib...8(SQL.80).aspx
--
Mark Rae
ASP.NET MVP http://www.markrae.net
What type is client.Sex? Looks like whatever it is, it cannot accept int as a
value. Unless the Convert of the (int)dr["Sex"] is failing because it
itself is not an integer.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com
"Jonathan Wood" wrote:
The following code raises the error "Specified cast is not valid."
MembershipUser user = Membership.GetUser(userId);
DataSet ds = DataLayer.ExecQueryData("SELECT * FROM mc_Clients WHERE
UserId='" + userId.ToString() + "'");
if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
return null;
ClientEx client = new ClientEx();
client.ClientData = new Client();
client.ClientData.Email = user.UserName;
client.ClientData.UserID = user.ProviderUserKey;
client.ClientData.LastLogin = user.LastLoginDate;
DataRow dr = ds.Tables[0].Rows[0];
// ...
if (!Convert.IsDBNull(dr["Sex"]))
client.Sex = (int)dr["Sex"]; <<== ERROR HERE!
// ...
In the database table, Sex has a data type=smallint and nullable=True, and
client.Sex has a data type of int.
If I attempt to examine the contents of dr["Sex"] in the debugger, it shows
a value of 0x0000. If I attempt to examine the contents of (int)dr["Sex"] in
the debugger, it shows an error about not being able to unbox (sorry I no
longer have the exact text).
Any suggestions?
--
Jonathan Wood
SoftCircuits Programming http://www.softcircuits.com
As stated, client.Sex is an int.
--
Jonathan Wood
SoftCircuits Programming http://www.softcircuits.com
"Peter Bromberg [C# MVP]" <pb*******@yahoo.NoSpamMaam.comwrote in message
news:23**********************************@microsof t.com...
What type is client.Sex? Looks like whatever it is, it cannot accept int
as a
value. Unless the Convert of the (int)dr["Sex"] is failing because it
itself is not an integer.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com
"Jonathan Wood" wrote:
>The following code raises the error "Specified cast is not valid."
MembershipUser user = Membership.GetUser(userId); DataSet ds = DataLayer.ExecQueryData("SELECT * FROM mc_Clients WHERE UserId='" + userId.ToString() + "'"); if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0) return null; ClientEx client = new ClientEx(); client.ClientData = new Client(); client.ClientData.Email = user.UserName; client.ClientData.UserID = user.ProviderUserKey; client.ClientData.LastLogin = user.LastLoginDate; DataRow dr = ds.Tables[0].Rows[0];
// ...
if (!Convert.IsDBNull(dr["Sex"])) client.Sex = (int)dr["Sex"]; <<== ERROR HERE!
// ...
In the database table, Sex has a data type=smallint and nullable=True, and client.Sex has a data type of int.
If I attempt to examine the contents of dr["Sex"] in the debugger, it shows a value of 0x0000. If I attempt to examine the contents of (int)dr["Sex"] in the debugger, it shows an error about not being able to unbox (sorry I no longer have the exact text).
Any suggestions?
-- Jonathan Wood SoftCircuits Programming http://www.softcircuits.com
Mark,
>The following code raises the error "Specified cast is not valid."
Yes, it would do...
> if (!Convert.IsDBNull(dr["Sex"]))
if (dr["Sex"] != DBNull.Value)
So what does that mean? Does that mean dr["Sex"] was, in fact, null but my
check was wrong? And what the heck does Convert.IsDBNull() do? This seemed
to work on some other fields.
Actually, I just tried this:
if (dr["Sex"] != DBNull.Value)
client.Sex = (int)dr["Sex"];
And I seem to get exactly the same error on the same line.
>In the database table, Sex has a data type=smallint
So the Sex field can have a range of values from -32,768 to 32,767...?
Hey, we like our site to support all types. ;-) Seriously, it's coming from
a RadioButtonList and just seemed to make more sense to use an int. Besides,
I don't see how a database can store a bit field using less than a byte
anyway.
--
Jonathan Wood
SoftCircuits Programming http://www.softcircuits.com
Actually, it looks like dr["Sex"] was NULL. But I thought I was testing for
that.
--
Jonathan Wood
SoftCircuits Programming http://www.softcircuits.com
"Peter Bromberg [C# MVP]" <pb*******@yahoo.NoSpamMaam.comwrote in message
news:23**********************************@microsof t.com...
What type is client.Sex? Looks like whatever it is, it cannot accept int
as a
value. Unless the Convert of the (int)dr["Sex"] is failing because it
itself is not an integer.
-- Peter
Site: http://www.eggheadcafe.com
UnBlog: http://petesbloggerama.blogspot.com
MetaFinder: http://www.blogmetafinder.com
"Jonathan Wood" wrote:
>The following code raises the error "Specified cast is not valid."
MembershipUser user = Membership.GetUser(userId); DataSet ds = DataLayer.ExecQueryData("SELECT * FROM mc_Clients WHERE UserId='" + userId.ToString() + "'"); if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0) return null; ClientEx client = new ClientEx(); client.ClientData = new Client(); client.ClientData.Email = user.UserName; client.ClientData.UserID = user.ProviderUserKey; client.ClientData.LastLogin = user.LastLoginDate; DataRow dr = ds.Tables[0].Rows[0];
// ...
if (!Convert.IsDBNull(dr["Sex"])) client.Sex = (int)dr["Sex"]; <<== ERROR HERE!
// ...
In the database table, Sex has a data type=smallint and nullable=True, and client.Sex has a data type of int.
If I attempt to examine the contents of dr["Sex"] in the debugger, it shows a value of 0x0000. If I attempt to examine the contents of (int)dr["Sex"] in the debugger, it shows an error about not being able to unbox (sorry I no longer have the exact text).
Any suggestions?
-- Jonathan Wood SoftCircuits Programming http://www.softcircuits.com
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:u4******************@TK2MSFTNGP03.phx.gbl...
>>The following code raises the error "Specified cast is not valid."
Yes, it would do...
>> if (!Convert.IsDBNull(dr["Sex"]))
if (dr["Sex"] != DBNull.Value)
So what does that mean? Does that mean dr["Sex"] was, in fact, null but my
check was wrong?
I believe so...
And what the heck does Convert.IsDBNull() do? This seemed to work on some
other fields.
It (supposedly) does exactly the same thing: http://msdn2.microsoft.com/en-us/lib....isdbnull.aspx
but I've found it to be an extremely unreliable way of checking for a null
value in a database field...
Actually, I just tried this:
if (dr["Sex"] != DBNull.Value)
client.Sex = (int)dr["Sex"];
And I seem to get exactly the same error on the same line.
Hmm - OK... Set a breakpoint on the first line above and, in the Immediate
window, inspect dr["Sex"]
>>In the database table, Sex has a data type=smallint
So the Sex field can have a range of values from -32,768 to 32,767...?
Hey, we like our site to support all types. ;-) Seriously, it's coming
from a RadioButtonList and just seemed to make more sense to use an int.
I don't understand the thinking behind that at all...
Besides, I don't see how a database can store a bit field using less than
a byte anyway.
SQL Server (and several other RDBMS) can: http://msdn2.microsoft.com/en-us/lib...atype.bit.aspx
My slightly tongue-in-cheek point was that you're using a larger datatype
than is necessary... For gender, I tend to use char(1)...
--
Mark Rae
ASP.NET MVP http://www.markrae.net
I figured this out. It only works if I cast using (short) insteat of (int).
I have no idea why that is--converting from a 16-bit integer to a 32-bit
integer is a trivial task, in fact one that the compiler still does after my
change.
--
Jonathan Wood
SoftCircuits Programming http://www.softcircuits.com
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:%2******************@TK2MSFTNGP04.phx.gbl...
The following code raises the error "Specified cast is not valid."
MembershipUser user = Membership.GetUser(userId);
DataSet ds = DataLayer.ExecQueryData("SELECT * FROM mc_Clients WHERE
UserId='" + userId.ToString() + "'");
if (ds == null || ds.Tables.Count == 0 || ds.Tables[0].Rows.Count == 0)
return null;
ClientEx client = new ClientEx();
client.ClientData = new Client();
client.ClientData.Email = user.UserName;
client.ClientData.UserID = user.ProviderUserKey;
client.ClientData.LastLogin = user.LastLoginDate;
DataRow dr = ds.Tables[0].Rows[0];
// ...
if (!Convert.IsDBNull(dr["Sex"]))
client.Sex = (int)dr["Sex"]; <<== ERROR HERE!
// ...
In the database table, Sex has a data type=smallint and nullable=True, and
client.Sex has a data type of int.
If I attempt to examine the contents of dr["Sex"] in the debugger, it
shows a value of 0x0000. If I attempt to examine the contents of
(int)dr["Sex"] in the debugger, it shows an error about not being able to
unbox (sorry I no longer have the exact text).
Any suggestions?
--
Jonathan Wood
SoftCircuits Programming http://www.softcircuits.com
Mark,
>And I seem to get exactly the same error on the same line.
Hmm - OK... Set a breakpoint on the first line above and, in the Immediate
window, inspect dr["Sex"]
I described that in my original post. At any rate, the fix was to type cast
to a short instead of an int, which doesn't seem like it should be the case.
>Besides, I don't see how a database can store a bit field using less than a byte anyway.
SQL Server (and several other RDBMS) can:
http://msdn2.microsoft.com/en-us/lib...atype.bit.aspx
My slightly tongue-in-cheek point was that you're using a larger datatype
than is necessary... For gender, I tend to use char(1)...
I didn't see where that link talked about the actual amount of storage used
to store bits in the database, I suppose it's possible to optimize bit
fields if they are stored contiguously, rather than with the other fields in
the same row, but I wouldn't have thought that's how it's done. At any rate,
I've used both char(1) and bit but I'm more comfortable using smallint in
this particular case.
--
Jonathan Wood
SoftCircuits Programming http://www.softcircuits.com
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:uZ**************@TK2MSFTNGP05.phx.gbl...
Perhaps a byte would be better
Yes it would - or a char(1)...
isn't that a tinyint?
Yes - a tinyint and a char(1) both occupy one byte...
a single byte is the least amount of storage space that most fields could
occupy.
Correct, apart from bit fields for boolean values, but that doesn't apply
here...
--
Mark Rae
ASP.NET MVP http://www.markrae.net
Changed to a tinyint, along with several other fields based on CheckBoxList
controls (some with several different options).
Thanks.
--
Jonathan Wood
SoftCircuits Programming http://www.softcircuits.com
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:u6**************@TK2MSFTNGP05.phx.gbl...
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:uZ**************@TK2MSFTNGP05.phx.gbl...
>Perhaps a byte would be better
Yes it would - or a char(1)...
>isn't that a tinyint?
Yes - a tinyint and a char(1) both occupy one byte...
>a single byte is the least amount of storage space that most fields could occupy.
Correct, apart from bit fields for boolean values, but that doesn't apply
here...
--
Mark Rae
ASP.NET MVP http://www.markrae.net
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:eH**************@TK2MSFTNGP03.phx.gbl...
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:u6**************@TK2MSFTNGP05.phx.gbl...
>"Jonathan Wood" <jw***@softcircuits.comwrote in message news:uZ**************@TK2MSFTNGP05.phx.gbl...
>>Perhaps a byte would be better
Yes it would - or a char(1)...
>>isn't that a tinyint?
Yes - a tinyint and a char(1) both occupy one byte...
>>a single byte is the least amount of storage space that most fields could occupy.
Correct, apart from bit fields for boolean values, but that doesn't apply here...
Changed to a tinyint, along with several other fields based on
CheckBoxList controls (some with several different options).
So, how do you tell the difference between the two genders when you're using
a tinyint...?
--
Mark Rae
ASP.NET MVP http://www.markrae.net
Mark,
>Changed to a tinyint, along with several other fields based on CheckBoxList controls (some with several different options).
So, how do you tell the difference between the two genders when you're
using a tinyint...?
Not sure I understand the question. As it is, Male is the first option so
Male == 0 and Female == 1. I tried creating enums for this (and the other
options) but stupid C# has problems with if (i == sexesMale). Oh well, I can
write code that simply tests the value for being 0 or 1.
--
Jonathan Wood
SoftCircuits Programming http://www.softcircuits.com
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:OR**************@TK2MSFTNGP05.phx.gbl...
>So, how do you tell the difference between the two genders when you're using a tinyint...?
Not sure I understand the question. As it is, Male is the first option so
Male == 0 and Female == 1.
That seems totally unnatural and unintuitive to me...
When you use char(1), male is 'M' and female is 'F'...
A query like SELECT * FROM MyTable WHERE Sex = 0 just seems really weird...
--
Mark Rae
ASP.NET MVP http://www.markrae.net
Hmm... I'm not sure how long you've been programming but it doesn't seem at
all odd to me. At any rate, it's simply used for a couple of calculations
and on a report. There will be no queries based on it.
--
Jonathan Wood
SoftCircuits Programming http://www.softcircuits.com
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:OR**************@TK2MSFTNGP05.phx.gbl...
>>So, how do you tell the difference between the two genders when you're using a tinyint...?
Not sure I understand the question. As it is, Male is the first option so Male == 0 and Female == 1.
That seems totally unnatural and unintuitive to me...
When you use char(1), male is 'M' and female is 'F'...
A query like SELECT * FROM MyTable WHERE Sex = 0 just seems really
weird...
--
Mark Rae
ASP.NET MVP http://www.markrae.net
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:eS**************@TK2MSFTNGP02.phx.gbl...
Hmm... I'm not sure how long you've been programming but it doesn't seem
at all odd to me. At any rate, it's simply used for a couple of
calculations and on a report. There will be no queries based on it.
if the values are 0 and 1 then you should use bit
0 = false
1 = true
entred as you would a int
INSERT INTO dataTypes (aBit)Values(1)
>
--
Jonathan Wood
SoftCircuits Programming http://www.softcircuits.com
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>"Jonathan Wood" <jw***@softcircuits.comwrote in message news:OR**************@TK2MSFTNGP05.phx.gbl...
>>>So, how do you tell the difference between the two genders when you're using a tinyint...?
Not sure I understand the question. As it is, Male is the first option so Male == 0 and Female == 1.
That seems totally unnatural and unintuitive to me...
When you use char(1), male is 'M' and female is 'F'...
A query like SELECT * FROM MyTable WHERE Sex = 0 just seems really weird...
-- Mark Rae ASP.NET MVP http://www.markrae.net
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:eS**************@TK2MSFTNGP02.phx.gbl...
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP06.phx.gbl...
>"Jonathan Wood" <jw***@softcircuits.comwrote in message news:OR**************@TK2MSFTNGP05.phx.gbl...
>>>So, how do you tell the difference between the two genders when you're using a tinyint...?
Not sure I understand the question. As it is, Male is the first option so Male == 0 and Female == 1.
That seems totally unnatural and unintuitive to me...
When you use char(1), male is 'M' and female is 'F'...
A query like SELECT * FROM MyTable WHERE Sex = 0 just seems really weird...
I'm not sure how long you've been programming
LOL! About 30 years or so...
--
Mark Rae
ASP.NET MVP http://www.markrae.net
"ThatsIT.net.au" <me@thatsitwrote in message
news:B5**********************************@microsof t.com...
if the values are 0 and 1 then you should use bit
Unless the field is used in any query criteria, as bit fields can't be
indexed...
--
Mark Rae
ASP.NET MVP http://www.markrae.net
Okay, so we're in similar boats there. It just seemed strange to me that it
would seem strange to you to use a small integer to store one of two
possible values.
--
Jonathan Wood
SoftCircuits Programming http://www.softcircuits.com
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:%2****************@TK2MSFTNGP04.phx.gbl...
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:eS**************@TK2MSFTNGP02.phx.gbl...
>"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message news:%2****************@TK2MSFTNGP06.phx.gbl...
>>"Jonathan Wood" <jw***@softcircuits.comwrote in message news:OR**************@TK2MSFTNGP05.phx.gbl...
So, how do you tell the difference between the two genders when you're using a tinyint...?
Not sure I understand the question. As it is, Male is the first option so Male == 0 and Female == 1.
That seems totally unnatural and unintuitive to me...
When you use char(1), male is 'M' and female is 'F'...
A query like SELECT * FROM MyTable WHERE Sex = 0 just seems really weird...
I'm not sure how long you've been programming
LOL! About 30 years or so...
--
Mark Rae
ASP.NET MVP http://www.markrae.net
How much memory do you think a bit field takes up in a database? Unless many
bit fields are stored contiguosly, packed into bytes, each bit field would
still take up one byte of storage each. So why is it so important to use a
bit field rather than a byte field?
--
Jonathan Wood
SoftCircuits Programming http://www.softcircuits.com
"ThatsIT.net.au" <me@thatsitwrote in message
news:B5**********************************@microsof t.com...
>
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:eS**************@TK2MSFTNGP02.phx.gbl...
>Hmm... I'm not sure how long you've been programming but it doesn't seem at all odd to me. At any rate, it's simply used for a couple of calculations and on a report. There will be no queries based on it.
if the values are 0 and 1 then you should use bit
0 = false
1 = true
entred as you would a int
INSERT INTO dataTypes (aBit)Values(1)
>> -- Jonathan Wood SoftCircuits Programming http://www.softcircuits.com
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message news:%2****************@TK2MSFTNGP06.phx.gbl...
>>"Jonathan Wood" <jw***@softcircuits.comwrote in message news:OR**************@TK2MSFTNGP05.phx.gbl...
So, how do you tell the difference between the two genders when you're using a tinyint...?
Not sure I understand the question. As it is, Male is the first option so Male == 0 and Female == 1.
That seems totally unnatural and unintuitive to me...
When you use char(1), male is 'M' and female is 'F'...
A query like SELECT * FROM MyTable WHERE Sex = 0 just seems really weird...
-- Mark Rae ASP.NET MVP http://www.markrae.net
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:em**************@TK2MSFTNGP06.phx.gbl...
"ThatsIT.net.au" <me@thatsitwrote in message
news:B5**********************************@microsof t.com...
>if the values are 0 and 1 then you should use bit
Unless the field is used in any query criteria, as bit fields can't be
indexed...
How do you mean?
I just made a query against a bit field
>
--
Mark Rae
ASP.NET MVP http://www.markrae.net
"ThatsIT.net.au" <me@thatsitwrote in message
news:E1**********************************@microsof t.com...
>>if the values are 0 and 1 then you should use bit
Unless the field is used in any query criteria, as bit fields can't be indexed...
How do you mean?
The bit datatype is not suitable for indexing, so should not be used for for
fields which are participate in query criteria i.e. WHERE clauses... http://sqlserver2000.databases.aspfa...it-column.html
--
Mark Rae
ASP.NET MVP http://www.markrae.net
re:
!How do you mean?
!I just made a query against a bit field
He said "not indexable"...not "not queryable".
You might run into query efficiency problems if the field isn't indexed.
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
"ThatsIT.net.au" <me@thatsitwrote in message news:E1**********************************@microsof t.com...
>
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message news:em**************@TK2MSFTNGP06.phx.gbl...
>"ThatsIT.net.au" <me@thatsitwrote in message news:B5**********************************@microsof t.com...
>>if the values are 0 and 1 then you should use bit
Unless the field is used in any query criteria, as bit fields can't be indexed...
How do you mean?
I just made a query against a bit field
>> -- Mark Rae ASP.NET MVP http://www.markrae.net
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:uF**************@TK2MSFTNGP06.phx.gbl...
"ThatsIT.net.au" <me@thatsitwrote in message
news:E1**********************************@microsof t.com...
>>>if the values are 0 and 1 then you should use bit
Unless the field is used in any query criteria, as bit fields can't be indexed...
How do you mean?
The bit datatype is not suitable for indexing, so should not be used for
for fields which are participate in query criteria i.e. WHERE clauses... http://sqlserver2000.databases.aspfa...it-column.html
My understanding is you can still query it, but not index it. And as long as
the table is indexed on at least one column other then the bit performance
will be fine.
>
--
Mark Rae
ASP.NET MVP http://www.markrae.net
"Juan T. Llibre" <no***********@nowhere.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
re:
!How do you mean?
!I just made a query against a bit field
He said "not indexable"...not "not queryable".
You might run into query efficiency problems if the field isn't indexed.
Actualy he said
"Unless the field is used in any query criteria"
but your point is noted.
>
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
"ThatsIT.net.au" <me@thatsitwrote in message
news:E1**********************************@microsof t.com...
>> "Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message news:em**************@TK2MSFTNGP06.phx.gbl...
>>"ThatsIT.net.au" <me@thatsitwrote in message news:B5**********************************@micros oft.com...
if the values are 0 and 1 then you should use bit
Unless the field is used in any query criteria, as bit fields can't be indexed...
>How do you mean?
I just made a query against a bit field
>>> -- Mark Rae ASP.NET MVP http://www.markrae.net
ThatsIT.net.au,
>Or get a request to add support for an "unspecified" state.
why not change them to bits?
I mean lets be honest, I doubt using a int is going to rob you of all your
memory and cause a problem, but if you are going to change to a byte you
may as well change to a bit
For the reasons I gave in the post you're replying to. And, for me, a better
question would be "why change them to bits?"
It's working. It's simple. It requires no conversions. And I see absolutely
no downside to the approach I'm taking. You confirmed in your other post
what I suspected: that a byte uses exactly the same amount of space in the
database as a bit does. I'm open to hear about issues anyone still may have
about this, but they need to be specific about what is gained as I don't see
anything to be gained by changing this.
--
Jonathan Wood
SoftCircuits Programming http://www.softcircuits.com
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:%2****************@TK2MSFTNGP03.phx.gbl...
ThatsIT.net.au,
>>Or get a request to add support for an "unspecified" state.
why not change them to bits?
I mean lets be honest, I doubt using a int is going to rob you of all your memory and cause a problem, but if you are going to change to a byte you may as well change to a bit
For the reasons I gave in the post you're replying to. And, for me, a
better question would be "why change them to bits?"
Why change to byte?
>
It's working. It's simple. It requires no conversions. And I see
absolutely no downside to the approach I'm taking. You confirmed in your
other post what I suspected: that a byte uses exactly the same amount of
space in the database as a bit does. I'm open to hear about issues anyone
still may have about this, but they need to be specific about what is
gained as I don't see anything to be gained by changing this.
No that not correct, a bit uses a bit, 1/8th of a byte. if you have another
Boolean record to keep your ahead. In your case that may not be so, but I
would say that most of the time one would.
As it said the explanation I posted it is still better to use a bit as you
may need to add another bit field later
so knowing that, i would say Why not change to bit?
--
Jonathan Wood
SoftCircuits Programming http://www.softcircuits.com
"ThatsIT.net.au" <me@thatsitwrote in message
news:E3**********************************@microsof t.com...
>The bit datatype is not suitable for indexing, so should not be used for for fields which are participate in query criteria i.e. WHERE clauses... http://sqlserver2000.databases.aspfa...it-column.html
My understanding is you can still query it, but not index it.
That's correct - which is what I said...
And as long as the table is indexed on at least one column other then the
bit performance will be fine.
??? That's simply not the way databases work... E.g.
UserID int
FirstName varchar(30)
Surname varchar(30)
Gender bit
If you inded the Surname column, that will not help one bit when you execute
a query like: SELECT * FROM UserTable WHERE Gender = 0
How could it...?
--
Mark Rae
ASP.NET MVP http://www.markrae.net
ThatsIT.net.au,
>For the reasons I gave in the post you're replying to. And, for me, a better question would be "why change them to bits?"
Why change to byte?
Because that is the smallest integer possible. And it also happens to be the
smallest field size as well.
No that not correct, a bit uses a bit, 1/8th of a byte. if you have
another Boolean record to keep your ahead. In your case that may not be
so, but I would say that most of the time one would.
It would be 1 bit field. That's 8 bits. It's not the same as 1/8th of a
byte.
As it said the explanation I posted it is still better to use a bit as you
may need to add another bit field later
In fact, I could have up to 8 with the same storage. Big deal. With an
integer value stored in a byte, I could have up to 256 different values.
I already explained why using an integer was more straight forward. If you
got what I said, then you know the reason. If you didn't, then we're
probably just wasting time.
--
Jonathan Wood
SoftCircuits Programming http://www.softcircuits.com
Jonathan Wood wrote:
Not sure I understand the question. As it is, Male is the first
option so Male == 0 and Female == 1. I tried creating enums for this
(and the other options) but stupid C# has problems with if (i ==
sexesMale). Oh well, I can write code that simply tests the value for
being 0 or 1.
There's an ISO standard for the values: http://en.wikipedia.org/wiki/ISO_5218
Andrew
Heh, well there ya go. The ISO standard for storing the sex would appear to
require a small integer. Oh well, I'm not looking to follow any ISO
standard, but looks like my approach is not unprecedented.
--
Jonathan Wood
SoftCircuits Programming http://www.softcircuits.com
"Andrew Morton" <ak*@in-press.co.uk.invalidwrote in message
news:%2******************@TK2MSFTNGP02.phx.gbl...
Jonathan Wood wrote:
>Not sure I understand the question. As it is, Male is the first option so Male == 0 and Female == 1. I tried creating enums for this (and the other options) but stupid C# has problems with if (i == sexesMale). Oh well, I can write code that simply tests the value for being 0 or 1.
There's an ISO standard for the values: http://en.wikipedia.org/wiki/ISO_5218
Andrew
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:%2****************@TK2MSFTNGP05.phx.gbl...
Heh, well there ya go.
I stand corrected.
The ISO standard for storing the sex would appear to require a small
integer.
A byte, not a small integer.
--
Mark Rae
ASP.NET MVP http://www.markrae.net
Mark,
>The ISO standard for storing the sex would appear to require a small integer.
A byte, not a small integer.
After programming for 30 years, I'm sure you are well aware that a byte is
an integer type. In the context of 32 and 64-bit integers, I would consider
an 8-bit type to be a small one. Don't you agree?
--
Jonathan Wood
SoftCircuits Programming http://www.softcircuits.com
To go further, everything is an integer in computer world. Even strings are
:).
in SQL
int is 32 bit,
smallint is 16 bit,
byte is 8 bit.
PS: Did not know that there are standards for sex :).
George.
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:eq**************@TK2MSFTNGP06.phx.gbl...
Mark,
>>The ISO standard for storing the sex would appear to require a small integer.
A byte, not a small integer.
After programming for 30 years, I'm sure you are well aware that a byte is
an integer type. In the context of 32 and 64-bit integers, I would
consider an 8-bit type to be a small one. Don't you agree?
--
Jonathan Wood
SoftCircuits Programming http://www.softcircuits.com
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:eq**************@TK2MSFTNGP06.phx.gbl...
After programming for 30 years, I'm sure you are well aware that a byte is
an integer type. In the context of 32 and 64-bit integers, I would
consider an 8-bit type to be a small one. Don't you agree?
http://technet.microsoft.com/en-us/l.../ms187745.aspx
--
Mark Rae
ASP.NET MVP http://www.markrae.net
In a way, yes. <gBut, logically, strings are different, as are
floating-point values.
--
Jonathan Wood
SoftCircuits Programming http://www.softcircuits.com
"George Ter-Saakov" <gt****@cardone.comwrote in message
news:Oi**************@TK2MSFTNGP06.phx.gbl...
To go further, everything is an integer in computer world. Even strings
are :).
in SQL
int is 32 bit,
smallint is 16 bit,
byte is 8 bit.
PS: Did not know that there are standards for sex :).
George.
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:eq**************@TK2MSFTNGP06.phx.gbl...
>Mark,
>>>The ISO standard for storing the sex would appear to require a small integer.
A byte, not a small integer.
After programming for 30 years, I'm sure you are well aware that a byte is an integer type. In the context of 32 and 64-bit integers, I would consider an 8-bit type to be a small one. Don't you agree?
-- Jonathan Wood SoftCircuits Programming http://www.softcircuits.com
I wasn't referring to specific SQL data type names, if that's what this is
about. I was talking in general terms about using a small integer.
--
Jonathan Wood
SoftCircuits Programming http://www.softcircuits.com
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message
news:OL****************@TK2MSFTNGP06.phx.gbl...
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:eq**************@TK2MSFTNGP06.phx.gbl...
>After programming for 30 years, I'm sure you are well aware that a byte is an integer type. In the context of 32 and 64-bit integers, I would consider an 8-bit type to be a small one. Don't you agree?
http://technet.microsoft.com/en-us/l.../ms187745.aspx
--
Mark Rae
ASP.NET MVP http://www.markrae.net
Wow! Why did I have to login to Password to see that page ?
To make things a bit clearer, a byte doesn't equal a smallint,
but a tinyint...at least in SQL Server.
In VB.NET, an integer equals 32 bits (VB6's "Long").
Juan T. Llibre, asp.net MVP
asp.net faq : http://asp.net.do/faq/
foros de asp.net, en español : http://asp.net.do/foros/
======================================
"Mark Rae [MVP]" <ma**@markNOSPAMrae.netwrote in message news:OL****************@TK2MSFTNGP06.phx.gbl...
"Jonathan Wood" <jw***@softcircuits.comwrote in message news:eq**************@TK2MSFTNGP06.phx.gbl...
>After programming for 30 years, I'm sure you are well aware that a byte is an integer type. In the context of 32 and 64-bit integers, I would consider an 8-bit type to be a small one. Don't you agree?
http://technet.microsoft.com/en-us/l.../ms187745.aspx
--
Mark Rae
ASP.NET MVP http://www.markrae.net
"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:eG**************@TK2MSFTNGP06.phx.gbl...
ThatsIT.net.au,
>>For the reasons I gave in the post you're replying to. And, for me, a better question would be "why change them to bits?"
Why change to byte?
Because that is the smallest integer possible. And it also happens to be
the smallest field size as well.
>No that not correct, a bit uses a bit, 1/8th of a byte. if you have another Boolean record to keep your ahead. In your case that may not be so, but I would say that most of the time one would.
It would be 1 bit field. That's 8 bits. It's not the same as 1/8th of a
byte.
>As it said the explanation I posted it is still better to use a bit as you may need to add another bit field later
In fact, I could have up to 8 with the same storage. Big deal. With an
integer value stored in a byte, I could have up to 256 different values.
There is only 2 genders not 256
>
I already explained why using an integer was more straight forward. If you
got what I said, then you know the reason. If you didn't, then we're
probably just wasting time.
I know what you sad, but you did not make sense, it is not a numeric
question, there is only 2 answers, not 256
>
--
Jonathan Wood
SoftCircuits Programming http://www.softcircuits.com This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Jace Benson |
last post by:
Ok I have read alot of things on zend.com, php.net and other sites went
to the wikibooks to try to understand how to use a class. I have this
project I want to do that I am sure would work great...
|
by: AFN |
last post by:
I am running the code below to generate XML from a data table. But some
fields in the data table are Null for every record. Suppose field5 has a
null database value. I would expect to see:
...
|
by: Wayne Wengert |
last post by:
I am using a datareader (dr1) to read through rows returned from a query. I
am getting the error: "No data exists for the row/column." at the "If
IsDbNull..." in the code below. The field "Photo1"...
|
by: rdemyan via AccessMonster.com |
last post by:
My front-end code manually links to the backend file.
I was wondering what stops Access from linking to the system tables in the
backend file. Is it just by virtue that they are hidden?
This...
|
by: so many sites so little time |
last post by:
the table is head the colunm is called body
<?php //show_site.php
// This script retrieves blog entries from the database.
// Address error handing.
ini_set ('display_errors', 1);...
|
by: Wes Groleau |
last post by:
I was doing update statements in SQL Server 2000.
I have a table with over 16 million rows.
It came from several hundred delimited text files,
and two of the columns are file ID (int) and Line...
|
by: jehugaleahsa |
last post by:
Hello:
I had an hour-long discussion with my boss today. Last night, right
before I dozed off, I realized some of his code resulted in duplicate
processing. I tried to explain it to him and he...
|
by: Hazza |
last post by:
Hi, I am using PHP and mysql to create a website.
I am fairly new to PHP, and thus am grateful to anyone who helps!
Firstly I am running a homepage, that displays additional content if a user...
|
by: Ben Thomas |
last post by:
Hello,
I have the following code which I don't understand why it works :
#include <iostream>
using namespace std;
void DontWork (unsigned int& i) {
cout << i << endl;
}
|
by: taylorcarr |
last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |