You must Sign In to post a response.
  • Category: ASP.NET

    Can we get Images from the database at the same time can Insert that images into database

    Hi Developers '

    i want to get records from the database.
    Insert getting images into database and Also Save the Inserted images to a Folder (Created folder in my Project).
    in my query i am get and inserted.
    i can get images.
    i can inserted that images to database.

    but the images does not save to a Folder (Created folder in my Project).

    Error:

    Server Error in /"MyApplication"/

    StartIndex cannot be less than zero.
    Parameter name: startIndex

    string extension = "";
    int n = filename.LastIndexOf(".");
    extension = filename.Substring(n);
    return extension;
    }

    My Using code is below :

    SqlConnection con = Connection.DBConn();

    for (int i = 0; i < regnumber.Length; i++)
    {
    string str = "select APPLICATION_ID,PHOTO,signature,FULL_NAME from APPLICATION where application_id='"+regnumber[i].ToString()+"'";
    SqlDataAdapter sda = new SqlDataAdapter(str, con);
    DataSet ds=new DataSet();
    sda.Fill(ds);
    if (ds.Tables[0].Rows.Count > 0)
    {

    string imagename = ds.Tables[0].Rows[0]["photo"].ToString();
    string signname = ds.Tables[0].Rows[0]["signature"].ToString();
    string fullname = ds.Tables[0].Rows[0]["full_name"].ToString();
    string path = Server.MapPath("~/");
    string path1 = "http://localhost:42216/EFA%20CHANGES%20FINISHED%2005-06-16/admin/EFA%20-ACCEPTED%20FILES%20-%20PUGAZ%20MIALED/Images.aspx";
    string photo_path = "";
    string photo = "";
    string sign_path = "";
    string sign = "";
    photo = imagename;
    sign = signname;


    if (FileUpload.PostedFile.FileName == "")
    {

    photo_path = "/MayPhotos/" +getExtension (FileUpload.FileName);
    photo = path1 + "/MayPhotos/" + getExtension(FileUpload.FileName);
    FileUpload.SaveAs(path + photo_path);
    }
    if (FileUpload1.PostedFile.FileName == "")
    {
    sign_path = "/MaySign/" + (FileUpload1.FileName);
    sign = path1 + "/MaySign/" + (FileUpload1.FileName);
    FileUpload1.SaveAs(path + sign_path);
    }
    string insert = "insert into APPLICATION_may_photo (application_id,photo,signature,full_name) values (@application_id,@photo,@signature,@full_name)";
    SqlCommand com = new SqlCommand(insert, con);
    com.Parameters.AddWithValue("@application_id", regnumber.ToString());
    com.Parameters.AddWithValue("@photo", photo.ToString());
    com.Parameters.AddWithValue("@signature", signname.ToString());
    com.Parameters.AddWithValue("@full_name", fullname.ToString());
    com.ExecuteNonQuery();
    }


    so please help me to i am resolve this error and done my task is important.
    thanking you
    Paul.S
  • #766199
    Hi

    your error related index based this

    StartIndex cannot be less than zero.
    Parameter name: startIndex

    whether you get filename or getdatas is null i think then only your index issue came

    trace debugging where you meet this

    i mention sample code this is working try this


    string path1 = "http://localhost:42216/EFA%20CHANGES%20FINISHED%2005-06-16/admin/EFA%20-ACCEPTED%20FILES%20-%20PUGAZ%20MIALED/Images.aspx";
    string extension = "";
    int n = path1.LastIndexOf(".");
    extension = path1.Substring(n);
    [/COD]

    I am also attached snapshot also

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #766200
    Hi,

    I suspect that the issue might be in this line "extension = filename.Substring(n);", as per error details startIndex cannot be larger than length of string, I suggest you to debug your code and check the string length, and check the length condition, it should be greater than 0 in size. Then only you can perform action against that string.

    If you want to know how to insert images into folder of the project and save the file name in database record, I already prepare one article for the same, I suggest you to refer this link, this might be helpful to you.

    http://www.dotnetspider.com/resources/45091-Insert-Edit-Update-Delete-Get-back-Image-from-DataBase.aspx

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #766201
    error message 'Index cannot be less than zero' indicates your are trying fetch something from an array or list which is not exist there,
    from your code it looks problem exist in your below line
    filename.LastIndexOf(".");
    if you want to get the extension of file name then use following code

    //instead of below code
    string extension = "";
    int n = filename.LastIndexOf(".");
    extension = filename.Substring(n);

    //use this code
    string yourPath = "D:\\test.txt";
    string szExtension = Path.GetExtension(yourPath); // returns .txt

    hope it helps

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]

  • #766206
    Thanks Mr.Naveen and Mr.Developer thanks for your reply .

    i am having a question,
    Can we get Images from database and at the same time i need to Save the Images into a Folder in my Project which is am get from the database. 2 actions will be execute in a single click.
    Save the Images to a Folder with extension is very important.
    Images should be save without using FileUpload Control.

    My Requirement is possible or Not Possibe .. ? Mr.Naveen and Mr.Developer
    if possible na please suggest me to how to done it
    Thanks with
    Paul.S

  • #766208
    Hi Paul,

    Could you please clarify onething, "Get Images from database" means already images are saved in database? those images you want to save it into your project solution.

    --------------------------------------------------------------------------------
    Give respect to your work, Instead of trying to impress your boss.

    N@veen
    Blog : http://naveens-dotnet.blogspot.in/

  • #766209
    Hi
    Paul

    I need little bit clarify in your Query.Because Mostly People using Fileupload Control to save Db for 2 type .

    1. File path stored to Db
    2. Binary File format to db

    after we can shows the images.

    But you said when image retrieve that time save images in your local path

    Do one thing

    Solution
    ==========
    when you retrieve the images from db you can file extension also
    so you can extract the filepath then using fso concept save or delete also.

    Name : Dotnet Developer-2015
    Email Id : kumaraspcode2009@gmail.com

    'Not by might nor by power, but by my Spirit,' says the LORD Almighty.

  • #766211
    Yes, it is possible when you fetch image from database you can directly save it in folder
    see below snippet to know how to do it

    //read with reader
    while (objDR.read())
    {
    //store imageColumn in byte
    Byte[] objBlob = (Byte)objDR["imageColumn"]
    //write blob in file
    System.IO.File.writeAllBytes("d:\test\1.jpg", objBlob);
    }


    here are the two simple line to get file from database and save it in folder
    [note: when you fetch image from database, its file extension should not be changed]

    Thanks
    Koolprasd2003
    Editor, DotNetSpider MVM
    Microsoft MVP 2014 [ASP.NET/IIS]


  • Sign In to post your comments