I have function that returns string and that works fine but when I call that function from another procedure when it executes function I get a message:
,,Conversion from 'result' to type 'Integer' is not valid,,
ASP debugger says:
,,Input string was not in a correct format. ,,
I tried to use Convert.ToString(), ToInt32() and other but I get the same error.

Hi,

Usually these kind of errors appear when a non numeric(alpha numeic/string) value is casted to numeric.

for example,

int i = Convert.ToInt32("abc");
int j = Convert.ToInt32("abc12");

The above conversion will throw the error. Can you check the values that are being converted in the procedure and let us know the if help is needed.

Thank you.

I guess you were trying to cast a TextBox.Text value. Maybe it was a string in it.

Well, use RegularExpressions to find out if the string contains any charachters.

if(StringContainsCharachters(StringValue))
    {
        throw new Exception("Chars in the string");
    }
    else
    {
        // Cast
        int CastedValue = Convert.ToInt32(StringValue);
    }

    protected bool StringContainsCharachters(string StringValue)
    {
        // Use regex here
        // with this method
        // System.Text.RegularExpressions.Regex.IsMatch(inputString, patternToCheck)
    }

That cannot help me, my string must be alpha-numeric. That string is username and it is in next pattern: ,,firstName.middleName.lastName,, . It worked until I set a collation LATIN in MS SQL database. I need to solve this as quickier as possible.

So why are you trying to convert it to Integer?

I am not trying, ASP.NET VB is trying. That is my code:

' getUsername is function that works fine
Dim result As String = getUsername()

Please put here the code of the getUsername method.

Here's the code:

Protected Function getUsername() As String
        Dim connection As New SqlConnection(baza)
        Dim command As New SqlCommand("SELECT * FROM students WHERE name = '" & listaUcenika.Text & "' COLLATE Latin1_General_CI_AI", connection)
        Dim reader As SqlDataReader

        getUsername = ""

        Try
            connection.Open()
            reader = command.ExecuteReader(Data.CommandBehavior.Default)

            While reader.Read()
                Return reader("username")
            End While

            connection.Close()
        Catch ex As Exception
            txtError.Text = ex.Message
        End Try
    End Function

All this code is incorrect I'm afraid...
First of all you returned a value inside the loop, that means you exit the function...
Second, it's not the name of the function getUsername () O.O
Third, I have no Idea what is the citac variable...

Try to return a static String from the function (Return "Hello") and see if the problem still exists.

Sorry, my code is on Serbian language, and I hurried to post answer before you go. Sorry. ,,Citac,, means reader, and korisnickoImeUcenika means getUsername.

I don't see the citac variable is declared...

Did you try my advice? (Return a static String value)

I solved this problem and tried your advice. Problem was in the calling procedure. I entered some arguments and VB understood it as ,,index As Integer,, . My code now works. Thanks a lot for help. I won't use ,,Return ,, statement anymore. I used before function = value but I forgot sometimes.

P.S.
I didn't translate ,,citac,, variable from Serbian to English.

ok

All this code is incorrect I'm afraid...
First of all you returned a value inside the loop, that means you exit the function...
Second, it's not the name of the function getUsername () O.O
Third, I have no Idea what is the citac variable...

Try to return a static String from the function (Return "Hello") and see if the problem still exists.

ok

So why are you trying to convert it to Integer?

okkkkkkkk

Sorry, my code is on Serbian language, and I hurried to post answer before you go. Sorry. ,,Citac,, means reader, and korisnickoImeUcenika means getUsername.

okkkkkkkkkkkkkkkkkkkkk

ok

Hi

There are many ways you can do this, but the easiest way is simply use the method Integer.parseInt().

Example:

String str = "456";
int i = Integer.parseInt( str );

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.