none
The SaveAs method is configured to require a rooted path, and the path RRS feed

  • Question

  • i am try to upload file and save path in database when submit the form show error 

    The SaveAs method is configured to require a rooted path, and the path 

    here is my code kindly advise us. 

    string imgfile = Path.GetFileName(FileImgesave.PostedFile.FileName);
                FileImgesave.SaveAs("Images/" + imgfile);
    
                SqlConnection connn = new SqlConnection(ConfigurationManager.ConnectionStrings["SECODMSConnectionString"].ConnectionString);
    
                connn.Open();
                string insertQuery = "insert tbl_Instrument_detail (tbl_customer_Id,tbl_Instrument_Id,tbl_Instrument_Serial,tbl_inst_install_date,tbl_Installed_Eng,tbl_upload_Installaion_doc)" +
                " values (@CustomerID,@InstrumentID,@InstrumentSerial,@InstrumentInstallDate,@InstrumentInstalledEng,@ImgPath)";
                SqlCommand com = new SqlCommand(insertQuery, connn);
                com.Parameters.AddWithValue("@CustomerID", DropDownCustomer.SelectedValue);
                com.Parameters.AddWithValue("@InstrumentID", DropDownInstrument.SelectedValue);
                com.Parameters.AddWithValue("@InstrumentSerial", txtSerialNo.Text);
                com.Parameters.AddWithValue("@InstrumentInstallDate", ASPxDateEd.Text);
                com.Parameters.AddWithValue("@InstrumentInstalledEng", DropDownListEngineer.SelectedValue);
                com.Parameters.AddWithValue("@ImgPath", "Images/" + imgfile);
                com.ExecuteNonQuery();
                Response.Redirect(Request.Url.AbsoluteUri);
                connn.Close();
    

    Friday, July 26, 2019 5:19 PM

Answers

  • Doesn't the error message make it perfectly clear?  You're doing

        FileImgesave.SaveAs("Images/" + imgfile);

    But where on the disk do you think that "Images" directory is going to be?  When you're handling web requests, the concept of "current directory" is never quite clear.  Thus, as the error message says, you cannot use a relative path.  You must use a path that starts with a drive letter, like "C:/Web/Data/Images/".

    You may be able to use something like HttpContext.Server.MapPath if you want the folder for the request you are handling.



    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Marked as answer by saqsaqPK Friday, July 26, 2019 6:26 PM
    Friday, July 26, 2019 6:22 PM

All replies

  • A guess here would be you need the path for Images, not just Images/

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Friday, July 26, 2019 5:49 PM
    Moderator
  • kindly advise us how to solved this issue
    Friday, July 26, 2019 5:51 PM
  • Doesn't the error message make it perfectly clear?  You're doing

        FileImgesave.SaveAs("Images/" + imgfile);

    But where on the disk do you think that "Images" directory is going to be?  When you're handling web requests, the concept of "current directory" is never quite clear.  Thus, as the error message says, you cannot use a relative path.  You must use a path that starts with a drive letter, like "C:/Web/Data/Images/".

    You may be able to use something like HttpContext.Server.MapPath if you want the folder for the request you are handling.



    Tim Roberts | Driver MVP Emeritus | Providenza & Boekelheide, Inc.

    • Marked as answer by saqsaqPK Friday, July 26, 2019 6:26 PM
    Friday, July 26, 2019 6:22 PM
  • Thanks for your reply i under stand and updaate the code know my problem solved .
     string path = Server.MapPath(@"Images/Misc/Installation_Certificate/");
            if (FileImgesave.PostedFile != null)
    
            {
                string imgfile = Path.GetFileName(FileImgesave.PostedFile.FileName);
                FileImgesave.SaveAs(path + imgfile);
                 string name = (@"Images/Misc/Installation_Certificate/" + txtSerialNo.Text + "_"+imgfile);
    
                SqlConnection connn = new SqlConnection(ConfigurationManager.ConnectionStrings["SECODMSConnectionString"].ConnectionString);
    
                connn.Open();
                string insertQuery = "insert tbl_Instrument_detail (tbl_customer_Id,tbl_Instrument_Id,tbl_Instrument_Serial,tbl_inst_install_date,tbl_Installed_Eng,tbl_upload_Installaion_doc)" +
                " values (@CustomerID,@InstrumentID,@InstrumentSerial,@InstrumentInstallDate,@InstrumentInstalledEng,@ImgPath)";
                SqlCommand com = new SqlCommand(insertQuery, connn);
                com.Parameters.AddWithValue("@CustomerID", DropDownCustomer.SelectedValue);
                com.Parameters.AddWithValue("@InstrumentID", DropDownInstrument.SelectedValue);
                com.Parameters.AddWithValue("@InstrumentSerial", txtSerialNo.Text);
                com.Parameters.AddWithValue("@InstrumentInstallDate", ASPxDateEd.Text);
                com.Parameters.AddWithValue("@InstrumentInstalledEng", DropDownListEngineer.SelectedValue);
                com.Parameters.AddWithValue("@ImgPath", name);
                com.ExecuteNonQuery();
                Response.Redirect(Request.Url.AbsoluteUri);
                connn.Close();

    Friday, July 26, 2019 6:26 PM