Lỗi specified cast is not valid trong c năm 2024

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.InvalidCastException: Specified cast is not valid.

Cause

This problem might occur on postback because the control tree hierarchy is re-ordered. On postback, the view state for controls is loaded based on the positions of the controls [indexes] in the control tree. The hierarchy that is created on postback must match the hierarchy that is used initially when the view state is saved.

Resolution

Make sure that the control tree is re-created on postback in the same order that it was saved at the end of the previous request.

Status

This behavior is by design.

Need more help?

Want more options?

Explore subscription benefits, browse training courses, learn how to secure your device, and more.

Communities help you ask and answer questions, give feedback, and hear from experts with rich knowledge.

A problem in early versions of Visual Studio 2017, i.e. 15.1 and 15.2, sometimes causes errant "Specified cast is not valid" compilation errors when Visual Assist is installed. The problem does not occur with 15.3 and newer. If the problem resurfaces and you are directed by the support team at Whole Tomato Software, work around the compilation errors by preventing Visual Assist from being notified of changes to the open solution. If directed, disable notifications by modifying the registry:

HKCU\Software\Whole Tomato\Visual Assist X\VANet15\EnableVcProjectSync = 0

Restore value to 01 to restore default behavior.

Visual Studio 2017 15.2 and older, Visual Assist build 2223 and older

A problem in Visual Studio 2017 sometimes causes errant "Specified cast is not valid" compilation errors when Visual Assist is installed. Visual Assist can work around the problem, but several features in Visual Assist are adversely affected in the process.

The ability to work around the problem varies with the build of Visual Assist.

If the workaround is enabled in your build, Visual Studio 2017 no longer notifies Visual Assist of changes to the open solution. In turn, several features in Visual Assist are affected because Visual Assist is not notified of the addition to, or removal of, files in your solution or of changes to include directories in your solution. The affected features include Open Find in Solution [Shift+Alt+O],

include completion, Find References, Change Signature, Rename, Add Include, and include-file resolution. In order to use an affected feature on a file[s] that has been added to, and not on a file[s] that has been removed from, your solution since it was last opened, you must reopen your solution so Visual Assist can ascertain the proper state of the solution. Also, in order for Visual Assist to recognize changes to the include paths in your solution since the solution was last opened, you must reopen your solution.

Visual Assist build 2223

The workaround to avoid errant "Specified cast is not valid" compilation errors is enabled by default. If you wish to disable the workaround and risk the chance of the compilation errors, modify the registry:

HKCU\Software\Whole Tomato\Visual Assist X\VANet15\ForceVs2017ProjectSync = 01

Restore value to 00 to avoid the compilation errors.

Visual Assist builds 2208-2217

The workaround to avoid "Specified cast is not valid" compilation errors is always enabled.

Visual Assist build 2219, and builds 2210 and older

The workaround is not available. If you get errant "Specified cast is not valid" compilation errors, update to a newer build of Visual Assist.

As I always say, Errors are a great source of learning and I am fortunate to have you as my readers. Because one of my blog reader reported an interesting issue which is worth blogging. This is his initial email:

Hi Pinal, Your blogs have been very helpful to me to find solution of almost any SQL problem so far. This time, I am writing directly to see if you have time and provide some help to me. I have a backup from database in SQL Server 2008 R2. When I want to restore this backup to SQL Server, I get this error: “Error: Specified cast is not valid. [SqlManagerUI]” How to I resolve this error? Thanks!

We then exchanged various mails with screenshots, query, output and with his patience, we were able to find the cause of the issue. Here we go to what happened behind the scenes:

Here were the steps to restore the database:

As soon as OK was clicked, this was the error.

If we click on red cross icon at left bottom, we would see below

Here is the partial text of the message.

Specified cast is not valid. [SqlManagerUI]

Program Location:    at Microsoft.SqlServer.Management.SqlManagerUI.SqlRestoreDatabaseGeneral.PopulateGridWithBackupSetsFromDevices[]    at Microsoft.SqlServer.Management.SqlManagerUI.SqlRestoreDatabaseGeneral.GetBackupSetsFromDevices[]    at Microsoft.SqlServer.Management.SqlManagerUI.SqlRestoreDatabaseGeneral.textDeviceSelected_TextChanged[Object sender, EventArgs e]

As we can see above that SSMS is trying to populate the grid with the details about the backup. When I put profiler on my machine, it was running Restore Headeronly command on the selected file. So, I asked to run the command manually. You can refer my earlier blog for this SQL SERVER – Restore Database Backup using SQL Script [T-SQL]

When we ran the command, we found below

Above could be because of two reasons [which I found]

  1. Backup taken on SQL 2012 and Restore Headeronly was done in SQL 2008 R2
  2. Backup media is corrupted.

Solution of 1st one is easy – restore it on same or higher version. 2nd one is a difficult situation because you need to look for another good/restorable backup. In my case, it was first situation so we were good.

Another possible reason of “Specified cast is not valid” would be password protected backups. In such backups, headeronly would return “*** PASSWORD PROTECTED ***” in the first column as shown below.

So, next time you ever see any UI error, find the T-SQL statement by using profiler and run that directly to see “real” error message.

Have you ever come across situation where UI was giving some misleading error? Please comment and share your knowledge.

Chủ Đề