0

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.

5
Contributors
16
Replies
21
Views
7 Years
Discussion Span
Last Post by crishjeny
0

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.

0

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)
    }
0

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.

0

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

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

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

Edited by jugosoft: n/a

0

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.

0

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

0

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

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

0

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.

Edited by jugosoft: n/a

0

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.

0

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.

0

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 );

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.