c# - Storing Images in Project Folder - Directory Not Found Error Message -
i trying save images slideimages folder in asp.net web forms website, using c#.
when try submit image using following code:
protected void btnsubmitimage_click(object sender, eventargs e) { //get filename fileupload control string filename = path.getfilename(fileuploadimages.postedfile.filename); //save images slideimages folder fileuploadimages.saveas(server.mappath("slideimages/" + filename)); //open database connection con7.open(); //query insert images name , description database sqlcommand cmd = new sqlcommand("insert slideshowtable(imagename,description) values(@imagename,@description)", con7); //passing parameters query cmd.parameters.addwithvalue("@imagename", filename); cmd.parameters.addwithvalue("@description", txtdesc.text); cmd.executenonquery(); //close dbconnection con7.close(); txtdesc.text = string.empty; binddatalist(); }
i below error message when trying execute 4th line of code above.
an exception of type 'system.io.directorynotfoundexception' occurred in mscorlib.dll not handled in user code. additional information: not find part of path 'c:\users\11342\onedrive\documents\4th year\fyp\ballinorawaterfallcommunity\slideimages\'.
here project folder structure. trying store images in slideimages folder of project.
i managed store images in 'images' folder using below code. have tried adapt fix above problem, have not been able to.
protected void btnsubmit_click(object sender, eventargs e) { //get filename fileupload control string filename = path.getfilename(fileuploadimages.postedfile.filename); //save images images folder fileuploadimages.saveas(server.mappath("images/" + filename)); //getting dbconnection web.config connectionstring sqlconnection con = new sqlconnection(configurationmanager.connectionstrings["ballinoradbconnectionstring1"].tostring()); //open database connection con.open(); //query insert images path , name database sqlcommand cmd = new sqlcommand("insert group_images(imagename,imagepath) values(@imagename,@imagepath)", con); //passing parameters query cmd.parameters.addwithvalue("@imagename", filename); cmd.parameters.addwithvalue("@imagepath", "images/" + filename); cmd.executenonquery(); //close dbconnection con.close(); response.redirect("~/admin.aspx"); }
you should use server.mappath("~/slideimages)
. notice ~/
prefix forces mappath start @ root of web application.
then add file name file not exist yet on disk should use system.io.path.combine
combine mapped root website path (and images folder) file name.
also should check if directory exists or not. in screen shot have content in images
folder being copied destination website path when deploy. other 1 empty, might not created when deploy application because there no content copy.
here modified code, check if exists , create if not. should verify web application's user context has sufficient privileges create directory in root website directory.
var directory = server.mappath("~/slideimages"); if(!system.io.directory.exists(directory)) system.io.directory.create(directory); fileuploadimages.saveas(system.io.path.combine(directory, filename));
side note
you should wrap instances implement idisposable
using
blocks or make sure disposed of in dispose method of containing type. noticed in 2nd example created sqlconnection
instance , called close
later not practice. why is because if have exception occurs sqlconnection, sqlcommand, or else needs clean resources stick around longer needed can cause issues later on exceeding max number of simultaneous allowed open connections sql server or not being able serve image because there lock on file.
for more information see best practices - executing sql statements
Comments
Post a Comment