By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
458,184 Members | 1,498 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 458,184 IT Pros & Developers. It's quick & easy.

Cannot implicitly convert type

P: n/a
I am getting an error (a few among many) for the
following lines of code:

retval[curRowNum].BrokerName = (( curRow["BrokerName"] ==
System.DBNull.Value ) ? SqlString.Null : (string)curRow["BrokerName"] );

retval[curRowNum].BrokerGroupId = (( curRow["BrokerGroupId"] ==
System.DBNull.Value ) ? SqlInt32.Null : (int)curRow["BrokerGroupId"] );

The errors are (in turn):

"Cannot implicitly convert type 'System.Data.SqlTypes.SqlString' to
'string'"
"Cannot implicitly convert type 'System.Data.SqlTypes.SqlInt32' to 'int'"

So I'm curious, where is it getting the idea that I am
*implicitly* tyring to convert the type? When it is very
apparent that I am *explicitly* trying to convert it. I've
done some searches on Google but couldn't come up
with an appropriate answer. I'm hoping you'll be able
to tell me what's going on.

thnx,
Christoph
Nov 16 '05 #1
Share this Question
Share on Google+
22 Replies


P: n/a
Christopher,

What are the defined data types of BrokerName and BrokerId?

Telmo Sampaio

"Christoph Boget" <jc*****@yahoo.com> wrote in message
news:eb**************@TK2MSFTNGP09.phx.gbl...
I am getting an error (a few among many) for the
following lines of code:

retval[curRowNum].BrokerName = (( curRow["BrokerName"] ==
System.DBNull.Value ) ? SqlString.Null : (string)curRow["BrokerName"] );

retval[curRowNum].BrokerGroupId = (( curRow["BrokerGroupId"] ==
System.DBNull.Value ) ? SqlInt32.Null : (int)curRow["BrokerGroupId"] );

The errors are (in turn):

"Cannot implicitly convert type 'System.Data.SqlTypes.SqlString' to
'string'"
"Cannot implicitly convert type 'System.Data.SqlTypes.SqlInt32' to 'int'"

So I'm curious, where is it getting the idea that I am
*implicitly* tyring to convert the type? When it is very
apparent that I am *explicitly* trying to convert it. I've
done some searches on Google but couldn't come up
with an appropriate answer. I'm hoping you'll be able
to tell me what's going on.

thnx,
Christoph

Nov 16 '05 #2

P: n/a
Christoph Boget <jc*****@yahoo.com> wrote:
I am getting an error (a few among many) for the
following lines of code:

retval[curRowNum].BrokerName = (( curRow["BrokerName"] ==
System.DBNull.Value ) ? SqlString.Null : (string)curRow["BrokerName"] );

retval[curRowNum].BrokerGroupId = (( curRow["BrokerGroupId"] ==
System.DBNull.Value ) ? SqlInt32.Null : (int)curRow["BrokerGroupId"] );

The errors are (in turn):

"Cannot implicitly convert type 'System.Data.SqlTypes.SqlString' to
'string'"
"Cannot implicitly convert type 'System.Data.SqlTypes.SqlInt32' to 'int'"

So I'm curious, where is it getting the idea that I am
*implicitly* tyring to convert the type? When it is very
apparent that I am *explicitly* trying to convert it. I've
done some searches on Google but couldn't come up
with an appropriate answer. I'm hoping you'll be able
to tell me what's going on.


I suspect that BrokerName is a string property - in which case, if the
condition is true, it's as if you're saying:

retval[curRowNum].BrokerName = SqlString.Null;

The only things you're *explicitly* converting are from object (as far
as the compiler knows) to string/int.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #3

P: n/a
> What are the defined data types of BrokerName and BrokerId?

'string' and 'int', respecitvely.

Christoph
Nov 16 '05 #4

P: n/a
> I suspect that BrokerName is a string property

It is.
- in which case, if the condition is true, it's as if you're saying:
retval[curRowNum].BrokerName = SqlString.Null;
I didn't think there would be an issue with setting a string property
to null. Or any data type to null. Is that not the case?
The only things you're *explicitly* converting are from object (as far
as the compiler knows) to string/int.


And 'String.Null' is an object? Not a data type?

thnx,
Christoph
Nov 16 '05 #5

