c# - "The ConnectionString property has not been initialized." Error in SQL bulkcopy upload -
i created mvc 4 application,in application have function upload excel file database.
this working fine in localhost .
but when deploy in iis , try upload excel file.i'm getting following error
the connectionstring property has not been initialized.
these connection strings i'm using upload excel files
<add name="dbconnection" connectionstring="data source="000.000.00.00";initial catalog=affhec_db;persist security info=true;user id=**;password=****" providername="system.data.sqlclient" /> <add name="constr" connectionstring="data source=000.000.0.000;initial catalog=affhec_db;user id=**;password=*****;" /> <add name="excel07+constring" connectionstring="provider=microsoft.ace.oledb.12.0;data source={0};extended properties='excel 8.0;hdr=yes';" /> <add name="excel03constring" connectionstring="provider=microsoft.jet.oledb.4.0;data source={0};extended properties='excel 8.0;hdr=yes'" />
this code sql bulk copy upload
[httppost] [acceptverbs(httpverbs.post)] public actionresult student(httppostedfilebase fileupload1, tbl_hec_programme programme) { try { string constring = string.empty; //upload , save file if (request.files["fileupload1"].contentlength > 1) { try { string excelpath = path.combine(httpcontext.server.mappath("~/content/"), path.getfilename(fileupload1.filename)); fileupload1.saveas(excelpath); string extension = system.io.path.getextension(fileupload1.filename); constring = string.format(constring, excelpath); } catch (exception ex) { this.setnotification("the file type submitted invalid", notificationenumeration.error); } try { using (oledbconnection excel_con = new oledbconnection(constring)) { excel_con.open(); datatable dtexceldata = new datatable(); string query = "select " + "s1.hec_id, " + "s1.student_personal_id, " + "s1.student_passport_number, " + "s1.student_id_by_university, " + "s1.first_name, " + // // join 3 tables "from (((([personal_data$] s1) " + "left outer join [enrolment_data$] s2 on s1.hec_id = s2.hec_id) " + using (oledbdataadapter oda = new oledbdataadapter(query, excel_con)) { oda.fill(dtexceldata); } string consstring = configurationmanager.connectionstrings["constr"].connectionstring; using (sqlconnection con = new sqlconnection(consstring)) { using (sqlbulkcopy sqlbulkcopy = new sqlbulkcopy(con, sqlbulkcopyoptions.checkconstraints | sqlbulkcopyoptions.firetriggers | sqlbulkcopyoptions.keepnulls | sqlbulkcopyoptions.tablelock | sqlbulkcopyoptions.useinternaltransaction | sqlbulkcopyoptions.keepidentity, null)) { //set database table name sqlbulkcopy.destinationtablename = "tbl_hei_student"; sqlbulkcopy.bulkcopytimeout = 0; sqlbulkcopy.columnmappings.add("id", "id"); sqlbulkcopy.columnmappings.add("student_personal_id", "student_personal_id_cpr"); sqlbulkcopy.columnmappings.add("student_passport_number", "student_passport_number"); ................ con.open(); try { sqlbulkcopy.writetoserver(dtexceldata); int totalrowsadded = dtexceldata.rows.count; uploadstatuswriter(user.identity.getuserid(), status_type, date_type); this.setnotification("student data uploaded", notificationenumeration.success); return redirecttoaction("studentindex", "hei"); } catch (exception ex) { this.setnotification(ex.message, notificationenumeration.error); return redirecttoaction("student", "excel"); } { con.close(); } } } } } catch (exception ex) { this.setnotification(ex.message, notificationenumeration.error); return redirecttoaction("student", "excel"); } } else { this.setnotification("please select file first , click submit button", notificationenumeration.error); return redirecttoaction("student", "excel"); } } catch (exception ex) { return redirecttoaction("student", "excel"); } }
i had same problem. after many many researches found should configurations in iis deployment allow permissions release folder.
here configurations
step 1
add proper site name
"content directory" chose correct path of release folder
for binding, "type" give https or http , if deploy in machine ip address give machine ip address else server ip address , find proper port also.
for application pool keep defaultapppool now
step 2
then go applications pools find web site's application pool , change asp.net v4.0 (if .net framework 4.0)
step 3
go web application main panel find directory browsing (under iis section)
step 4
double click directory browsing , enable
step 5
find asp in application on main panel(under iis section)
step 6
right click on asp find enable parent path (under behavior). make value true
step 7
then go main panel in iis manger tool , under sites find deployed site.then right click .find explore go location of release folder
step 8
right click on folder go properties give permission relevant users
after above changes problem solved hope yours too.
Comments
Post a Comment