Help With a Combo Problem
-
And what goes wrong ? =(int)this.cmbSubDiagnosis.SelectedValue; Is it possible that selectedValue is a string and you need Convert.ToInt16 instead of the cast ? Christian Graus - Microsoft MVP - C++
-
1. Change (int) to Convert.ToInt32( (like I said ) 2. Step through the debugger in future to see what's going on when you've got a line that blows up 3. Next time you ask a question, if you know where the code blows up, then give us the code, and either way, tell us what the error is you're getting. Christian Graus - Microsoft MVP - C++
-
1. Change (int) to Convert.ToInt32( (like I said ) 2. Step through the debugger in future to see what's going on when you've got a line that blows up 3. Next time you ask a question, if you know where the code blows up, then give us the code, and either way, tell us what the error is you're getting. Christian Graus - Microsoft MVP - C++
-
Its Still giving error at the same point and the error is An unhandled exception of type 'System.InvalidCastException' occurred in mscorlib.dll Additional information: Specified cast is not valid.
Step 2 - use the debugger to find why the Value can't cast to a number. Christian Graus - Microsoft MVP - C++
-
Step 2 - use the debugger to find why the Value can't cast to a number. Christian Graus - Microsoft MVP - C++
-
when i used the debugger it shows that the selectedvalue has "System.Data.DataRowView" value that is why its not converting it but what is the solution to it.
OK, so that's what you need to cast it to, and then get the value out of it. Christian Graus - Microsoft MVP - C++
-
OK, so that's what you need to cast it to, and then get the value out of it. Christian Graus - Microsoft MVP - C++
Casting to Int32 is also not working . It is giving same error Is there any other way to achive my aim.Now where should i look. I am doing nothing different. Is there any other way to fill the combo with the rows from table with their code as value member. or is there any otehr event which i can use.
-
Casting to Int32 is also not working . It is giving same error Is there any other way to achive my aim.Now where should i look. I am doing nothing different. Is there any other way to fill the combo with the rows from table with their code as value member. or is there any otehr event which i can use.
Instead of casting to Int32 you have to first cast to System.Data.DataRowView. If you have this you can use its indexer to access the element you need. After that cast this element to Int32 and you are done.
-
Instead of casting to Int32 you have to first cast to System.Data.DataRowView. If you have this you can use its indexer to access the element you need. After that cast this element to Int32 and you are done.
-
are you getting this problem only when the form is loading?? Afzal Hassen
-
As Im really in a hurry here just a small snippet:
DataRovwView drv = (DataRowView)this.cmbSubDiagnosis.SelectedValue;
int myIntValue = (int)drv[IndexOrNameOfTheColumnYouWishToAccess]; -
Casting to Int32 is also not working . It is giving same error Is there any other way to achive my aim.Now where should i look. I am doing nothing different. Is there any other way to fill the combo with the rows from table with their code as value member. or is there any otehr event which i can use.
Your core problem is that you're shoving a datarowview into the value, the problem really is when you set the data source. You need to tell the combo which property to use to set the value for each row. The problem has nothing to do with the event. You have a datarowview in the value, you cannot magically turn it into an int, you need to either get it out as a datarowview ( which is what it is ) and then work out where your number is or, preferably, you need to fix the code that sets the datasource, so that it does in fact contain an int as you were hoping. In general, these 'please help me, something blows up' posts are useless, look how long it took us to get to the heart of the problem. You should learn to use your debugger. You'll get your answer a lot quicker if you post something like 'this line throws an invalid cast exception and the value in the row that I'm trying to cast is a datarowview.' A few seconds in the debugger will tell you that, and as you learn C#, that's the path you'd follow to fix the problem, by first knowing what it is. Christian Graus - Microsoft MVP - C++
-
Your core problem is that you're shoving a datarowview into the value, the problem really is when you set the data source. You need to tell the combo which property to use to set the value for each row. The problem has nothing to do with the event. You have a datarowview in the value, you cannot magically turn it into an int, you need to either get it out as a datarowview ( which is what it is ) and then work out where your number is or, preferably, you need to fix the code that sets the datasource, so that it does in fact contain an int as you were hoping. In general, these 'please help me, something blows up' posts are useless, look how long it took us to get to the heart of the problem. You should learn to use your debugger. You'll get your answer a lot quicker if you post something like 'this line throws an invalid cast exception and the value in the row that I'm trying to cast is a datarowview.' A few seconds in the debugger will tell you that, and as you learn C#, that's the path you'd follow to fix the problem, by first knowing what it is. Christian Graus - Microsoft MVP - C++