none
Error that Divide by zero error encountered? RRS feed

  • Question

  • Hello

    I got error that below. What should i do ? How can i fix my query ?

    Thanks. 

    ---------------------------

    ---------------------------
    Divide by zero error encountered.   at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean breakConnection)

       at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning()

       at System.Data.SqlClient.TdsParser.Run(RunBehavior runBehavior, SqlCommand cmdHandler, SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject stateObj)

       at System.Data.SqlClient.SqlDataReader.HasMoreRows()

       at System.Data.SqlClient.SqlDataReader.ReadInternal(Boolean setTimeout)

       at System.Data.Common.DataAdapter.FillLoadDataRow(SchemaMapping mapping)

       at System.Data.Common.DataAdapter.FillFromReader(DataSet dataset, DataTable datatable, String srcTable, DataReaderContainer dataReader, Int32 startRecord, Int32 maxRecords, DataColumn parentChapterColumn, Object parentChapterValue)

       at System.Data.Common.DataAdapter.Fill(DataTable[] dataTables, IDataReader dataReader, Int32 startRecord, Int32 maxRecords)

       at System.Data.Common.DbDataAdapter.FillInternal(DataSet dataset, DataTable[] datatables, Int32 startRecord, Int32 maxRecords, String srcTable, IDbCommand command, CommandBehavior behavior)

       at System.Data.Common.DbDataAdapter.Fill(DataTable[] dataTables, Int32 startRecord, Int32 maxRecords, IDbCommand command, CommandBehavior behavior)

       at System.Data.Common.DbDataAdapter.Fill(DataTable dataTable)

       at MsDataAccess.ExecuteQuery(SqlConnection connection, String query, Object[] pObjects) in C:\_Work\_Projeler\winform\TicariSet\TicariSet\MsDataAccess.cs:line 278
    At
    SELECT       

     

    x_faturacikisdetail_F_001.x_kod as x_kod,

    x_faturacikisdetail_F_001.x_adi as x_adi,

    x_faturacikisdetail_F_001.x_miktar as x_miktar,

    x_faturacikisdetail_F_001.x_fiyat as x_fiyat,

    x_faturacikisdetail_F_001.x_stutar - x_faturacikisdetail_F_001.x_kdvtutari - x_faturacikisdetail_F_001.x_oivtutari as x_tutar,

    x_stokkarti_F_001.x_stokkartalisf as x_brmmaliyet,

    x_faturacikisdetail_F_001.x_miktar* x_stokkarti_F_001.x_stokkartalisf as x_maliyet,

    x_faturacikisdetail_F_001.x_tutar-(x_faturacikisdetail_F_001.x_miktar * x_stokkarti_F_001.x_stokkartalisf) as x_kar,

    ((x_faturacikisdetail_F_001.x_tutar-(x_faturacikisdetail_F_001.x_miktar * x_stokkarti_F_001.x_stokkartalisf))/(x_faturacikisdetail_F_001.x_miktar* x_stokkarti_F_001.x_stokkartalisf))*100 as x_karyuzde

    FROM            x_faturacikisdetail_F_001 INNER JOIN

                             x_stokkarti_F_001

    ON x_faturacikisdetail_F_001.x_sid = x_stokkarti_F_001.x_id

     INNER JOIN

                             x_faturacikismaster_F_001 ON x_faturacikisdetail_F_001.x_faturacikismasterid = x_faturacikismaster_F_001.x_id INNER JOIN

                             x_carikarti_F_001 ON x_faturacikismaster_F_001.x_cariid = x_carikarti_F_001.x_id

    WHERE       (NOT (x_faturacikisdetail_F_001.x_id IS NULL))  AND (x_faturacikismaster_F_001.x_faturatarihi >= @x_faturatarihimin) AND (x_faturacikismaster_F_001.x_faturatarihi <= @x_faturatarihimax) AND (x_faturacikismaster_F_001.x_vadetarihi >= @x_vadetarihimin) AND (x_faturacikismaster_F_001.x_vadetarihi <= @x_vadetarihimax)
    ---------------------------
    Tamam  
    ---------------------------

    Monday, August 13, 2012 11:00 PM

Answers

All replies

  • Anywhere you are performing division on data, you should check for zero values.  It looks like you only have one place where you are performing division, which is here:

    ((x_faturacikisdetail_F_001.x_tutar-(x_faturacikisdetail_F_001.x_miktar * x_stokkarti_F_001.x_stokkartalisf))/(x_faturacikisdetail_F_001.x_miktar* x_stokkarti_F_001.x_stokkartalisf))*100 as x_karyuzde

    If either x_faturacikisdetail_F_001.x_miktar or x_stokkarti_F_001.x_stokkartalisf contain a value of 0, you will hit the divide by zero error.  You can check those two values for 0 to prevent the divide by zero error.

    Thanks,
    Sam Lester (MSFT)


    My Blog

    This posting is provided "AS IS" with no warranties, and confers no rights. Please remember to click "Mark as Answer" and "Vote as Helpful" on posts that help you. This can be beneficial to other community members reading the thread.

    Monday, August 13, 2012 11:08 PM
    Moderator
  • I solved that using COALESCE NULLIF.
    • Marked as answer by MSDN.CSharp Monday, August 13, 2012 11:42 PM
    Monday, August 13, 2012 11:42 PM