473,508 Members | 2,241 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

System.InvalidCastException with UserControl and comboBox

I have a csharp, VS 2003 solution for a winform application
The application uses an object called a "matter" and the class is
defined in matter.cs.

I can load matter objects into a combobox
Matter[] matters= v.Matters;
comboBox1.Items.AddRange(matters);
And I can fetch the matter object after a selection:

private void comboBox1_SelectedIndexChanged(object sender,
System.EventArgs e)

{
Matter m;
m= (Matter) comboBox1.SelectedItem;
Debug.WriteLine(m.GetType().ToString()); // --> MIDE.Matter
Debug.WriteLine(m.Name); // this works ok
Debug.WriteLine(m.ID); // this works ok
}

All is working well-
Now I want a UserControl which also has a combobox which is loaded
with a set of Matters.
I add a new project in the solution for my UserControl & I ctrl-drag
matter.cs into the UserControl's solution.

In my UserControl, I can populate & display the combox fine, but when
I go to fetch the object out of it, I get a
System.InvalidCastException.
Note that I can get the object from the combobox as an object, the
object tells me it the proper type, and I can run the objects
ToString() fine, but my cast is dying.
in the user control --

private void cboMatterName_SelectedIndexChanged(object sender,
System.EventArgs e)

{
object o;
o= cboMatterName.SelectedItem;

Debug.WriteLine(o.GetType().ToString()); // --> MIDE.Matter
Debug.WriteLine(o.ToString()); // This works ok

Matter m;

// *** this throws a System.InvalidCastException ***
m= (Matter) cboMatterName.SelectedItem;

}

Any ideas?

thanx
Bob
Nov 15 '05 #1
1 3668
Check SelectedItem.GetType().GUID against typeof(Matter).GUID before the
cast that throws the exception. Are they the same?

If your projects are referencing each other, it may be that your ComboBox
has Matter objects from one project, however you are casting it to a Matter
from the UserControl project. This would cause your InvalidCastException.

Regards,
Matt

"bob scola" <sc***@scn.org> wrote in message
news:55*************************@posting.google.co m...
I have a csharp, VS 2003 solution for a winform application
The application uses an object called a "matter" and the class is
defined in matter.cs.

I can load matter objects into a combobox
Matter[] matters= v.Matters;
comboBox1.Items.AddRange(matters);
And I can fetch the matter object after a selection:

private void comboBox1_SelectedIndexChanged(object sender,
System.EventArgs e)

{
Matter m;
m= (Matter) comboBox1.SelectedItem;
Debug.WriteLine(m.GetType().ToString()); // --> MIDE.Matter
Debug.WriteLine(m.Name); // this works ok
Debug.WriteLine(m.ID); // this works ok
}

All is working well-
Now I want a UserControl which also has a combobox which is loaded
with a set of Matters.
I add a new project in the solution for my UserControl & I ctrl-drag
matter.cs into the UserControl's solution.

In my UserControl, I can populate & display the combox fine, but when
I go to fetch the object out of it, I get a
System.InvalidCastException.
Note that I can get the object from the combobox as an object, the
object tells me it the proper type, and I can run the objects
ToString() fine, but my cast is dying.
in the user control --

private void cboMatterName_SelectedIndexChanged(object sender,
System.EventArgs e)

{
object o;
o= cboMatterName.SelectedItem;

Debug.WriteLine(o.GetType().ToString()); // --> MIDE.Matter
Debug.WriteLine(o.ToString()); // This works ok

Matter m;

// *** this throws a System.InvalidCastException ***
m= (Matter) cboMatterName.SelectedItem;

}

Any ideas?

thanx
Bob

Nov 15 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
1446
by: codymanix | last post by:
My problem is: I have comboboxes which resides in a usercontrol. Always when the usercontrol changes its visible status (the dialog is shown or you switch to the tabpage where the usercontrol...
2
2078
by: John B | last post by:
Hello I want to create a set of controls that have some common methods and properties. I was thinking that I should subclass the UserControl class and add these common features there and then...
4
2647
by: DOTNET | last post by:
Hi, Anybody help me regarding this error: I am assigning the values to the session variables when the button is clicked and passing these session variables to the next page and when I am...
0
1061
by: Ray Cassick \(Home\) | last post by:
Ok, I am about at the end of my rope here. Been looking at this code for hours and can not see what is wrong with it. I have an object saved to a file using Binary Serialization. The file has...
8
4130
by: ChrisK | last post by:
Hi, I'm beginner with VB.net and i have a trouble... At work, they decided to program with tabcontrol...and i'mm in charge of this ! There is my problem. I've a form with one tabcontrol (TBC1...
1
2126
by: Jochen Berger | last post by:
Moin! I've 'composed' a special UserControl I need for my current project. On this Control I put a ComboBox. The user is not allowed to access the ComboBox directly (to protect some properties),...
9
15346
by: davidr | last post by:
Hi, I've researched this a lot and have a well understanding but still can't get the darn textbox in the javascript. I have a textbox that is set to runat="server" and I have a client...
4
1833
by: rsdev | last post by:
Hi, I have an InvalidCastException which is completely puzzling me. I have checked all the members in the stored procedure against my data provider and seems to be ok. Also in the stack trace it...
6
1925
by: Johnny Jörgensen | last post by:
I've got a usercontrol derived from a normal ComboBox that contains some special formatting code. On my main form I've got a lot of my custom comboboxes. I discovered a bug in the derived...
0
7326
Oralloy
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,...
0
7383
jinu1996
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...
1
7046
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
5627
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5053
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4707
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3194
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
1
766
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
418
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.