The framework has recently been updated on my computer and I've updated from Visual Studio 2008 to 2010, now there is a part of my code that won't work.
Public Property ItemCount() As Integer
Get
Dim val As Object = ViewState("ItemCount")
Return If(val IsNot Nothing, CInt(val), 0)
End Get
Set(ByVal value As Integer)
ViewState("ItemCount") = value
End Set
End Property
The "Return If(val IsNot Nothing, CInt(val), 0)" part of the code does not work
ERROR:
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30201: Expression expected.
Source Error:
Line 21: Get
Line 22: Dim val As Object = ViewState("ItemCount")
Line 23: Return If(val IsNot Nothing, CInt(val), 0)
Line 24: End Get
Line 25: Set(ByVal value As Integer
Is there a converter that I can use to bring this part of the code upto date, I'm assuming is the code that's now been outdated.
Thanks.
Get
Dim val As Object = ViewState("ItemCount")
' Return If(val IsNot Nothing, CInt(val), 0)
If val Is Nothing Then
Return 0
Else
Return (CInt(val))
End If
End Get
Try This, it should work.
Related
I am trying to enter data for each row into a SQL Database table but keep getting this error
'Conversion from string "VisitorID" to type 'Integer' is not valid.'
This is the code i have got
Dim url As String
url = bcintegration.GetInfoForIntegration(DropDownList1.SelectedValue).Rows(0)(1)
ServicePointManager.Expect100Continue = True
ServicePointManager.SecurityProtocol = CType(3072, SecurityProtocolType)
Dim json As String = (New WebClient).DownloadString(url)
IntegrationGridView.DataSource = JsonConvert.DeserializeObject(Of DataTable)(json)
IntegrationGridView.DataBind()
For Each row As GridViewRow In IntegrationGridView.Rows
If DropDownList1.SelectedValue = "SignInSystemEntityIntegration" Then
THIS IS THE LINE WITH THE ERROR - bcintegration.SubmitDataForSignInSystemEntity(CInt(row.Cells("VisitorID").Text), Nothing, Nothing, Nothing, Nothing, Nothing, True, False, Nothing, Nothing, Nothing, 1)
Else
End If
Next
The 'Nothing' statement is just for testing - that will have data later on.
Please Help
Thanks
You don't get to just make up methods or properties on the fly. The Item property that you're getting here:
row.Cells("VisitorID")
only accepts an ordinal index, not a name. The documentation, which you should have read, tells you that and Intellisense would have told you that when you wrote the code. The error message is also telling you that. You need to provide a column index, not a column name.
You can convert String to int by doing like this:
1.For C#: int val = Int32.Parse("12");
2.For VB.NET: Dim val as Integer = CInt("12")
Here you can find all Type Conversion Functions (Visual Basic).
https://msdn.microsoft.com/en-us/library/s2dy91zy.aspx
How to Convert a String to a Number (C# Programming Guide).
https://msdn.microsoft.com/en-us/library/bb397679.aspx
Working on an old .net 3.5 vb web application
Getting over 1000 warnings on public property methods such as the following
CA1062 : Microsoft.Design : In externally visible method 'TheClass.TheMethod.Set(String)', validate parameter 'value' before using it.
Original:
Public Property DealerBMRName() As String
Get
Return hdBMRName.Value
End Get
Set(ByVal value As String)
hdBMRName.Value = value.Trim()
End Set
End Property
modified yet still throwing the error:
Public Property DealerBMRName() As String
Get
Return hdBMRName.Value
End Get
Set(ByVal value As String)
If value Is Nothing Then
hdBMRName.Value = ""
Else
hdBMRName.Value = CStr(value)
End If
End Set
End Property
Pretty sure I am following MSDN suggested workaround:
http://msdn.microsoft.com/en-us/library/ms182182(v=vs.100).aspx
Any ideas what I might be missing apart from the fact that the code itself is ugly?
I can't remove the error even with something as basic as:
Set(ByVal value As String)
hdBMRName.Value = "SomeValue"
End Set
Using VS2010 and resharper.
The issue resolved itself when I fixed a bad line entry in my web.config. It had nothing to do with the error but may have been throwing the issue.
PS: I needed to use .Value as the object is an asp:hiddenField.
I am trying to test for an instance of an object, but VB pukes and throws the exception:
Conversion from string "" to type 'Boolean' is not valid.
Here is how I am testing:
Dim objGA As New Gatherer.Gathered("", -1)
objGA = objGatherers(idx)
If Not objGA Is Nothing Then <--exception occurs here
' Do something here
End If
If I do not do this check then I get:
Object reference not set to an instance of an object.
I don't understand the first error given the objGA is an object not a string!
How else should I perform this test? Is there a consistent way to check?
If objGA IsNot Nothing Then
' put some code here...
End If
MSDN: IsNot Operator
try this.
try to check null object using IsDBNull(oValue) Method.
IsDBNull is inbuilt function to check null value.
Dim oValue As Object
Dim DefaultValue As Object
If IsDBNull(oValue) Then
Return DefaultValue
Else
Return oValue
End If
Try this:
Dim objGA As New Gatherer.Gathered("", -1)
Stop 'examine objGA
objGA = objGatherers(idx)
Stop 'examine objGA
At the first stop is objGA a "Gathered object"? When you type the first line does it tell you what the return type is?
I have a VB.Net project in visual studio 2010 intended to let a user upload a video and then stick it in a DB. The file is called "Media/Test.aspx.vb".
When I tried to run the code to debug, tons of errors were generated for a file called App_Web_nkhy2w0y.0.vb
I couldn't locate this file anywhere, and I certainly didn't write it, so I double clicked on the error and it opened a file with about 350 lines of (apparently) auto-generated code, and I have no clue what I did to cause it to do this. Nor do I have any idea how to fix it.
I'll admit I'm new to VB.Net, but I've never seen anything like this online so I'm in the dark.
My original code is below for the code-behind file that handles nothing more than a fileupload control, button, and label in the actual page.
Imports System.IO
Imports System.Data.SqlClient
Imports System.Data
Partial Class Media_Test
Inherits System.Web.UI.UserControl
Protected Sub TryMe_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles TryMe.Click
Dim connection As SqlConnection
'check the file
Dim buffer As Byte()
If testFile.HasFile AndAlso testFile.PostedFile IsNot Nothing AndAlso testFile.PostedFile.FileName <> "" Then
Dim file As HttpPostedFile = testFile.PostedFile
'retrieve the HttpPostedFile object
buffer = New Byte(file.ContentLength - 1) {}
Dim bytesReaded As Integer = file.InputStream.Read(buffer, 0, testFile.PostedFile.ContentLength)
'the HttpPostedFile has InputStream porperty (using System.IO;)
'which can read the stream to the buffer object,
'the first parameter is the array of bytes to store in,
'the second parameter is the zero index (of specific byte)
'where to start storing in the buffer,
'the third parameter is the number of bytes
'you want to read (do u care about this?)
If bytesReaded > 0 Then
Try
Dim connectionString As String = ConfigurationManager.ConnectionStrings("ConnectionString").ConnectionString
connection = New SqlConnection(connectionString)
Dim cmd As New SqlCommand("INSERT INTO Videos (Video, Video_Name, Video_Size)" & " VALUES (#video, #videoName, #videoSize)", connection)
cmd.Parameters.Add("#video", SqlDbType.VarBinary, buffer.Length).Value = buffer
cmd.Parameters.Add("#videoName", SqlDbType.VarChar).Value = testFile.FileName
cmd.Parameters.Add("#videoSize", SqlDbType.BigInt).Value = file.ContentLength
Using connection
connection.Open()
Dim i As Integer = cmd.ExecuteNonQuery()
Label1.Text = "uploaded, " & i.ToString() & " rows affected"
End Using
Catch ex As Exception
Label1.Text = ex.Message.ToString()
End Try
End If
Else
Label1.Text = "Choose a valid video file"
End If
End Sub
End Class
And the top of the generated page is:
#ExternalChecksum("C:\Users\handcm\Documents\Visual Studio 2010\WebSites\APPS\Media\Test.aspx","{406ea660-64cf-4c82-b6f0-42d48172a799}","F31F20662F14B4894B6FBF58215628CB")
'------------------------------------------------------------------------------
' <auto-generated>
' This code was generated by a tool.
' Runtime Version:4.0.30319.296
'
' Changes to this file may cause incorrect behavior and will be lost if
' the code is regenerated.
' </auto-generated>
'------------------------------------------------------------------------------
I can post the entire file if need be, but could someone shine some light on what's going on here? I have no clue how to debug a file I didn't write or have never seen.
Edit 2: I checked the "'InitializeCulture' is not a member of 'ASP.media_test_aspx" error and it usually is a discrepancy between the class being inherited on the aspx page not being the correct as in the aspx.vb file. However, in this case, there isn't a discrepancy as it's the correct file.
Edit: Some of the error messages I am getting:
Error 4 'GetWrappedFileDependencies' is not a member of 'ASP.media_test_aspx'. C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\apps\f1b28c0f\bab8f810\App_Web_nkhy2w0y.0.vb 103
Error 2 Class 'media_test_aspx' must implement 'ReadOnly Property IsReusable As Boolean' for interface 'System.Web.IHttpHandler'. Implementing property must have matching 'ReadOnly' or 'WriteOnly' specifiers. C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\apps\f1b28c0f\bab8f810\App_Web_nkhy2w0y.0.vb 84
Error 10 'ProcessRequest' is not a member of 'Media_Test'. C:\Windows\Microsoft.NET\Framework\v4.0.30319\Temporary ASP.NET Files\apps\f1b28c0f\bab8f810\App_Web_nkhy2w0y.0.vb 342
Error 1 'InitializeCulture' is not a member of 'ASP.media_test_aspx'. C:\Users\handcm\Documents\Visual Studio 2010\WebSites\APPS\Media\Test.aspx 1
I don't even know what the last one is referring to, as there is definitely not an "Initialize Culture" on the page.
I am having an VB Script. I need to log the error information in a file. I need to log every information like error number error description and in which sub routine does the error occured.
Please provide some code
You can make use of the FileSystemObject and the Error object if you're using VBScript.
Paste the following into error.vbs and run it. It will throw an error then log the details to a file called c:\errors.log
Option Explicit
On Error Resume Next ' Potential error coming up
Dim MyArray(5)
MyArray(7) = "BWA HA HA"
If Err.Number <> 0 Then
LogError(Err)
Err.Clear
End If
On Error Goto 0 ' Stop looking for errors
Sub LogError(Details)
Dim fs : Set fs = CreateObject("Scripting.FileSystemObject")
Dim logFile : Set logFile = fs.OpenTextFile("c:\errors.log", 8, True)
logFile.WriteLine(Now() & ": Error: " & Details.Number & " Details: " & Details.Description)
End Sub
If you're using an ASP page then you can make use of ASPError to get more detailed information about the error such as line number, etc (remember to replace CreateObject with Server.CreateObject).
Edit:
To get the line number that caused the error in .vbs script you could add this as a parameter to the sub routine.
VBScript doesn't support on error goto label. The following piece of code will never work -:
On Error GoTo HandleError '' on error will code jump to specified signal
Dim aa = 15 / 0
GoTo Finish '' skips error
handlingHandleError:
Dim msgSet
msg = Err.Description & vbCrLf & Err.Number
MsgBox msgFinish:'' there is end of sciprt
Put your entire sub or function inside a do loop (or other loop). Put your error handling at the outside of the do loop
private sub BucketList()
do while 1=1
ClimbMountain(top)
if err.Number <> 0 then exit do
SwimOcean(deep)
if err.Number <> 0 then exit do
GiveErrorHandlingToVBS(isNeverGoingToHappen)
if err.Number <> 0 then exit do
exit do
loop
'Error Handler
if err.Number <> 0 then
'handle error
end if
end sub
For error handling in VBScript is used "On Error" clausule.
There are 3 ways, how to handle errors:
On Error Resume Next '' ignore errors
On Error GoTo 0 '' removes error handlning
On Error GoTo HandleError '' on error will code jump to specified signal
Samples:
On Error Resume Next '' ignore errors
SomeIgnorableFunction()
On Error GoTo 0 '' removes error ignoring
SomeImportantFunction()
On Error GoTo HandleError '' on error will code jump to specified signal
Dim a
a = 15 / 0
GoTo Finish '' skips error handling
HandleError:
Dim msg
Set msg = Err.Description & vbCrLf & Err.Number
MsgBox msg
Finish:
'' there is end of sciprt