Hello,
I am trying to read a image path to save variables: acctnumber, chknumber and date and the image into a database, I have created the connection with those variables and to read the file to a some extent but I am totally lost on how to substract each variable to save them plus the image into database when I hit the save button.
This is the file to read/image example:
1991|4145|42551|1015737750|321173483|04-03-2017|11000201|0|1991-4145-1015737750-04032017.png
where 1991 = acctNumber, 4145= chkNumber, 04-03-2017 = ImgDate and png = Image extension
using System;
using System.Configuration;
using System.Data;
using System.Data.SqlClient;
using System.IO;
namespace ImageToBinary
{
public partial class Conversion : System.Web.UI.Page
{
protected void Button1_Click(object sender, EventArgs e)
{
// Load file meta data with FileInfo
FileInfo fileInfo = new FileInfo(path);
// The byte[] to save the data in
byte[] data = new byte[fileInfo.Length];
// Load a filestream and put its content into the byte[]
using (FileStream fs = fileInfo.OpenRead())
{
fs.Read(data, 0, data.Length);
}
//convert the image into the byte
//Insert the Data in the Table
using (SqlConnection connection = new SqlConnection())
{
connection.ConnectionString = ConfigurationManager.ConnectionStrings["DBCS"].ToString();
connection.Open();
SqlCommand cmd = new SqlCommand();
cmd.Connection = connection;
string commandText = "Insert into chkImages values (@acctNumber,@chknumber,@imgdate)";
cmd.CommandText = commandText;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@acctNumber", SqlDbType.Int);
cmd.Parameters.Add("@chknumber", SqlDbType.Int);
cmd.Parameters.Add("@imgdate", SqlDbType.VarChar);
cmd.ExecuteNonQuery();
cmd.Dispose();
connection.Close();
Response.Write("Image has been Added");
}
}
}
}

Nikunj SatasiyaRank #302 · 8.3K reputationPosted Jul 10, 2024, 9:12 PM EDT
Hi Ivonne,
Let i modifay your code and expain how you can implement this in your
Button1_Clickevent handler:protected void Button1_Click(object sender, EventArgs e) { string imagePath = "1991|4145|42551|1015737750|321173483|04-03-2017|11000201|0|1991-4145-1015737750-04032017.png"; // Extract variables from imagePath string[] parts = imagePath.Split('|'); int acctNumber = int.Parse(parts[0]); int chkNumber = int.Parse(parts[1]); string imgDate = parts[5]; // Assuming 04-03-2017 is the date part of the image path // Load file meta data with FileInfo string path = Server.MapPath("~/Images/") + imagePath; FileInfo fileInfo = new FileInfo(path); // The byte[] to save the data in byte[] data = new byte[fileInfo.Length]; // Load a filestream and put its content into the byte[] using (FileStream fs = fileInfo.OpenRead()) { fs.Read(data, 0, data.Length); } // Insert the Data into the Table string connectionString = ConfigurationManager.ConnectionStrings["DBCS"].ToString(); using (SqlConnection connection = new SqlConnection(connectionString)) { connection.Open(); SqlCommand cmd = new SqlCommand(); cmd.Connection = connection; // Example command text to insert into chkImages table string commandText = "INSERT INTO chkImages (acctNumber, chknumber, imgdate, imageData) VALUES (@acctNumber, @chknumber, @imgdate, @imageData)"; cmd.CommandText = commandText; cmd.CommandType = CommandType.Text; // Parameters cmd.Parameters.AddWithValue("@acctNumber", acctNumber); cmd.Parameters.AddWithValue("@chknumber", chkNumber); cmd.Parameters.AddWithValue("@imgdate", imgDate); cmd.Parameters.AddWithValue("@imageData", data); // Execute SQL command cmd.ExecuteNonQuery(); cmd.Dispose(); connection.Close(); // Response message Response.Write("Image and data have been added to the database."); } }Hope this will work for you.
Vishal JoshiRank #377 · 6.7K reputationPosted Jul 11, 2024, 3:13 AM EDT
Hello Ivonne,
Please check-out the below sample code to extract the file details. You can use a separate function to extract the expected details and modify based on requirements.
private static void GetFileDetails(out string imageExtension, out string accountNumber, out string chkNumber, out string idNumber, out DateTime imageDate) { string strPath = "1991|4145|42551|1015737750|321173483|04-03-2017|11000201|0|1991-4145-1015737750-04032017.png"; //function call to get the filename string filename = strPath.Split('|').Last(); string[] fileDetails = filename.Split('-').ToArray(); imageExtension = Path.GetExtension(filename); accountNumber = fileDetails[0].Trim(); chkNumber = fileDetails[1].Trim(); idNumber = fileDetails[2].Trim(); imageDate = DateTime.ParseExact(fileDetails[3].Replace(imageExtension, "").Trim(), "MMddyyyy", System.Globalization.CultureInfo.InvariantCulture); }Thank
Vishal Joshi