Ivonne Aspilcueta

Ivonne Aspilcueta

  • 1.1k
  • 601
  • 8.4k

how to Populate in textbox from a path

Jul 12 2024 5:02 PM

Hello,

I want to automatically populate 3 variables: acctNumber, ChkNumber and Date in a textbox and then submit them with the image to be saved in the database, base on the file: Index.txt which contains the path for each image, but I am totally lost on how to populate each variable to save them plus the image into database when I hit the save button.

734 = acctNumber,  7762= chkNumber,  04-03-2017 = ImgDate and png = Image extension

This is the file to read/image example:

734|7762|6000|1015755710|322282894|04-03-2017|25002592|0|734-7762-1015755710-04032017.png
869|1601|101000|1015620730|322275348|04-03-2017|24001896|0|869-1601-1015620730-04032017.png
892|502673|10000|1015690570|43301601|04-03-2017|22003186|0|892-502673-1015690570-04032017.png
982|28862706|17700|1015674700|11900445|04-03-2017|22001593|0|982-28862706-1015674700-04032017.png
983|3342659|542750|1015661030|11900445|04-03-2017|22000220|0|983-3342659-1015661030-04032017.png
983|3363780|542750|1015661020|11900445|04-03-2017|22000219|0|983-3363780-1015661020-04032017.png
1365|8127|123098|1015737890|241274514|04-03-2017|11000215|0|1365-8127-1015737890-04032017.png

This is the interface:

This is my code so far:

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)
        {
            if (!FileUpload1.HasFile) //Validation  
            {
                Response.Write("No file Selected"); return;
            }
            else
            {
                string filename = FileUpload1.PostedFile.FileName;
                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);
                }

                //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");
                }
            }
        }
    }
}

 


Answers (1)