User Name Password Register
DaniWeb IT Discussion Community
All
What is DaniWeb IT Discussion Community?
You're currently browsing the ASP.NET section within the Web Development category of DaniWeb, a massive community of 455,964 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 3,630 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our ASP.NET advertiser: Lunarpages ASP Web Hosting
Views: 4059 | Replies: 2
Reply
Join Date: Nov 2007
Posts: 1
Reputation: annie212 is an unknown quantity at this point 
Rep Power: 0
Solved Threads: 0
annie212 annie212 is offline Offline
Newbie Poster

Question Cast from string "" to type 'Integer' is not valid.

  #1  
Nov 19th, 2007
Below is my code and I get the above mentioned error in vb.net. I am trying to pass blank in integer .

Private ReadOnly Property GPLegalEntityId() As Integer
Get
If Request.QueryString("GPLegalEntityId") = "" Then
Return ""
Else
Return Convert.ToInt32(Request.QueryString("GPLegalEntityId"))
End If

End Get
End Property
AddThis Social Bookmark Button
Reply With Quote  
Join Date: Sep 2007
Posts: 1,058
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 61
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Cast from string "" to type 'Integer' is not valid.

  #2  
Nov 22nd, 2007
You cannot convert an empty string to an integer, that's why you are receiving the error. A querystring is treated as a string. If the length of the querystring is above 0 numbers, and contains no letters, this code will work. You will receive the same error, however, if your querystring is 1231k213, all due to the K. You have to remove the letters and characters from your string, and then convert it. To do this, use something like the function below to make it an integer. Also, make sure that the numbers do not exceed the integer length, otherwise you will need to convert it into a LONG instead of INTEGER.

You may also use string instead of integer to quickily put a fix to your problem, but you will have this problem again if there are more than integers in your querystring.
'Make Querystrings Ints
Public Function MakeInt(ByVal stringint As String) As String
Dim lngCount As Long
Dim strOut As String
if Len(stringint) > 0 then
  for lngCount = 1 to len(stringint)
    if isnumeric(mid$(stringint, lngCount, 1)) then
      strOut = strOut & mid$(stringint, lngCount, 1)
    end if
  next lngCount
end if
MakeInt = strOut
end function
Reply With Quote  
Join Date: Sep 2007
Posts: 1,058
Reputation: SheSaidImaPregy is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 61
SheSaidImaPregy SheSaidImaPregy is offline Offline
Veteran Poster

Re: Cast from string "" to type 'Integer' is not valid.

  #3  
Nov 22nd, 2007
Also, always trim you querystring to remove those spaces "(Trim(Request.QueryString(""))"
Reply With Quote  
Reply

Only community members can participate in forum threads. You must register or log in to contribute.

DaniWeb ASP.NET Marketplace
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)

 

Thread Tools Display Modes

Similar Threads
Other Threads in the ASP.NET Forum

All times are GMT -4. The time now is 9:02 am.
Forum system based on vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC