|
Hey, I'm a very beginner so please don't laugh
I want to validate a date user inputs:
Dim scr_time As Date
dim ok as boolean
System.Console.WriteLine("Enter screening date & time [DD/MM/YYYY HH/MM]")
scr_time = System.Console.ReadLine()
ValidateDate(scr_time, ok)
Sub ValidateDate(ByVal scr_time As String, ByRef bol_isvalid As Boolean)
If IsDate(scr_time) Then
bol_isvalid = True
Else
bol_isvalid = False
End If
End Sub
When entered 'notdate' the error says "Conversion from string "notdate" to type 'Date' is not valid."
Is that because I declared scr_time as date, instead of string? If I declare the date as string is
now >= scr_date
going to work fine?
Second question regarding validation of number:
Dim cnt_adult As Integer
System.Console.WriteLine("How many adult tickets required?")
cnt_adult = System.Console.ReadLine()
ValidateNumber(cnt_adult, ok)
If ok Then
System.Console.WriteLine()
Else
System.Console.WriteLine("Incorrect value. Press any key to try again.")
System.Console.ReadKey()
GoTo sale
End If
Sub ValidateNumber(ByVal text As String, ByRef bol_isvalid As Boolean)
If IsNumeric(text) Then
bol_isvalid = True
Else
bol_isvalid = False
End If
End Sub
"Conversion from string "t" to type 'Integer' is not valid."
I know the solution would be to declare the variable cnt_adult as string, by I need it as integer so I can multiply it with another integer later.
modified on Wednesday, October 7, 2009 3:41 PM
|
|
|
|
|
Use the TryCatch structured error handling, it will help you wonders, or you can also use Int32.TryParse[^] to convert a string to a numeric.
|
|
|
|
|
I don't want to convert entered by user letters to equivalent numbers, I just want program to make sure the user inputs only numbers where asked, if he enters anything else I will ask him to enter it untill its numbers only [loop].
|
|
|
|
|
If you read the documentation for ReadLine you will see that the very first line states:
Reads the next line of characters from the standard input stream.
**NOTE** it says characters. Not numbers. Not hieroglyphics. Characters.
Therefore you will have to use int.TryParse and DateTime.TryParse (the last of these will fail incidentally given your code. Use HH:MM not HH/MM) to get the data into the form that you want.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
Actually characters include numbers.
|
|
|
|
|
Well since you obviously already know everything, why did you ask for help?
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
I don't know everything, furthermore I know much less than you regarding this topic, however I'm certain that the numbers are characters and ReadLine reads them.
dim int_number as integer
System.Console.WriteLine("Enter number")
int_number = System.Console.ReadLine()
System.Console.WriteLine(int_number)
Examples of characters include letters, numerical digits, and common punctuation marks (such as '.' or '-'). from wikipedia.org
btw. I respect your knowledge [as seen on your profile] but what you said was mistake, wasn't it?
Or maybe I'm wrong, or didn't understand you quiet well, then explain me please.
Thank you for your time.
added:
oh, I understand now what's the misunderstanding. "characters" can refer to a letters only or a group of signs incl. letters, numbers, punctuation marks. So the meaning of a character in this quote "Reads the next line of characters from the standard input stream." is general [character may be a letter or a number ect]
modified on Wednesday, October 7, 2009 2:12 PM
|
|
|
|
|
Yup your 'added:' is correct.
Readline() reads a series of characters and returns them as a String . Your code works because, behind the scenes, the VB compiler 'sees' that you want an int and does the casting for you. Just to confirm this for your own satisfaction, run the code and enter '22f', after a bit you should get an exception message saying 'The cast from string to int was unsuccessful.', or similar. Now you haven't done any casting, so it must have been done for you. (FYI: your code won't even compile in C#. Another reason not to do it this way, just in case you have to change language for some reason. )
In C# your code is almost identical:
int int_number = 0;
System.Console.WriteLine("Enter number");
int_number = System.Console.ReadLine();
System.Console.WriteLine(int_number);
Anyway, if you always treat the value from a ReadLine() as a string, you won't get any run-time errors, regardless of what the user enters. You can then attempt the conversion yourself, in a way that you can control. Using the TryParse() methods of the various Types is about the most easily controlled way of doing it.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
|
Declare cnt_adult as string , and then do
ok = Integer.TryParse(cnt_adult)
|
|
|
|
|
Use a temporary string to read the input.
Dim userInput as String
Dim isValid as Boolean
Dim cnt_adult as Integer
userInput = System.Console.ReadLine()
isValid = Integer.TryParse(userInput, cnt_adult)
|
|
|
|
|
I see that others have already replied but here's my effort anyway:
Dim cnt_adult As Integer
Dim userInput As String
Dim ok As Boolean
ok = False
While (ok = False)
System.Console.WriteLine("How many adult tickets required?")
userInput = System.Console.ReadLine()
ok = Integer.TryParse(userInput, cnt_adult)
End While
Sorry for not replying earlier, but I was in the middle of a very, very important game.
Henry Minute
Do not read medical books! You could die of a misprint. - Mark Twain
Girl: (staring) "Why do you need an icy cucumber?"
“I want to report a fraud. The government is lying to us all.”
|
|
|
|
|
When I try to make a connection to the db from VB I get this error message.
"A network-related or instance-specific error occurred while establishing a connection to SQL Server. The server was not found or was not accessible. Verify that the instance name is correct and that SQL Server is configured to allow remote connections. (provider: SQL Network Interfaces, error: 26 - Error Locating Server/Instance Specified)"
I'm am using the default named instance.
The connection works just fine with the SQL Server Management Studio and the configuration manager. I've done two days of searching and trying various items that I've found but nothing seems to work.
Any help would be most appreciated.
|
|
|
|
|
Any chance we can see your connection string?
|
|
|
|
|
Thanks Elliot,
This is the connection string from the machine.config file in the C:\WINDOWS\Microsoft.net\framework\v2.0.50727\CONFIG file.
<add name="LocalSqlServer" connectionstring="data source=.\SQLEXPRESS;Integrated Security=SSPI;AttachDBFilename=|DataDirectory|GGTEST.mdf;User Instance=true" providername="System.Data.SqlClient">;
Garth
|
|
|
|
|
|
The link you sent wrote: Server Error in '/' Application.
The resource cannot be found.
Description: HTTP 404. The resource you are looking for (or one of its dependencies) could have been removed, had its name changed, or is temporarily unavailable. Please review the following URL and make sure that it is spelled correctly.
Requested URL: /article.aspx
Version Information: Microsoft .NET Framework Version:2.0.50727.3074; ASP.NET Version:2.0.50727.3074
|
|
|
|
|
Eliott,
Don't know why my posts are changing what I write.
Will you share your email address? I'll send it to you that way.
Thanks
Garth
|
|
|
|
|
Eliott,
I have a feeling that I'm looking for the connection string in the wrong place. Where would I find it? This is VB2008 Express - a windows desktop application (not web page).
Thanks
Garth
|
|
|
|
|
Garth Geoghan wrote: Verify that the instance name is correct and that SQL Server is configured to allow remote connections.
Is the server running on a seperate machine?? By default, SQL Server does not allow connections from other machines. You have to open up the Configuration Manager and enable this yourself.
|
|
|
|
|
Thanks Dave,
No, the server is on the same machine.
Garth
|
|
|
|
|
In that case, we'd need the connection string. Your other post was apparently full of XML tags, so the string never showed. Post just the connection string, not the enclosing XML tags.
|
|
|
|
|
Thanks Dave,
Please tell me where I would find the connection string that you need? Machine.config? Which one?
Garth
|
|
|
|
|
This is the connection string from the machine.config file in the C:\WINDOWS\Microsoft.net\framework\v2.0.50727\CONFIG file.
connectionstring = "data source=.\SQLEXPRESS; Integrated Security=SSPI;AttachDBFilename=|DataDirectory|GGTEST.mdf;User Instance=true" providerName="System.Data.SqlClient"
|
|
|
|
|
The connection string you posted is for an Express Edition of SQL Server, not for the Developer or normal editions.
First, is the GGTEST.mdf file in the same directory as the .EXE you launched? While debugging, this would be in the Debug or Release folders under the bin folder in your project folder.
|
|
|
|