c# - How do I...Count the no of records inserted. -
i'm getting values form excel in datatable i'm updating/inserting record in database.
every thing working fine need once finishing records should display message user ..no of records inserted.
and while inserting time throwing error
string not recognized valid datetime.
eg: 20 records inserted.
here code:
private void import_to_grid(string filepath, string extension, string ishdr) { string strconnstring = configurationmanager.connectionstrings["cargonetconnectionstring"].connectionstring; //file upload path string folderpath = server.mappath(configurationmanager.appsettings["folderpath"]); //file name string filename = lblfilename.text; //create connection string excel work book string constr = ""; switch (extension) { case ".xls": //excel 97-03 constr = "provider=microsoft.jet.oledb.4.0;data source=" + folderpath + filename + ";extended properties=\"excel 8.0;hdr=yes;imex=2\""; break; case ".xlsx": //excel 07 constr = "provider=microsoft.ace.oledb.12.0;data source=" + folderpath + filename + ";extended properties=\"excel 12.0 xml;hdr=yes\""; break; } constr = string.format(constr, filepath, ishdr); oledbconnection connexcel = new oledbconnection(constr); oledbcommand cmdexcel = new oledbcommand(); oledbdataadapter oda = new oledbdataadapter(); datatable dt = new datatable(); cmdexcel.connection = connexcel; //get name of first sheet connexcel.open(); datatable dtexcelschema; dtexcelschema = connexcel.getoledbschematable(oledbschemaguid.tables, null); string sheetname = ddlsheets.selectedvalue.tostring(); connexcel.close(); //read data first sheet connexcel.open(); cmdexcel.commandtext = "select * [" + sheetname + "]"; oda.selectcommand = cmdexcel; oda.fill(dt); connexcel.close(); //bind database int count=0; using (lqtransagentseafreightratedatacontext db = new lqtransagentseafreightratedatacontext()) { foreach (datarow r in dt.rows) { var newsfr = new tb_transagentseafreightrate_2 { pod = r["pol"].tostring(), pol = r["pod"].tostring(), forwarder = r["forwarder"].tostring(), forwarderreference = r["forwarder reference"].tostring(), shippingline = r["shipping line"].tostring(), containertype = r["container type"].tostring(), containersize = r["container size"].tostring(), validfrom = convert.todatetime(r["validity from"].tostring()), validto = convert.todatetime(r["valitity to"].tostring()), basicrate = convert.todecimal(r["basic rate"]), paf = convert.todecimal(r["paf "]), caf = convert.todecimal(r["caf"]), pss = convert.todecimal(r["pss"]), totalamount = convert.todecimal(r["total amount"]), freedays = convert.todecimal(r["free days"]), creditdays = r["credit days"].tostring(), nitdeposit = r["nit deposit"].tostring(), tasf_nuisactive = 1, tasf_mcmp_nuuniqueid = mobjgenlib.convertlong(txtcompanyid.text) }; db.tb_transagentseafreightrate_2s.insertonsubmit(newsfr); db.submitchanges(); count = count + 1; } } //scriptmanager.registerstartupscript(this, up.gettype(), "alert", "alert('saved successfully');", true); //bind data gridview dg_agentsfr.caption = path.getfilename(filepath); dg_agentsfr.datasource = dt; dg_agentsfr.databind(); //savedatafromgv(); }
please me. in advance.
dt.rows.count() tell how many rows available in dataset..it helpful determine total no of records inserted
Comments
Post a Comment