C#
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:
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:


Anoop Kumar SharmaRank #127 · 17.4K reputationPosted Jul 17, 2024, 10:52 PM EDT
Hi Ivonne Aspilcueta,
Either the size of acctNumber or chkNumber is going beyond the int max size (-2,147,483,648 to 2,147,483,647). You need to change the data type to bigint in the database. Just identify the value you are passing in the acctNumber or chkNumber, which ever is going beyond the int max size, change its data type to bigint.
I hope this will help you.
Asma KhalidRank #160 · 14.7K reputationPosted Jul 17, 2024, 10:57 PM EDT
Convert to DB table data type to float instead because sometimes even bigint gives error for very large integers.
Vishal JoshiRank #377 · 6.7K reputationPosted Jul 18, 2024, 1:00 AM EDT
Hello
You need to set the datatype with command parameter. please try the below code.
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); SqlParameter imgParam = new SqlParameter("@imageData", SqlDbType.VarBinary, imageData.Length); imgParam.Value = imageData; cmd.Parameters.Add(imgParam); cmd.ExecuteNonQuery(); connection.Close(); Response.Write("Image has been Added"); } } }Thanks
Vishal Joshi