473,396 Members | 2,020 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

converting C# to VB problem

Hello, I have this code in C# and it works perfect:
Expand|Select|Wrap|Line Numbers
  1. protected string GetUniqueName(string filename) {
  2. string temp = filename;
  3.  
  4. for (int x=1; System.IO.File.Exists(temp); x++){
  5. temp = filename.Substring(0, filename.LastIndexOf(".")) + x + filename.Substring(filename.LastIndexOf("."));
  6. }
  7. return new System.IO.FileInfo(temp).Name;
  8. }
  9.  
Then I converted it to VB:
Expand|Select|Wrap|Line Numbers
  1. Protected Function GetUniqueName(ByVal filename As String) As String
  2. Dim temp As String = filename
  3. Dim x As Integer = 1
  4. While System.IO.File.Exists(temp)
  5. temp = filename.Substring(0, filename.LastIndexOf(".")) + x + filename.Substring(filename.LastIndexOf("."))
  6. System.Math.Min(System.Threading.Interlocked.Incre ment(x), x - 1)
  7. End While
  8. Return (New System.IO.FileInfo(temp)).Name
  9. End Function
  10.  
Now I am getting an error:

Input string was not in a correct format.

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.FormatException: Input string was not in a correct format.

Source Error:

Line 303: Dim x As Integer = 1
Line 304: While System.IO.File.Exists(temp)
Line 305: temp = filename.Substring(0, filename.LastIndexOf(".")) + x + filename.Substring(filename.LastIndexOf("."))
Line 306: System.Math.Min(System.Threading.Interlocked.Incre ment(x), x - 1)
Line 307: End While

Source File: D:\wwwroot\www.yourkinetico.com\wwwroot\AdPlanner\ App_Code\Utils\Upload.vb Line: 305

Stack Trace:

[FormatException: Input string was not in a correct format.]
Microsoft.VisualBasic.CompilerServices.Conversions .ParseDouble(String Value, NumberFormatInfo NumberFormat) +211
Microsoft.VisualBasic.CompilerServices.Conversions .ToDouble(String Value, NumberFormatInfo NumberFormat) +74


Any help would be greatly appreciated!
May 23 '07 #1
1 1194
Plater
7,872 Expert 4TB
If someone sends in a file without a . in it, your program will fail.

The System.IO.File and System.IO.FileInfo classes exist inside VB, why mess with success?

TADA: (you needed a .ToString() on that x)
Expand|Select|Wrap|Line Numbers
  1. Protected Function GetUniqueName(ByVal filename As String) As String
  2.    Dim temp As String = filename
  3.    Dim x As Integer = 1
  4.    While System.IO.File.Exists(temp)
  5.       temp = filename.Substring(0, filename.LastIndexOf(".")) + x.ToString() + filename.Substring(filename.LastIndexOf("."))
  6.       x = x + 1
  7.    End While
  8.    Return (New System.IO.FileInfo(temp)).Name
  9. End Function
  10.  
May 23 '07 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: mustafa | last post by:
Dear sir , I have built my application in visual basic 6.0 and crystal Report8.5 , Now i migrated my application to VB.net using the upgrade wizard.My visual basic form is upgraded to vb.net...
8
by: prabha | last post by:
Hello Everybody, I have to conert the word doc to multiple html files,according to the templates in the word doc. I had converted the word to xml.Also through Exsl ,had finished the multiple...
3
by: Stephan Brunner | last post by:
Hi I have created two flavors of an XSLT stylesheet to transform all attributes of an XML document to elements: They both work as expected with MSXML and XMLSPY but throw an exception ...
5
by: Robert | last post by:
I have a series of web applications (configured as separate applications) on a server. There is a main application at the root and then several virtual directories that are independant...
12
by: Frederik Vanderhaeghe | last post by:
Hi, I have a problem converting text to a double. Why doesn't the code work: If Not (txtdocbedrag.Text = "") Then Select Case ddlBedrag.SelectedIndex Case 0 Case 1
7
by: Tor Aadnevik | last post by:
Hi, I have a problem converting values from Single to double. eg. When the Single value 12.19 is converted to double, the result is 12.1899995803833. Anyone know how to avoid this? Regards...
4
by: gg9h0st | last post by:
i'm a newbie studying php. i was into array part on tutorial and it says i'll get an array having keys that from member variable's name by converting an object to array. i guessed "i can...
7
by: Coleen | last post by:
Does anyone have any good detailed information on the conversion process? We are in the process of converting 2 projects from 2003 to 2005 and have some conversion errors that I can not find...
2
by: shenanwei | last post by:
DB2 V8.2 on AIX, type II index is created. I see this from deadlock event monitor. 5) Deadlocked Connection ... Participant no.: 2 Lock wait start time: 09/18/2006 23:04:09.911774 .........
2
by: TheLongshot | last post by:
Ok, let's try this again. I have an ASP.NET 1.1 application that I'm working to convert to 2.0, but I've run into a snag. The problem is with this line: return...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.