locked
Input srting was not in a correct format RRS feed

  • Question

  • User1052662409 posted

    Hi all,

    My update query is working fine if I use simple SqlDbCommand object through adding parameters like below

    SqlCommand com = new SqlCommand("StoredProcedure2", con);
            com.CommandType = CommandType.StoredProcedure;
            com.Parameters.Add("@InvoiceComments", SqlDbType.VarChar, 150).Value = InvoiceComments;
            com.Parameters.Add("@InvoiceDate", SqlDbType.DateTime).Value = Convert.ToDateTime(InvoiceDate);
            com.Parameters.Add("@ProductName", SqlDbType.VarChar, 50).Value = ProductName;
            com.Parameters.Add("@Description", SqlDbType.VarChar, 150).Value = Description;
            com.Parameters.Add("@Qty", SqlDbType.Int).Value = Convert.ToInt32(Qty);
            com.Parameters.Add("@Amount", SqlDbType.Int).Value = Convert.ToInt32(Amount);
            com.Parameters.Add("@TaxAmount", SqlDbType.Int).Value = Convert.ToInt32(TaxAmount);
            com.Parameters.Add("@PaidAmount", SqlDbType.Int).Value = Convert.ToInt32(paidAmount);
            com.Parameters.Add("@CustomerName", SqlDbType.VarChar, 50).Value = CustomerName;
            com.Parameters.Add("@CustomerAddress", SqlDbType.VarChar, 150).Value = CustomerAddress;
            com.Parameters.Add("@UnitCost", SqlDbType.Int).Value = Convert.ToInt32(UnitCost);
            com.Parameters.Add("@InvoiceReference", SqlDbType.Int).Value = Convert.ToInt32(InvoiceReference);        
            con.Open();
            com.ExecuteNonQuery();
            con.Close(); return "data updated Successfully";

    There is no problem doing like that. But If I use the Enterprise Library like below

            Microsoft.Practices.EnterpriseLibrary.Data.Database Obj = Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase("InvoiceConString");
            int row1 = Obj.ExecuteNonQuery("StoredProcedure2", InvoiceComments, Convert.ToDateTime(InvoiceDate), ProductName, Description, Convert.ToInt32(Qty), Convert.ToInt32(Amount), Convert.ToInt32(TaxAmount), Convert.ToInt32(paidAmount), CustomerName, CustomerAddress, Convert.ToInt32(UnitCost), Convert.ToInt32(InvoiceReference));
    return "data Updated Successfully";

    It shows error "Input srting was not in a correct format" why?

    Note:- I am not making mistake in converting any variable(which are needed to be converted, because through using the same variables my first code block is working fine)

    Thursday, June 27, 2013 1:07 AM

