DBNull error - 'System.InvalidCastException' - vb.net

Using Visual Studio 2008 and generating .net 2.0 framework with VB.net for a web application.
I encountered a very strange problem currently. After I build a solution and click a link in a web page, I got an error message as following. The same thing happened when I tried to run in debug mode.
*************************************** ERROR Message ******************************************************
"An exception of type 'System.InvalidCastException' occurred in Microsoft.VisualBasic.dll but was not handled in user code"
"Additional information: Operator '=' is not defined for type 'DBNull' and string "". "
*******************************************************************************************************************
The problem part is simply as below in a vb file. The value of dataview(0)(“name”) is NULL and my_name is a variable string. The strange is thing I didn't change any code in this vb file. This vb file was built previously and used for a long time ago without any problem. It just suddenly happened today. I have this syntax all over the program without causing any problem. I have tried to restore the whole solution back to original but still have the problem.
Can anyone please advise why all the sudden and how to fix?
If dataview(0)(“name”) = my_name then …

Try to determine NULL value first ..
If NOT IsDBNull(dataview(0)(“name”)) Then
If dataview(0)(“name”) = my_name then
'codes here
End If
End If

Related

An unhandled exception occurred while processing the request SqlException

When I create a new project (web api) in Visual Studio 2019, I try running the project but I got the following error:
An unhandled exception occurred while processing the request.
SqlException: Invalid object name 'Books'.
Microsoft.Data.SqlClient.SqlCommand+<>c.b__169_0(Task result)
Did you check spelling in SqlDataReader? For me, I was misspelled. If the spelling is correct, any chance are you re-using the same name of an existing environmental variable for a different database on the same SQL Server? If the answer is yes it can be issue too.

Why is my Deployment script causing me errors?

I am working on a project in visual studio 2012. Recently, I added a database project to the solution. The database already existed before I added it to the solution and everything worked fine.
Now, however, when I try to run the application I get errors. The errors are being caused by a computer-generated file called [database name].sql. At the top of the tile, it reads:
/*
Deployment script for [the database name here]
This code was generated by a tool.
Changes to this file may cause incorrect behavior and will be lost if
the code is regenerated.
*/
This file gets re-created every time the application runs. The errors that occur appear to be syntax errors. I cannot fix them because any changes I make to the file are irrelevant because a new file gets generated with each run and the errors re-appear.
I tried looking into this more online but had trouble. This is all rather new to me.
Here are some of the errors being created:
GO
:setvar DatabaseName "(the database name is here)"
which gives me three errors that read:
Error 88 SQL80001: Incorrect syntax near ':'.
Error 89 SQL80001: 'DatabaseName' is not a recognized option.
Error 90 SQL80001: Incorrect syntax near '"(the database name is here in the code)"'.
Also, there is a line of code that reads:
CREATE USER [(the domain)\(the username)] FOR LOGIN [(the domain)\(the username)];
GO
which gives the following error:
Error 119 SQL72014: .Net SqlClient Data Provider: Msg 15401, Level 16, State 1, Line 1 Windows NT user or group '(the domain)\(the username)' not found. Check the name again.
From the errors you have posted it looks like there are two issues:
Windows NT user or group '(the domain)\(the username)' not found. - The user being used to access the database doesn't exist. As it's a Windows user I'm guessing that it uses the current user's credentials.
Make sure that your instance of SQL can accept Windows logins and that you (and anyone else building the software) has the necessary access rights.
'DatabaseName' is not a recognized option. - This is more than likely also caused by the first issue, but double check that the database exists.
The error message caught me out recently as it did not relate to the cause of my problem at all. It turns out my SQL script was incorrectly written (for my case - I forgot to add IDENTITY (1,1) to my PK column) to begin with.
Moral of the story for me was to test out the SQL data file in SSMS first.

Value of type 'Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer' cannot be converted to 'System.Type

