C# NULL VALUES BEING INSERTED IN DB -
i reading xml file , saving extracted data according elements of xml file. using xmlreader. when running program ... runs fine but, null values being sent db. codes follows :
using system; using system.xml; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.data.sqlclient; using system.configuration; using system.data; public partial class _default : system.web.ui.page { string org_id; string org_desig; string org_name; string add_1; string add_2; string add_3; string cityname; string countrycode; string countryname; string postalcode; protected void page_load(object sender, eventargs e) { } protected void button1_click(object sender, eventargs e) { // load xml file xmltextreader reader = new xmltextreader("pxmlf-8612013050420130606105906.xml"); // loop on xml file while (reader.read()) { // element if (reader.nodetype == xmlnodetype.element) { // if element 1 required if (reader.name == "organizationid") { org_id = (reader.readelementstring()); } if (reader.name == "organisationdesignator") { org_desig = (reader.readelementstring()); } if (reader.name == "organizationname1") { org_name = (reader.readelementstring()); } if (reader.name == "addressline1") { add_1 = (reader.readelementstring()); } if (reader.name == "addressline2") { add_2 = (reader.readelementstring()); } if (reader.name == "addressline3") { add_3 = (reader.readelementstring()); } if (reader.name == "cityname") { cityname = (reader.readelementstring()); } if (reader.name == "countrycode") { countrycode = (reader.readelementstring()); } if (reader.name == "countryname") { countryname = (reader.readelementstring()); } if (reader.name == "postalcode") { postalcode = (reader.readelementstring()); } //populate table data extracted values //connect db string connstr = configurationmanager.connectionstrings["connxml"].connectionstring; sqlconnection con = new sqlconnection(connstr); con.open(); //send extracted data db sqlcommand cmd = new sqlcommand(); cmd.commandtext = "insert inv_header values ('" + org_id + "','" + org_desig + "', '" + org_name + "' , '" + add_1 + "', '" + add_2 + "', '" + add_3 + "', '" + cityname + "', '" + countrycode + "', '" + countryname + "', '" + postalcode + "')"; cmd.commandtype = commandtype.text; cmd.connection = con; cmd.executenonquery(); con.close(); reader.close(); } }
it seems inserting values after every reader.read(). try insert after while loop.
using system; using system.xml; using system.collections.generic; using system.linq; using system.web; using system.web.ui; using system.web.ui.webcontrols; using system.data.sqlclient; using system.configuration; using system.data; public partial class _default : system.web.ui.page { string org_id; string org_desig; string org_name; string add_1; string add_2; string add_3; string cityname; string countrycode; string countryname; string postalcode; protected void page_load(object sender, eventargs e) { } protected void button1_click(object sender, eventargs e) { // load xml file xmltextreader reader = new xmltextreader("pxmlf-8612013050420130606105906.xml"); // loop on xml file while (reader.read()) { // element if (reader.nodetype == xmlnodetype.element) { // if element 1 required switch (reader.name) { case "organizationid": org_id = (reader.readelementstring()); break; case "organisationdesignator": org_desig = (reader.readelementstring()); break; case "organizationname1": org_name = (reader.readelementstring()); break; case "addressline1": add_1 = (reader.readelementstring()); break; case "addressline2": add_2 = (reader.readelementstring()); break; case "addressline3": add_3 = (reader.readelementstring()); break; case "cityname": cityname = (reader.readelementstring()); break; case "countrycode": countrycode = (reader.readelementstring()); break; case "countryname": countryname = (reader.readelementstring()); break; case "postalcode": postalcode = (reader.readelementstring()); break; } } } //connect db string connstr = configurationmanager.connectionstrings["connxml"].connectionstring; sqlconnection con = new sqlconnection(connstr); con.open(); //send extracted data db sqlcommand cmd = new sqlcommand(); cmd.commandtext = "insert inv_header values ('" + org_id + "','" + org_desig + "', '" + org_name + "' , '" + add_1 + "', '" + add_2 + "', '" + add_3 + "', '" + cityname + "', '" + countrycode + "', '" + countryname + "', '" + postalcode + "')"; cmd.commandtype = commandtype.text; cmd.connection = con; cmd.executenonquery(); con.close(); reader.close(); } }
Comments
Post a Comment