Lỗi specified cast is not valid trong visual studio 2023 năm 2024

I am just using a table visual to show data stored in oracle table, which has about 20 columns. I pulled in 10 columns which includes some decimal data type columns, but after that from the remaining columns when I try to pull in decimal columns I get below error

Product Version: 2.88.1144.0 (20.12) (x64)

Error Message: Specified cast is not valid.. The exception was raised by the IDataReader interface. Please review the error message and provider documentation for further information and corrective action.

OS Version: Microsoft Windows NT 10.0.18363.0 (x64 en-US)

CLR Version: 4.7 or later [Release Number = 528040]

Peak Virtual Memory: 38.6 GB

Private Memory: 846 MB

why is this happening ? I never encountered this before

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

That's an assignment, not a comparison - so the result that you are converting with ToBoolean is probably not what you expected. Probably, you meant "==" rather than "=".

But the problem is almost certainly the cast of the Value property to a bool - use the debugger to find out exactly what the cell contains, and then start look for what it should be. Chances are you are looking at the wrong column, or a empty row at the bottom of the DGV.

And your shouldn't really be using the DGV itself, but the DataSource from which it was filled; and not using "magic numbers" like "22" anyway.

Disclaimer: The information provided on DevExpress.com and affiliated web properties (including the DevExpress Support Center) is provided "as is" without warranty of any kind. Developer Express Inc disclaims all warranties, either express or implied, including the warranties of merchantability and fitness for a particular purpose. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Confindential Information: Developer Express Inc does not wish to receive, will not act to procure, nor will it solicit, confidential or proprietary materials and information from you through the DevExpress Support Center or its web properties. Any and all materials or information divulged during chats, email communications, online discussions, Support Center tickets, or made available to Developer Express Inc in any manner will be deemed NOT to be confidential by Developer Express Inc. Please refer to the DevExpress.com Website Terms of Use for more information in this regard.

Today, we have a service request that our customer faced the following error message: System.InvalidCastException: Specified cast is not valid reading a Resultset or doing any operation with the database.

Following, I would like to share my lessons learned with this issue.

  • First, I would like to check if this error is raising by SQL Database. For this reason, I wrote down this small TSQL-code:

create procedure MyCodeTest(@state as int) 
as select @state

  • Calling EXEC MyCodeTest 'X' we got the following error: Msg 8114, Level 16, State 1, Procedure MyCodeTest, Line 0 [Batch Start Line 1] Error converting data type varchar to int.
  • Even, running SELECT CAST('X' AS int) or SELECT CONVERT(int,'X'), our customer faced the problem: Msg 245, Level 16, State 1, Line 9 Conversion failed when converting the varchar value 'X' to data type int.

So, all points to, depending on, the value that the ResultSet is reading or the code conversion that is using is not the expecting values. In terms of coding, for example, in SQL Server TSQL we have TRY_PARSE (Transact-SQL) - SQL Server | Microsoft Learn to check the value conversion test before executing the conversion.

When first loading a WPF window (a user form) the VS Designer throws an exception apparently caused by a MultiBinding within the .XAML code. The exception is 'Specified cast is not valid' with the stack trace pointing to the MultiBinding converter in question:

Lỗi specified cast is not valid trong visual studio 2023 năm 2024

When I 'Click here to reload the designer', the WPF window then loads with no error or exception, and the project builds and runs without error.

The binding is for a column in a datagrid:

<DataGridTemplateColumn Header=" Details" Width="200"  CellStyle="{StaticResource NoTabCell}"  >  
     <DataGridTemplateColumn.CellTemplate>  
             <DataTemplate>  
                  <TextBlock Margin="4,0,0,0" Background="Transparent" TextWrapping="Wrap">  
                        <TextBlock.Text>  
                               <MultiBinding Converter="{StaticResource ValueConverterTransactionIsAssetValueChange}">  
                                        <Binding  Path="ToAssetRegister"/>  
                                        <Binding Path="Details"/>  
                                 </MultiBinding>  
                         </TextBlock.Text>  
                      </TextBlock>  
               </DataTemplate>  
       </DataGridTemplateColumn.CellTemplate>  
</DataGridTemplateColumn>  

and the MultiValue converter is:

class ValueConverterTransactionIsAssetValueChange : IMultiValueConverter  
    {  
        public ValueConverterTransactionIsAssetValueChange() { }  
        public object Convert(object[] value, Type targetType, object parameter,  
                  System.Globalization.CultureInfo culture)  
        {  
            if (value[1] != null)  
            {  
                bool? toAssetRegister = (bool?)value[0];  
                string details = (string)value[1];  
                if (toAssetRegister==true)  
                {  
                    details = details.Length > 97? details.Substring(0, 96) + " *": details + " *";  
                    return details;  
                }  
                else { return details; }  
            }  
            else  
            {  
                return "";  
            }  
        }  
        public object[] ConvertBack(object value, Type[] targetType, object parameter,  
          System.Globalization.CultureInfo culture)  
        {  
            throw new NotSupportedException();  
        }  
    }  

The fields passed to the MultiValueConverter are:

  • value[0]: ToAssetRegister, which is a nullable bool (bool?)
  • value1: Details, which is a nullable string (I cannot specify the 'details' string in the multiValueConverter (value1) as nullable as I am running C# version 7.3 , which does not allow 'nullable reference types', hence the need to check for value1!=null).

The exception is only thrown when the WPF window is first loaded into the VS Designer, but disappears when re-loaded and doesn't affect building or running the project. It has also only arisen after upgrading to VS2022 and Windows 11 - there have been no changes to the code. It is simply an annoyance, but I am concerned there might be another underlying issue that I am overlooking.