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

int casting error when trying to retrieve value from comboBox

P: n/a
i can't seem to find a way to cast the value in my comboBox to integer. i've
tried the following:
short iProjectID = Convert.ToInt16(cboProjects.SelectedValue);

i keep getting a "Sepcified cast is not allowed" error. how can i do this?

tks
Nov 17 '05 #1
Share this Question
Share on Google+
12 Replies


P: n/a
>short iProjectID = Convert.ToInt16(cboProjects.SelectedValue);

If the combobox contains strings, try

short iProjectID = Convert.ToInt16((string)cboProjects.SelectedValue) ;
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #2

P: n/a
Are you sure that your SelectedValue property is actually set to something?
If so... what?

Brendan
"Dica" wrote:
i can't seem to find a way to cast the value in my comboBox to integer. i've
tried the following:
short iProjectID = Convert.ToInt16(cboProjects.SelectedValue);

i keep getting a "Sepcified cast is not allowed" error. how can i do this?

tks

Nov 17 '05 #3

P: n/a

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:u5**************@TK2MSFTNGP14.phx.gbl...
short iProjectID = Convert.ToInt16(cboProjects.SelectedValue);
If the combobox contains strings, try

short iProjectID = Convert.ToInt16((string)cboProjects.SelectedValue) ;


still doesn't work. selectedValue is an integer from a dataSet. i've still
got a cast error.


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 17 '05 #4

P: n/a

"Brendan Grant" <gr****@NOSPAMdahat.com> wrote in message
news:30**********************************@microsof t.com...
Are you sure that your SelectedValue property is actually set to something? If so... what?
the value is a int column from my dataSet. i've revised the code to ensure
the selectedValue is set to something as follows:
if (cboProjects.SelectedIndex > 0)

{

short iProjectID = Convert.ToInt16((String)cboProjects.SelectedValue) ;

updateTasksList(iProjectID);

}

this is still giving me a cast error. i'm checking that the selectedIndex
is greated than 0 since i intend to put a "Select Proiject" option into the
list at index 0. this raises other problems, however. i first bound my
comboBox to a dataSet and then try inserting the "Select Project" option and
get a new error:

"Additional information: Cannot modify the Items collection when the
DataSource property is set."

how can i do this?


Brendan
"Dica" wrote:
i can't seem to find a way to cast the value in my comboBox to integer. i've tried the following:
short iProjectID = Convert.ToInt16(cboProjects.SelectedValue);

i keep getting a "Sepcified cast is not allowed" error. how can i do this?
tks

Nov 17 '05 #5

P: n/a
try return it from the query:

select 0, 'Select Project'
union
select id, description
from tablename

Nov 17 '05 #6

P: n/a
selectedValue is an integer from a dataSet.


What kind of integer (int, uint, short etc)? What does
cboProjects.SelectedValue.GetType().Name return? If for example it's
an int, you should use Convert.ToInt32 instead of ToInt16 (or even
easier, just cast to int directly to unbox).
Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.
Nov 17 '05 #7

P: n/a

"Mattias Sjögren" <ma********************@mvps.org> wrote in message
news:OF**************@TK2MSFTNGP12.phx.gbl...
selectedValue is an integer from a dataSet.
What kind of integer (int, uint, short etc)?

it's int (it's the UID column in my table)

What does cboProjects.SelectedValue.GetType().Name return?
Int32

If for example it's an int, you should use Convert.ToInt32 instead of ToInt16
tried it. same error.

(or even easier,
just cast to int directly to unbox).

not sure what you mean by 'cast to int directly to unbox'.


Mattias

--
Mattias Sjögren [MVP] mattias @ mvps.org
http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
Please reply only to the newsgroup.

Nov 17 '05 #8

P: n/a
Dica <ge*****@hotmail.com> wrote:
What does
cboProjects.SelectedValue.GetType().Name return?


Int32


In that case, you shouldn't be casting it to a string. Just use:

