0

I am trying to get the String value in a list but getting error as

Input string was not in a correct format.

There are many helpful links available on the net but didn't resolved mine one.

Here is my code.

newRow["EXP_TYPE_ID"] = Convert.ToString(e.Record["EXP_TYPE"]);
    newRow["EXP_TYPE"] = CF.ExecuteScaler("Select Type_desc from type_mst where Type_Code = 'PAR' and Type_Abbr ='" + Convert.ToString(e.Record["EXP_TYPE"]) + "'").ToString();

and CF.ExecuteScaler

public string ExecuteScaler(string StrQuery)
{
    DB.EConnection();
    cmd = new OracleCommand(StrQuery, DB.conn);
    cmd.Connection = DB.conn;
    int val=Convert.ToInt32(cmd.ExecuteScalar());
    DB.conn.Close();
    string ret = val.ToString();
    return ret;
}

Note I can't change Scaler function.

update

StrQuery = Select Type_desc from type_mst where Type_Code = 'PAR' and Type_Abbr ='PUR'

and the value of the query

PURCHASER

8
  • Could you please show us the value of StrQuery when you pass it to ExecuteScaler()? Commented Oct 7, 2016 at 6:26
  • Where exactly are you getting the error? Commented Oct 7, 2016 at 6:27
  • @MegaTron: Updated the question with more details Commented Oct 7, 2016 at 6:28
  • @sachin: I get error at while executing my query. Commented Oct 7, 2016 at 6:29
  • what type is Type_desc from the query? if it's string, what are the possible values? it looks like it's not convertible to int and that's why it's throwing the exception Commented Oct 7, 2016 at 6:44

1 Answer 1

1

I believe the exception is thrown in the following line?

int val=Convert.ToInt32(cmd.ExecuteScalar());

As you have said, cmd.ExecuteScalar() returns value "PURCHASER", which then you are trying to convert into int. That is clearly not possible, and this is what the exception is saying.

Please try it like that:

public string ExecuteScaler(string StrQuery)
{
    DB.EConnection();
    cmd = new OracleCommand(StrQuery, DB.conn);
    cmd.Connection = DB.conn;
    string ret = Convert.ToString(cmd.ExecuteScalar());
    DB.conn.Close();
    return ret;
}
Sign up to request clarification or add additional context in comments.

3 Comments

getting error as cannot implicitly convert type int to string at line Convert.ToInt32(cmd.ExecuteScalar());
@nad But you said before: Note I can't change Scaler function.
@MegaTron: I added one more function and renamed it. I said,I cant change the function, but I can add function

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.