I'm working on a custom TFS Build workflow based on the code found in two blogs:
http://www.theringworx.com/blog/?p=494
https://lajak.wordpress.com/2011/08/30/tfs2010-retrieve-associated-workitems-to-changesets-tfs-api/
When I implement the VB.Net code within my XAML workflow, I'm currently getting the following errors on build runtime:
The build process failed validation. Details:
Validation Error: The private implementation of activity '1:
DynamicActivity' has the following validation error: Compiler
error(s) encountered processing expression "TFSService".
Value of type
'Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer'
cannot be converted to 'System.Type'.
Validation Error: The private implementation of activity '1:
DynamicActivity' has the following validation error: Compiler
error(s) encountered processing expression "TFS.GetService(Of
VersionControlServer)();".
End of expression expected.
I've traced the error down to this section of code (Simplified from XAML):
TFS=Microsoft.TeamFoundation.Client.TfsTeamProjectCollectionFactory.GetTeamProjectCollectionFactory(New Uri("MyTeamsTFSServer"))
TFSService = TFS.GetService(Of VersionControlServer)()
I've tried changing the type of TFSService from both Microsoft.TeamFoundation.VersionControl.Client.VersionControlServer and System.Type without success.
From what I can tell the error is occurring in the "Of VersionControlServer".
Has anyone ever encountered this? And if so how did you resolve it?
Turns out the issue was the URL I was using, I made a typo when I was entering it in. Yay for descriptive error messages...
I found this while I was checking my authentication using the two methods:
TFS.HasAuthenticated
TFS.Authenticate();
My code blew out when I was running the Authenticate() method.

VB.Net "COMException was unhandled" Error

I have a VB.Net program written with .Net 2.0
I'm trying to write a code that logs entire registry entries into a log file.
I have got it to work wit the following code.
http://pastebin.com/kmA63cUe
However whenever I try to add an additional Registry key to log
LogPrint4("<-----------------HKLM/WebCheck-------------------->")
If reg.enumvalues(&H8000002, "Software\Microsoft\Windows\CurrentVersion\WebCheck", keys2, types) = 0 Then
If IsArray(keys2) Then
For p = 0 To UBound(keys2)
reg.getstringvalue(&H8000002, "Software\Microsoft\Windows\CurrentVersion\WebCheck", keys2(p), value)
LogPrint4(keys2(p) & "=" & value)
Next
End If
End If
It comes up with the following error
COMException was unhandled Check the ErrorCode Property of the
exception to determine the HRESULT returned by the COM object.
I'm not sure why it is limiting it only to 5 checks.
Anyone have any ideas?
Consider using the built-in Registry support instead of the COM libraries. At a minimum, you'll get clearer error messages. It is supported in .NET 2.0.
http://msdn.microsoft.com/en-us/library/microsoft.win32.registry(v=vs.80).aspx
Good luck!

Vb2012 admin permission

I have coded up a cleaner type of program but getting a huge error with permissions ( I think )
The error message is printed out like so.
An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll
Additional information: Access to the path 'C:\Windows\CSC\v2.0.6' is denied.
And it says that the error part of the code is this line
For Each fi In DirectroyInfos.GetFiles(filter)
But here is the fill block of code.
For Each fi In DirectroyInfos.GetFiles(filter)
Try
file_count = file_count + 1
file_size = CULng(file_size + fi.Length)
FilesToDelete.Add(fi.FullName)
Catch ex As UnauthorizedAccessException
'There's really no pretty way to handle this exception
Catch ex As FileNotFoundException
'There's really no pretty way to handle this exception
End Try
I think it's some sort of permission problem I have windows 7 and have noticed there are a lot of run has admin problems.. I think its trying to remove or gain access to a file which it does not have permission to get .
Is there anyway to fix this? Am I missing something in my coding?
As you said in your question, you don't have write access that file.
Either skip files you don't have access to, or run the application as admin.
There appears to be a limitiation with the win32 api that will skip all files in the folder if you do not have access to one of them.
Check out this solution from Microsoft Connect:
How to: Iterate Through a Directory Tree (C# Programming Guide)