(int) cboProjects.SelectedValue
If for example it's
an int, you should use Convert.ToInt32 instead of ToInt16


tried it. same error.
(or even easier, just cast to int directly to unbox).


not sure what you mean by 'cast to int directly to unbox'.


The above, I believe.

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

P: n/a

"Jon Skeet [C# MVP]" <sk***@pobox.com> wrote in message
news:MP************************@msnews.microsoft.c om...
Dica <ge*****@hotmail.com> wrote:
What does
cboProjects.SelectedValue.GetType().Name return?
Int32


In that case, you shouldn't be casting it to a string. Just use:

(int) cboProjects.SelectedValue


ugh. i seem to have a gift for making things more complicated than they need
to be. your sufggstion works just fine.

tks jon

If for example it's
an int, you should use Convert.ToInt32 instead of ToInt16


tried it. same error.
(or even easier, just cast to int directly to unbox).


not sure what you mean by 'cast to int directly to unbox'.


The above, I believe.

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

Nov 17 '05 #10

P: n/a

"Kevin" <ke***@inatrice.co.za> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
try return it from the query:

select 0, 'Select Project'
union
select id, description
from tablename
the stored proc gets used in other apps as well, such as our web site, so if
i modify it with a union, our web page and other apps where 'Select Project'
has been hard coded into the list will show this option twice. i suppose i
could create another copy of the storedProc and use union in that one, but
it seems like a unnecessary waste of space for one tiny variation. is there
any other way to pull this off?


Nov 17 '05 #11

P: n/a
Dica,

You could add a default parameter to the existing sp. (code below)
This shouldn't break client code. Simply send a value above 0 as a
parameter to the sp and you should get your version. if no parameter is
sent then the existing select will be returned.

If you really don't want to update the sp then you could also add an
extra datarow manually to the datatable before the binding
operation.(Code Below).

Haven't had time to test the code examples but I'm sure you get the
idea.
--northwind database
CREATE PROCEDURE dbo.test

@firstparam int = 0

AS

if (@firstparam = 0)
begin
select employeeid,lastname
from employees
end
else
begin
select 0 employeeid,'Select Project' lastname
union
select employeeid,lastname
from employees
end
OR IN CODE

DO THIS BEFORE BINDING.

DataSet ds = (Retrieve DataSet Code);

DataRow ndr = ds.Tables[0].NewRow();
ndr[0] = 0;
ndr[1] = "Select Project";
ds.Tables[0].Rows.Add(ndr);

//Bind dataset to control in normal fashion

Nov 17 '05 #12

P: n/a

"Kevin" <ke***@inatrice.co.za> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Dica,

You could add a default parameter to the existing sp. (code below)
This shouldn't break client code. Simply send a value above 0 as a
parameter to the sp and you should get your version. if no parameter is
sent then the existing select will be returned.
if no parameter is sent, my classic ASP apps are going to crap out. if
storedProcs supported things like method overloading, this might be an
option. otherwise, failing to pass a storedProc a parameter will generate a
'The stored procedure whatever excected parameter blah' error.

If you really don't want to update the sp then you could also add an
extra datarow manually to the datatable before the binding
operation.(Code Below).

Haven't had time to test the code examples but I'm sure you get the
idea.
--northwind database
CREATE PROCEDURE dbo.test

@firstparam int = 0

AS

if (@firstparam = 0)
begin
select employeeid,lastname
from employees
end
else
begin
select 0 employeeid,'Select Project' lastname
union
select employeeid,lastname
from employees
end
OR IN CODE

DO THIS BEFORE BINDING.

DataSet ds = (Retrieve DataSet Code);

DataRow ndr = ds.Tables[0].NewRow();
ndr[0] = 0;
ndr[1] = "Select Project";
ds.Tables[0].Rows.Add(ndr);

//Bind dataset to control in normal fashion
ah, yes, this is the sort of thing i was looking for. thanks kevin - much
appreciated.


Nov 17 '05 #13

This discussion thread is closed

Replies have been disabled for this discussion.