Answers

  • User220959680 posted

        Microsoft.Practices.EnterpriseLibrary.Data.Database Obj = Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase("InvoiceConString");
            int row1 = Obj.ExecuteNonQuery("StoredProcedure2", InvoiceComments, Convert.ToDateTime(InvoiceDate), ProductName, Description, Convert.ToInt32(Qty), Convert.ToInt32(Amount), Convert.ToInt32(TaxAmount), Convert.ToInt32(paidAmount), CustomerName, CustomerAddress, Convert.ToInt32(UnitCost), Convert.ToInt32(InvoiceReference));
    return "data Updated Successfully";

    Update to the similar one below

    var selectCommand = DatabaseInstance.GetStoredProcCommand("GetReturnValue");
    selectCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@anyname", DbType.Int16));
    
    selectCommand.Parameters["anyname"].Direction = ParameterDirection.ReturnValue;
    
    DatabaseInstance.ExecuteNonQuery(selectCommand);
    
    var intReturnValue = Convert.ToInt64(selectCommand.Parameters["@ReturnValue"].Value);



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 27, 2013 7:51 AM
  • User281315223 posted

    Have you considered placing a breakpoint within your code to ensure that not only each value is in the proper format (for example an unformatted string value isn't attempting to be cast as an integer or DateTime)?

    You would also want to check to ensure that any of the explicitly define lengths of the columns (such as  varchar(50) were not exceeded).

    One final thing to check would be to ensure that the parameters being entered are actually in the order that they appear within the stored procedure. If they are not it could result in the wrong value being passed into your query.

    Another option would be to actually specify the store procedure operation seperately from the database as seen below : 

    //Create your database
    Microsoft.Practices.EnterpriseLibrary.Data.Database Obj = Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase("InvoiceConString");
    
    //Create your DbCommand
    DbCommand dbCommand = Obj.GetStoredProcCommand("StoredProcedure2", InvoiceComments, Convert.ToDateTime(InvoiceDate), ProductName, Description, Convert.ToInt32(Qty), Convert.ToInt32(Amount), Convert.ToInt32(TaxAmount), Convert.ToInt32(paidAmount), CustomerName, CustomerAddress, Convert.ToInt32(UnitCost), Convert.ToInt32(InvoiceReference));
    
    //Execute
    int rows = dbCommand.ExecuteNonQuery();
    
    return "data Updated Successfully";
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 27, 2013 7:55 AM
  • User281315223 posted

    Have you placed a breakpoint within here to see which line is causing the null reference exception? (Typically this will only occur when you attempt to actually reference a property or method from a null object)

    My assumption would be that perhaps the Connection object isn't able to be accessed through your DbCommand object. Have you tried simply trying to execute the command itself? 

    Microsoft.Practices.EnterpriseLibrary.Data.Database Obj = Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase("InvoiceConString");
    
    DbCommand com =Obj.GetStoredProcCommand("StoredProcedure2", InvoiceComments, Convert.ToDateTime(InvoiceDate), ProductName, Description, Convert.ToInt32(Qty), Convert.ToInt32(Amount), Convert.ToInt32(TaxAmount), Convert.ToInt32(paidAmount), CustomerName, CustomerAddress, Convert.ToInt32(UnitCost), Convert.ToInt32(InvoiceReference));
    
    //Execute
    int rows = com.ExecuteNonQuery();
    
    return "data Updated Successfully";

    If you are still encountering issues, place a breakpoint within the code and inspect your DbCommand object to check if the Connection object is null (or if any other properties that you can think of or would access are null as well).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 28, 2013 7:44 AM

All replies

  • User-1356203182 posted

    Have you checked what data is coming to the query? It might be as it is trying to convert string value to int .

    Thursday, June 27, 2013 7:47 AM
  • User220959680 posted

        Microsoft.Practices.EnterpriseLibrary.Data.Database Obj = Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase("InvoiceConString");
            int row1 = Obj.ExecuteNonQuery("StoredProcedure2", InvoiceComments, Convert.ToDateTime(InvoiceDate), ProductName, Description, Convert.ToInt32(Qty), Convert.ToInt32(Amount), Convert.ToInt32(TaxAmount), Convert.ToInt32(paidAmount), CustomerName, CustomerAddress, Convert.ToInt32(UnitCost), Convert.ToInt32(InvoiceReference));
    return "data Updated Successfully";

    Update to the similar one below

    var selectCommand = DatabaseInstance.GetStoredProcCommand("GetReturnValue");
    selectCommand.Parameters.Add(new System.Data.SqlClient.SqlParameter("@anyname", DbType.Int16));
    
    selectCommand.Parameters["anyname"].Direction = ParameterDirection.ReturnValue;
    
    DatabaseInstance.ExecuteNonQuery(selectCommand);
    
    var intReturnValue = Convert.ToInt64(selectCommand.Parameters["@ReturnValue"].Value);



    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 27, 2013 7:51 AM
  • User281315223 posted

    Have you considered placing a breakpoint within your code to ensure that not only each value is in the proper format (for example an unformatted string value isn't attempting to be cast as an integer or DateTime)?

    You would also want to check to ensure that any of the explicitly define lengths of the columns (such as  varchar(50) were not exceeded).

    One final thing to check would be to ensure that the parameters being entered are actually in the order that they appear within the stored procedure. If they are not it could result in the wrong value being passed into your query.

    Another option would be to actually specify the store procedure operation seperately from the database as seen below : 

    //Create your database
    Microsoft.Practices.EnterpriseLibrary.Data.Database Obj = Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase("InvoiceConString");
    
    //Create your DbCommand
    DbCommand dbCommand = Obj.GetStoredProcCommand("StoredProcedure2", InvoiceComments, Convert.ToDateTime(InvoiceDate), ProductName, Description, Convert.ToInt32(Qty), Convert.ToInt32(Amount), Convert.ToInt32(TaxAmount), Convert.ToInt32(paidAmount), CustomerName, CustomerAddress, Convert.ToInt32(UnitCost), Convert.ToInt32(InvoiceReference));
    
    //Execute
    int rows = dbCommand.ExecuteNonQuery();
    
    return "data Updated Successfully";
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Thursday, June 27, 2013 7:55 AM
  • User1052662409 posted

    Rion Williams

    Have you considered placing a breakpoint within your code to ensure that not only each value is in the proper format (for example an unformatted string value isn't attempting to be cast as an integer or DateTime)?

    it doesn't show any wrong information.

    Rion Williams

    You would also want to check to ensure that any of the explicitly define lengths of the columns (such as  varchar(50) were not exceeded).

    Yes, I have already checked for this.

    Rion Williams

    One final thing to check would be to ensure that the parameters being entered are actually in the order that they appear within the stored procedure. If they are not it could result in the wrong value being passed into your query.

    yes they all are in correct order.

    Thanks

    Gaurav

    Friday, June 28, 2013 12:17 AM
  • User1052662409 posted

    Thanks Sukumar, but your code is totaly out of blue for me. I am not saying it is wrong but I don't have enough exp. to understand that.

    Like what are GetReturnValue,@ReturnValue.

    Could you please elaborate using my variables.

    Thanks

    Friday, June 28, 2013 12:21 AM
  • User1052662409 posted

    Thanks Rion, now I have changed my code .

    Microsoft.Practices.EnterpriseLibrary.Data.Database Obj = Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase("InvoiceConString");
            DbCommand com =Obj.GetStoredProcCommand("StoredProcedure2", InvoiceComments, Convert.ToDateTime(InvoiceDate), ProductName, Description, Convert.ToInt32(Qty), Convert.ToInt32(Amount), Convert.ToInt32(TaxAmount), Convert.ToInt32(paidAmount), CustomerName, CustomerAddress, Convert.ToInt32(UnitCost), Convert.ToInt32(InvoiceReference));
            //Execute
    
            if (com.Connection.State != ConnectionState.Open)
            {
                com.Connection.Open();
                int rows = com.ExecuteNonQuery();
                com.Connection.Close();
            }
            return "data Updated Successfully";

    But now it gives error "

    Object reference not set to an instance of an object.

    "

    Friday, June 28, 2013 3:05 AM
  • User281315223 posted

    Have you placed a breakpoint within here to see which line is causing the null reference exception? (Typically this will only occur when you attempt to actually reference a property or method from a null object)

    My assumption would be that perhaps the Connection object isn't able to be accessed through your DbCommand object. Have you tried simply trying to execute the command itself? 

    Microsoft.Practices.EnterpriseLibrary.Data.Database Obj = Microsoft.Practices.EnterpriseLibrary.Data.DatabaseFactory.CreateDatabase("InvoiceConString");
    
    DbCommand com =Obj.GetStoredProcCommand("StoredProcedure2", InvoiceComments, Convert.ToDateTime(InvoiceDate), ProductName, Description, Convert.ToInt32(Qty), Convert.ToInt32(Amount), Convert.ToInt32(TaxAmount), Convert.ToInt32(paidAmount), CustomerName, CustomerAddress, Convert.ToInt32(UnitCost), Convert.ToInt32(InvoiceReference));
    
    //Execute
    int rows = com.ExecuteNonQuery();
    
    return "data Updated Successfully";

    If you are still encountering issues, place a breakpoint within the code and inspect your DbCommand object to check if the Connection object is null (or if any other properties that you can think of or would access are null as well).

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, June 28, 2013 7:44 AM