Ivonne Aspilcueta

Ivonne Aspilcueta

  • 882
  • 622
  • 10.2k

Error when Saving Images

Jul 17 2024 10:52 PM

        protected void Button1_Click(object sender, EventArgs e)
        {
            if (!FileUpload1.HasFile) //Validation
            {
                Response.Write("No file Selected"); return;
            }
            else
            {

                string path = @"Y:\2017\Apr\1-10";
                var files = Directory.GetFiles(path, "index.txt", SearchOption.AllDirectories);
             
                    // Read and populate textboxes from Index.txt        
                    //string indexFilePath = Server.MapPath("~/index.txt");
                    string indexFilePath = Path.Combine(files);
                    
                    PopulateTextBoxes(indexFilePath);

                // Extract values from textboxes
                string acctNumber = txtAcctNumber.Text;
                string chkNumber = txtChkNumber.Text;
                string imgDate = txtDate.Text;
                string imagePath = ViewState["ImagePath"].ToString();

                // Retrieve the base path from the configuration
                string basePath = ConfigurationManager.AppSettings["ImageBasePath"];

                // Construct the full path to the image file
                string fullImagePath = Path.Combine(basePath, imagePath);

                // Convert the image to byte array
                byte[] imageData = ConvertImageToByteArray(fullImagePath);

                // Insert the data into the database
                using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["DBCS"].ToString()))
                {
                    connection.Open();
                    SqlCommand cmd = new SqlCommand("INSERT INTO chkImages (acctNumber, chknumber, imgdate, imageData) VALUES (@acctNumber, @chknumber, @imgdate, @imageData)", connection);

                    cmd.Parameters.AddWithValue("@acctNumber", acctNumber);
                    cmd.Parameters.AddWithValue("@chknumber", chkNumber);
                    cmd.Parameters.AddWithValue("@imgdate", imgDate);
                    cmd.Parameters.AddWithValue("@imageData", imageData);

                    cmd.ExecuteNonQuery();
                    connection.Close();

                    Response.Write("Image has been Added");
                }
            }
        }

Hello,

I am getting an error when run my app, when it is trying to save the data into my table chkImages.

Error Message: System.Data.SqlClient.SqlException: 'The conversion of the nvarchar value '2200130558' overflowed an int column.
The statement has been terminated.'

This is my table I created in SQL:

CREATE TABLE [dbo].[chkImages](
	[Img_ID] [bigint] IDENTITY(1,1) NOT NULL,
	[acctNumber] [int] NULL,
	[chkNumber] [int] NULL,
	[imgDate] [varchar](15) NULL,
	[imageData] [varbinary](max) NULL,

And this is my code:

 


Answers (3)