int casting error when trying to retrieve value from comboBox

  • Thread starter Thread starter Dica
  • Start date Start date
D

Dica

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
 
short iProjectID = Convert.ToInt16(cboProjects.SelectedValue);

If the combobox contains strings, try

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


Mattias
 
Are you sure that your SelectedValue property is actually set to something?
If so... what?

Brendan
 
Mattias Sjögren said:
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.
 
Brendan Grant said:
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?
 
try return it from the query:

select 0, 'Select Project'
union
select id, description
from tablename
 
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
 
Dica said:
What does

Int32

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

(int) cboProjects.SelectedValue
tried it. same error.


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

The above, I believe.
 
Mattias Sjögren said:
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

just cast to int directly to unbox).

not sure what you mean by 'cast to int directly to unbox'.
 
Jon Skeet said:
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
 
Kevin said:
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?
 
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
 
Kevin said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Back
Top