-3

I am getting this exception Input string was not in a correct format while executing the mysql stored procedure in C#.

Below is my sample code:

        MySqlManager dac = new MySqlManager();
        DbCommand dbCommand = dac.GetStoredProcCommand("DocdetailsById");
        dac.AddInParameter(dbCommand, "p_P_ID", DbType.Int32, id);
        dac.AddInParameter(dbCommand, "p_P_Mode", DbType.Int32, mode);
        dac.AddInParameter(dbCommand, "p_P_IPADDRESS", DbType.String, ipaddress);
        dac.AddInParameter(dbCommand, "p_P_OsBrowser", DbType.String, SessionManager.OSandBrowser);
        dac.AddInParameter(dbCommand, "p_P_Subscriberid", DbType.Int32, SessionManager.SubscriberId);
        IDataReader reader = dac.ExecuteReader(dbCommand);
        List<FilesDTO> result = new List<FilesDTO>();
        while (reader.Read())
        {
            result.Add(new FilesDTO
            {
                EmailID = reader["EmailID"] == DBNull.Value ? default(string) : Convert.ToString(reader["EmailID"]),
                DocumentID = reader["DocumentID"] == DBNull.Value ? default(int) : Convert.ToInt32(reader["DocumentID"]),
                Name = reader["Name"] == DBNull.Value ? default(string) : Convert.ToString(reader["Name"]),
                DocumentName = reader["DocumentName"] == DBNull.Value ? default(string) : Convert.ToString(reader["DocumentName"]),
                StatusID = reader["StatusID"] == DBNull.Value ? default(int) : Convert.ToInt32(reader["StatusID"]),
                SubscriberID = reader["SubscriberID"] == DBNull.Value ? default(int) : Convert.ToInt32(reader["SubscriberID"]),
                CreatedBy = reader["Initiater"] == DBNull.Value ? default(int) : Convert.ToInt32(reader["Initiater"]),
                DelegatorName = reader["DelegatorName"] == DBNull.Value ? default(string) : Convert.ToString(reader["DelegatorName"]),
                DelegatorEmailID = reader["DelegatorEmailID"] == DBNull.Value ? default(string) : Convert.ToString(reader["DelegatorEmailID"]),
                SignatureType = reader["SignatureType"] == DBNull.Value ? default(Int16) : Convert.ToInt16(reader["SignatureType"])
            });
        }
        dac.CloseConnection(dbCommand, reader);
        return result;

Once the ExecuteReader execution is done, i am getting the exception in reader.Read() as System.formatexception in DocumentID = reader["DocumentID"] == DBNull.Value ? default(int) : Convert.ToInt32(reader["DocumentID"]),

Please Can anyone help me? Thanks.

Chandu
  • 97
  • 1
  • 9
  • 1
    Inspect the value of reader["DocumentID"]. Seems there is something in there that cannot be converted to an int. – RazorShorts Aug 14 '18 at 06:56
  • 1
    can you please add if (reader.HasRows) { before while (reader.Read()) – Gauravsa Aug 14 '18 at 07:06
  • It could be the right moment to use an ORM. Any thing from Linq, Entity to Dapper. This way querrying a stored procedure and map the result to the a DTO with proper default value will take about 3 lines of code. – Drag and Drop Aug 14 '18 at 07:09
  • As RazorShorts said, May you [edit] your question with the value of `reader["DocumentID"]` when the error happend? This way we will have all the needed information. – Drag and Drop Aug 14 '18 at 07:24

2 Answers2

0

You can try Convert.ToInt32 after you've converted it to string first.

DocumentID = reader["DocumentID"] == DBNull.Value
                                    ? default(int) 
                                    : Convert.ToInt32(reader["DocumentID"].ToString()),
Drag and Drop
  • 2,672
  • 3
  • 25
  • 37
Faisal Rashid
  • 368
  • 1
  • 11
0

You are getting FormatException as part of executing the Convert.ToInt32 and Convert.ToInt16. which is happening when the input value is either null or not convertible to an integer. I prefer you to use Int32.TryParse() and Int16.TryParse() instead for Convert.To..

For Example :

int documentID = 0;
Int32.TryParse(reader["DocumentID"], out documentID)

For your scenario, you need to perform this operations repeatedly for different fields, so better to define a method like the following which accepts an object and return integer. The signature of the method will be like the following:

public static int GetInteger(object readerValue)
{
     int outputInt = 0;
     int.TryParse(readerValue.ToString(), out outputInt);
     return outputInt;
}

So that your code can be re-written as :

while (reader.Read())
{
    result.Add(new FilesDTO
    {
        EmailID = reader["EmailID"] == DBNull.Value ? default(string) : Convert.ToString(reader["EmailID"]),
        DocumentID = GetInteger(reader["DocumentID"]),
        Name = reader["Name"] == DBNull.Value ? default(string) : Convert.ToString(reader["Name"]),
        DocumentName = reader["DocumentName"] == DBNull.Value ? default(string) : Convert.ToString(reader["DocumentName"]),
        StatusID = GetInteger(reader["StatusID"]),
        SubscriberID = GetInteger(reader["SubscriberID"]),
        CreatedBy = GetInteger(reader["Initiater"]),
        DelegatorName = reader["DelegatorName"] == DBNull.Value ? default(string) : Convert.ToString(reader["DelegatorName"]),
        DelegatorEmailID = reader["DelegatorEmailID"] == DBNull.Value ? default(string) : Convert.ToString(reader["DelegatorEmailID"]),
        SignatureType = GetInteger(reader["SignatureType"])
    });
}
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • for shorter code and to avoid to mutch code modification we can declare the type of the out parameter directly in the Tryparse like : `Int32.TryParse(reader["DocumentID"], out int documentID)`. So code will end like : `DocumentID = reader["DocumentID"] == Int32.TryParse(reader["DocumentID"], out int documentID) ? default(int) : documentID,` – Drag and Drop Aug 14 '18 at 07:20
  • @DragandDrop: that's nice suggestion, thank you – sujith karivelil Aug 14 '18 at 07:27