P: n/a
Christoph Boget <jc*****@yahoo.com> wrote:
I suspect that BrokerName is a string property


It is.
- in which case, if the condition is true, it's as if you're saying:
retval[curRowNum].BrokerName = SqlString.Null;


I didn't think there would be an issue with setting a string property
to null. Or any data type to null. Is that not the case?


SqlString.Null is of type SqlString, not string, and it's not the same
as a null reference.
The only things you're *explicitly* converting are from object (as far
as the compiler knows) to string/int.


And 'String.Null' is an object? Not a data type?


SqlString.Null is a reference to a particular instance of SqlString.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #6

P: n/a
> The only things you're *explicitly* converting are from object (as far
as the compiler knows) to string/int.


I've tried changing the code to this:

_brokerGroupId = toReturn.Rows[0]["BrokerGroupId"] == System.DBNull.Value ?
null : (int)toReturn.Rows[0]["BrokerGroupId"];

so it's not converting from an object (presumably SqlInt32.Null?) but now
I'm getting the error:

"Type of conditional expression can't be determined because there is no
implicit
conversion between '<null>' and 'int'"

I just am not understanding this. Why is there an issue setting a data type
to
null? There shouldn't be one...

thnx,
Christoph
Nov 16 '05 #7

P: n/a
Hi,
In addition to Jon comments you also have the problem that the terniary
operator return a different type in the true and false statement, this is
not valid and you have to solve it. Maybe that is the "implicitiness"

It's an error any way :)

cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Christoph Boget <jc*****@yahoo.com> wrote:
I am getting an error (a few among many) for the
following lines of code:

retval[curRowNum].BrokerName = (( curRow["BrokerName"] ==
System.DBNull.Value ) ? SqlString.Null : (string)curRow["BrokerName"] );

retval[curRowNum].BrokerGroupId = (( curRow["BrokerGroupId"] ==
System.DBNull.Value ) ? SqlInt32.Null : (int)curRow["BrokerGroupId"] );

The errors are (in turn):

"Cannot implicitly convert type 'System.Data.SqlTypes.SqlString' to
'string'"
"Cannot implicitly convert type 'System.Data.SqlTypes.SqlInt32' to 'int'"
So I'm curious, where is it getting the idea that I am
*implicitly* tyring to convert the type? When it is very
apparent that I am *explicitly* trying to convert it. I've
done some searches on Google but couldn't come up
with an appropriate answer. I'm hoping you'll be able
to tell me what's going on.


I suspect that BrokerName is a string property - in which case, if the
condition is true, it's as if you're saying:

retval[curRowNum].BrokerName = SqlString.Null;

The only things you're *explicitly* converting are from object (as far
as the compiler knows) to string/int.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too

Nov 16 '05 #8

P: n/a
so... that answer your question...

"Christoph Boget" <jc*****@yahoo.com> wrote in message
news:u7**************@TK2MSFTNGP11.phx.gbl...
What are the defined data types of BrokerName and BrokerId?


'string' and 'int', respecitvely.

Christoph

Nov 16 '05 #9

P: n/a
Try this...

_brokerGroupId = (!toReturn.Rows[0].IsNull("BrokerGroupId") ?
(int)toReturn.Rows[0]["BrokerGroupId"] : -1);

There is no implicit conversion between null and int.

HTH,

Kyril

"Christoph Boget" <jc*****@yahoo.com> wrote in message
news:%2****************@TK2MSFTNGP11.phx.gbl...
The only things you're *explicitly* converting are from object (as far
as the compiler knows) to string/int.


I've tried changing the code to this:

_brokerGroupId = toReturn.Rows[0]["BrokerGroupId"] == System.DBNull.Value
?
null : (int)toReturn.Rows[0]["BrokerGroupId"];

so it's not converting from an object (presumably SqlInt32.Null?) but now
I'm getting the error:

"Type of conditional expression can't be determined because there is no
implicit
conversion between '<null>' and 'int'"

I just am not understanding this. Why is there an issue setting a data
type
to
null? There shouldn't be one...

thnx,
Christoph

Nov 16 '05 #10

P: n/a
Christoph Boget <jc*****@yahoo.com> wrote:
The only things you're *explicitly* converting are from object (as far
as the compiler knows) to string/int.


