0

I suddenly started having the following error in more than one C# project only on my PC:

An unhandled exception of type 'System.OverflowException' occurred in System.Data.dll Additional information: Arithmetic operation resulted in an overflow.

I've tried to run my projects with .Net framework 4, then 3.5, 3 and 4.5, but always same error. The error always appears while dealing with my SQL server database, in a project while filling a data table using dataadapter and the other project while trying to open a connection. Here's a sample code:

        string strConn = "Data Source=Home\\SqlExpress;Initial Catalog=PatientDBNew;Integrated Security=True";
        SqlConnection conn = new SqlConnection(strConn);
        string sql = string.Format("Select 'file:///' + FolderPath + PhotoName As PhotoPath From Patient Inner Join Photo On Patient.FileNumber = Photo.FileNumber Where Patient.FileNumber = '{0}' And BeforeOrAfter = {1} Order by DateTimeAdded Asc", Patient.FileNumber, beforeOrAfter);
        SqlDataAdapter da = new SqlDataAdapter(sql, conn); ;
        DataTable dt = null;

        try
        {
            dt = new DataTable();
            da.Fill(dt); //**here I get the exception**
        }
        catch (Exception ex)
        {
            ErrorLog("MethodName", ex);
        }

And this is from the other project:

        string strConn = "Data Source=Home\\SqlExpress;Initial Catalog=PatientDB;Integrated Security=True";
        SqlConnection conn = new SqlConnection(strConn);
        string sql = string.Format("Select Replace([Filenumber], 'A', '20'), [Bphoto_issuing_date], [Aphoto_issuing_date] From [Patient_Master] Where FileNumber like 'A%' Order by FileNumber");
        SqlDataAdapter da = new SqlDataAdapter(sql, conn);
        DataTable dt = new DataTable();
        da.Fill(dt); //**Here I don't get an exception!**

        strConn = "Data Source=MSD-Home;Initial Catalog=PatientDBNew;Integrated Security=True";
        conn = new SqlConnection(strConn);

        foreach (DataRow item in dt.Rows)
        {
            string fileNb = item[0].ToString();
            string startOfTreatment = item[1].ToString();
            string endOfTreatment = item[2].ToString();

            sql = "Update Patient Set BPhotoDate = '" + startOfTreatment + "', APhotoDate = '" + endOfTreatment + "' Where FileNumber = '" + fileNb + "'";

            using (SqlCommand cmd = new SqlCommand(sql))
            {
                cmd.Connection = conn;
                conn.Open(); //**Here I get the exception**
                cmd.ExecuteNonQuery();
                conn.Close();
            }
        }

What is clear is that when dealing with PatientDB it's going well, while when with PatientDBNew it's not. I tried to figure out what's the problem but couldn't. Here's my table "Patient" structure in the PatientDBNew database:

SET ANSI_PADDING ON
GO

CREATE TABLE [dbo].[Patient](
    [FileNumber] [varchar](25) NOT NULL,
    [PhysicianId] [int] NOT NULL,
    [FolderPath] [varchar](MAX) NULL CONSTRAINT [DF_Patient_FolderPath]  DEFAULT (''),
    [BPhotoDate] [datetime] NULL,
    [APhotoDate] [datetime] NULL,
 CONSTRAINT [PK_Patient] PRIMARY KEY CLUSTERED 
(
    [FileNumber] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY] TEXTIMAGE_ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[Patient]  WITH CHECK ADD  CONSTRAINT [FK_Patient_Physician] FOREIGN KEY([PhysicianId])
REFERENCES [dbo].[Physician] ([PhysicianId])
GO

ALTER TABLE [dbo].[Patient] CHECK CONSTRAINT [FK_Patient_Physician]
GO

FilenNumber does not exceed 25 characters, PhysicianId is always 1 or 2, and as I said earlier, this was running without any exception until 2 days ago when I started having this error.

Saleem
  • 709
  • 2
  • 13
  • 34
  • What did you think _"Select 'file:///' + FolderPath + PhotoName As PhotoPath...._ means? – Steve Oct 21 '15 at 22:14
  • You should dispose of your previous connection before assigning a new connection to `conn`. – Eric J. Oct 21 '15 at 22:16
  • @Steve I added the 'file:///' because the result of the query will be used as ReportViewer parameter, so I had to add this at the beginning. – Saleem Oct 21 '15 at 22:17
  • @EricJ. Already tried to enclose the conn in using but didn't work. Plus in the first code sample I use only one connection – Saleem Oct 21 '15 at 22:22

0 Answers0