I've tried changing the code to this:

_brokerGroupId = toReturn.Rows[0]["BrokerGroupId"] == System.DBNull.Value ?
null : (int)toReturn.Rows[0]["BrokerGroupId"];

so it's not converting from an object (presumably SqlInt32.Null?) but now
I'm getting the error:

"Type of conditional expression can't be determined because there is no
implicit
conversion between '<null>' and 'int'"

I just am not understanding this. Why is there an issue setting a data type
to null? There shouldn't be one...


Yes there is, because int is a value type - and you can't set a value
type variable to null. If you want to box the int, you could use:

.... null : (object)(int)toReturn.Rows[0]["BrokerGroupId"];

which would get that part of the statement to compile, but then if
_brokerGroupId is a variable of type int, the assignment part will
fail.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #11

P: n/a
> so... that answer your question...

No, actually, it doesn't. Because I'm not *implicitly*
converting the types, I'm *explicitly* converting them.
So the error makes no sense.

thnx
Christoph
Nov 16 '05 #12

P: n/a
Christoph Boget <jc*****@yahoo.com> wrote:
so... that answer your question...


No, actually, it doesn't. Because I'm not *implicitly*
converting the types, I'm *explicitly* converting them.
So the error makes no sense.


It makes perfect sense, as I showed before. The problem is that the
conversion that it's talking about isn't the one you're performing
explicitly.

I suggest that to make it easier to understand why the compiler's
complaining, you take your one big statement and break it down into
lots of little ones with local variables. You'll then be able to very
easily see where the error is, if you still can't.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #13

P: n/a
You are assigning a SqlString to a regular String, and a SqlInt to a regular
Integer. These are your implicit conversions.

Telmo Sampaio

"Christoph Boget" <jc*****@yahoo.com> wrote in message
news:Ox**************@TK2MSFTNGP10.phx.gbl...
so... that answer your question...


No, actually, it doesn't. Because I'm not *implicitly*
converting the types, I'm *explicitly* converting them.
So the error makes no sense.

thnx
Christoph

Nov 16 '05 #14

P: n/a
> I suggest that to make it easier to understand why the compiler's
complaining, you take your one big statement and break it down into
lots of little ones with local variables. You'll then be able to very
easily see where the error is, if you still can't.


I broke the code down into the following:

if( curRow["BrokerGroupId"] == System.DBNull.Value )
{
// Error is on the line below
retval[curRowNum].BrokerGroupId = null;
}
else
{
retval[curRowNum].BrokerGroupId = (int)curRow["BrokerGroupId"];
}

and the error I get is the one mentioned previously:

"Type of conditional expression can't be determined because there is no
implicit conversion between '<null>' and 'int'"

It's still not apparent to me why I can't set an int data type to null.
Or, if I can, how. Because this just isn't working. You mentioned
"boxing" in your previous email but I don't see how I can box in
the above situation.

thnx,
Christoph
Nov 16 '05 #15

P: n/a
Christoph Boget wrote:
I am getting an error (a few among many) for the
following lines of code:

retval[curRowNum].BrokerName = (( curRow["BrokerName"] ==
System.DBNull.Value ) ? SqlString.Null : (string)curRow["BrokerName"] );

retval[curRowNum].BrokerGroupId = (( curRow["BrokerGroupId"] ==
System.DBNull.Value ) ? SqlInt32.Null : (int)curRow["BrokerGroupId"] );

The errors are (in turn):

"Cannot implicitly convert type 'System.Data.SqlTypes.SqlString' to
'string'"
"Cannot implicitly convert type 'System.Data.SqlTypes.SqlInt32' to 'int'"

So I'm curious, where is it getting the idea that I am
*implicitly* tyring to convert the type? When it is very
apparent that I am *explicitly* trying to convert it. I've
done some searches on Google but couldn't come up
with an appropriate answer. I'm hoping you'll be able
to tell me what's going on.

thnx,
Christoph


http://www.devhood.com/tutorials/tut...utorial_id=454

The chart at the bottom of this article lists the implicit datatype
conversions.

Nov 16 '05 #16

P: n/a
Christoph Boget <jc*****@yahoo.com> wrote:
I suggest that to make it easier to understand why the compiler's
complaining, you take your one big statement and break it down into
lots of little ones with local variables. You'll then be able to very
easily see where the error is, if you still can't.
I broke the code down into the following:

if( curRow["BrokerGroupId"] == System.DBNull.Value )
{
// Error is on the line below
retval[curRowNum].BrokerGroupId = null;
}
else
{
retval[curRowNum].BrokerGroupId = (int)curRow["BrokerGroupId"];
}

and the error I get is the one mentioned previously:

"Type of conditional expression can't be determined because there is no
implicit conversion between '<null>' and 'int'"


I doubt that you get that error now, as there's no conditional
expression in the above code.
It's still not apparent to me why I can't set an int data type to null.
Because it's a value type. You *really* need to know the basics of .NET
such as the differences between value types and reference types before
getting into ADO.NET though - you'll save yourself a lot of headaches
like this.
Or, if I can, how. Because this just isn't working. You mentioned
"boxing" in your previous email but I don't see how I can box in
the above situation.


You can't if you're trying to assign to an object type variable, you
could box the int - but I don't think that's going to help you here.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #17

P: n/a
> Because it's a value type. You *really* need to know the basics of .NET
such as the differences between value types and reference types before
getting into ADO.NET though - you'll save yourself a lot of headaches
like this.


Fair enough. I just couldn't see any way other than setting the variable
to null to essentially "reset" the variable/property to nothing such that it
has no value.

thnx,
Christoph
Nov 16 '05 #18

P: n/a
Christoph Boget <jc*****@yahoo.com> wrote:
Because it's a value type. You *really* need to know the basics of .NET
such as the differences between value types and reference types before
getting into ADO.NET though - you'll save yourself a lot of headaches
like this.


Fair enough. I just couldn't see any way other than setting the variable
to null to essentially "reset" the variable/property to nothing such that it
has no value.


There's no such concept as setting a variable to have no value. Even
the null value is a perfectly good value.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #19

P: n/a
Fair enough. I just couldn't see any way other than setting the variable to null to essentially "reset" the variable/property to nothing such that it has no value.

There's no such concept as setting a variable to have no value. Even
the null value is a perfectly good value.


So there is no way to "unset" a variable?

thnx,
Christoph
Nov 16 '05 #20

P: n/a
Christoph Boget <jc*****@yahoo.com> wrote:
There's no such concept as setting a variable to have no value. Even
the null value is a perfectly good value.


So there is no way to "unset" a variable?


No. Why would you want to?

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #21

P: n/a
> > > There's no such concept as setting a variable to have no value. Even
the null value is a perfectly good value.

So there is no way to "unset" a variable?

No. Why would you want to?


Well, as it is no doubt apparent by now, I'm trying to conver Sql* data
types to c# data types. I'm doing this in a data access class that is
reading
(and writing) data to a datastore. When reading a new record, if that new
record has a NULL value in a column where the previous record had data,
I need a way to "unset" the value of the class' member variable that
represents
that column. And since I'm having issues converting, say, SqlString.Null to
a string (as was evidenced throughout this thread) I was just hoping that
there
was some other way to "unset" the variable.

thnx,
Christoph
Nov 16 '05 #22

P: n/a
Christoph Boget <jc*****@yahoo.com> wrote:
> There's no such concept as setting a variable to have no value. Even
> the null value is a perfectly good value.
So there is no way to "unset" a variable?

No. Why would you want to?


Well, as it is no doubt apparent by now, I'm trying to conver Sql*
data types to c# data types. I'm doing this in a data access class
that is reading (and writing) data to a datastore. When reading a new
record, if that new record has a NULL value in a column where the
previous record had data, I need a way to "unset" the value of the
class' member variable that represents that column. And since I'm
having issues converting, say, SqlString.Null to a string (as was
evidenced throughout this thread) I was just hoping that there was
some other way to "unset" the variable.


No - you need to have a value which represents NULL, basically -
whichis exactly what SqlInt.Null is for, but it only applies to the
SqlInt type.

The good news for you is that C# is going to get nullable value types
(I'm not sure at what performance/space penalty) - the bad news is that
Whidbey is still a fair way from being released.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet
If replying to the group, please do not mail me too
Nov 16 '05 #23

This discussion thread is closed

Replies have been disabled for this discussion.