c# - XML File Error Error : System.InvalidCastException: Specified cast is not valid -
i having problem code`using system;
namespace cm18b { public partial class form1 : form { public form1() { initializecomponent(); } private void form1_load(object sender, eventargs e) { system.threading.thread.sleep(40000); configuration config = configurationmanager.openexeconfiguration(application.executablepath); string machineid = config.appsettings.settings["machineid"].value; //messagebox.show(machineid); double time = convert.todouble(config.appsettings.settings["time"].value); system.timers.timer timer = new system.timers.timer(time); timer.elapsed += ontimer; timer.enabled = true; this.windowstate = formwindowstate.minimized; } private void ontimer(object source, elapsedeventargs e) { var timer = (system.timers.timer)source; timer.stop(); //messagebox.show("trying"); buildtransactionxml(); timer.start(); } public bool sendftp(string file_name) { configuration config = configurationmanager.openexeconfiguration(application.executablepath); string source = config.appsettings.settings["sourcelocation"].value + "/" + file_name; //change new file name string ftpusername = config.appsettings.settings["ftpusername"].value; string ftppassword = config.appsettings.settings["ftppassword"].value; string ftpfullpath = config.appsettings.settings["ftpurl"].value + "/" + file_name; try { string filename = path.getfilename(source); //string ftpfullpath = ftpurl; ftpwebrequest ftp = (ftpwebrequest)ftpwebrequest.create(ftpfullpath); ftp.credentials = new networkcredential(ftpusername, ftppassword); ftp.keepalive = true; ftp.usebinary = true; ftp.method = webrequestmethods.ftp.uploadfile; filestream fs = file.openread(source); byte[] buffer = new byte[fs.length]; fs.read(buffer, 0, buffer.length); fs.close(); stream ftpstream = ftp.getrequeststream(); ftpstream.write(buffer, 0, buffer.length); ftpstream.close(); return true; } catch (webexception ex) { writelog(ex); return false; } } public void buildtransactionxml() { string xmlstring = ""; configuration config = configurationmanager.openexeconfiguration(application.executablepath); string transactioncounter = config.appsettings.settings["transactioncounter"].value; string bustaa = config.appsettings.settings["bagnumber"].value; string cm18string = configurationmanager.connectionstrings["cm18string"].connectionstring; sqlconnection con = null; try { con = new sqlconnection(cm18string); if (con.state != system.data.connectionstate.open) { con.open(); } string currency = ""; string device = ""; string result = ""; string symbol = ""; string type = ""; string type1 = ""; string file_name = ""; string user = ""; string filesaved = ""; string date = ""; string time = ""; string cassette = ""; sqlcommand cmd = new sqlcommand("select [idp],[datetimeoperazione],[importo],[tipooperazione],[banconote_e500],[banconote_e200],[banconote_e100],[banconote_e50],[banconote_e20],[banconote_e10],[banconote_e5],[bustaa] [databos].[dbo].[operazioni] join [databos].[dbo].[buste] on [databos].[dbo].[operazioni].sessioneref = [databos].[dbo].[buste].sessioneref tipooperazione in ('161','3', '1') , idp > @idp", con); cmd.parameters.add("@idp", transactioncounter); sqldatareader rd = cmd.executereader(); while (rd.read()) { string filecounter = config.appsettings.settings["filecounter"].value; switch (rd.getint32(3)) { case 161: type = "out"; type1 = "out"; break; case 1: type = "outb"; type1 = "outb"; break; case 3: type = "in"; type1 = "in"; config.appsettings.settings["bagnumber"].value = rd.getstring(11); config.save(configurationsavemode.modified); bustaa= rd.getstring(11); break; default: type = ""; break; } cassette = bustaa; currency = "euro"; symbol = "€"; date = rd.getdatetime(1).tostring("m/dd/yyyy"); time = rd.getdatetime(1).tostring(); device = config.appsettings.settings["machineid"].value; file_name = device + "_" + type1 + ".xml"; xmlstring = ""; xmlstring = "<transaction><device>" + device + "</device><status>" + @type1 + "</status><currency><description>" + currency + "</description><symbol>" + symbol + "</symbol></currency><date_time><date>" + date + "</date><time>" + time + "</time></date_time><cassette>" + cassette + "</cassette><<filename>" + file_name + "</filename>"; file_name = device + "_" + type1 + "_" + filecounter + ".xml"; if (type == "in") { config.appsettings.settings["hasmoney"].value = "true"; int ttype = 0; int value = 0; int quantity = 0; (int =1 ; i<=7;i++) { ttype = 0; value = 0; quantity = 0; switch(i) { case 1: ttype = 500; break; case 2: ttype = 200; break; case 3: ttype = 100; break; case 4: ttype = 50; break; case 5: ttype = 20; break; case 6: ttype = 10; break; case 7: ttype = 5; break; } quantity = rd.getint32(i+4); value = quantity * ttype; if(quantity !=0) { xmlstring = xmlstring + "<transactionline type = \"" + ttype.tostring("##########0.00").replace(',', '.') + "\"><quantity>" + quantity.tostring() + "</quantity><value>" + value.tostring("##########0.00").replace(',', '.') + "</value></transactionline>"; } } } else if ((type == "out") || ((config.appsettings.settings["hasmoney"].value == "true") && (type == "outb"))) { type= "out"; config.appsettings.settings["hasmoney"].value = "false"; sqlconnection con1 = new sqlconnection(cm18string); if (con1.state != system.data.connectionstate.open) { con1.open(); } sqlcommand cmd1 = new sqlcommand("select sum([banconote_e500]),sum([banconote_e200]),sum([banconote_e100]),sum([banconote_e50]),sum([banconote_e20]),sum([banconote_e10]),sum([banconote_e5])),[bustaa] [databos].[dbo].[operazioni] join [databos].[dbo].[buste] on [databos].[dbo].[operazioni].sessioneref = [databos].[dbo].[buste].sessioneref tipooperazione in ('161','3', '1') , bustaa = @bag group bustaa", con1); cmd1.parameters.add("@bag",cassette); sqldatareader rd1 = cmd1.executereader(); rd1.read(); long ttype = 0; long value = 0; long quantity = 0; (int =1 ; i<=7;i++) { ttype = 0; value = 0; quantity = 0; switch(i) { case 1: ttype = 5; break; case 2: ttype = 10; break; case 3: ttype = 20; break; case 4: ttype = 50; break; case 5: ttype = 100; break; case 6: ttype = 200; break; case 7: ttype = 500; break; quantity = rd1.getint32(i - 1); value = quantity * ttype; if(quantity !=0) { // xmlstring = xmlstring + "<transactionline type = \"" + ttype.tostring("##########0.00").replace(',', '.') + "\"><quantity>" + quantity.tostring() + "</quantity><value>" + value.tostring("##########0.00").replace(',', '.') + "</value></transactionline>"; //enable csextra xmlstring = xmlstring + "<transactionline type = \"" + ttype.tostring("##########0.00") + "\"><quantity>" + quantity.tostring() + "</quantity><value>" + value.tostring("##########0.00") + "</value></transactionline>"; } } rd1.close(); con1.close(); } //else //{ //} xmlstring = xmlstring + "</transaction>"; string fout = config.appsettings.settings["destinationlocation"].value; system.io.streamwriter file = new system.io.streamwriter(fout + "\\"+ file_name); file.writeline(xmlstring); file.close(); if (sendftp(file_name)) { config.appsettings.settings["transactioncounter"].value = rd.getint32(0).tostring(); transactioncounter = rd.getint32(0).tostring(); config.save(configurationsavemode.modified); config.appsettings.settings["filecounter"].value = convert.toint32(config.appsettings.settings["filecounter"].value) + 1 + ""; filecounter = config.appsettings.settings["filecounter"].value; config.save(configurationsavemode.modified); writelog(file_name); } else { break; } } rd.close(); con.close(); }} catch (exception e) { writelog(e); }{ }} public void writelog(string message) { try { configuration config = configurationmanager.openexeconfiguration(application.executablepath); string logfilelocation = config.appsettings.settings["loglocation"].value; system.io.streamwriter file1 = new system.io.streamwriter(logfilelocation + "\\log.txt", true); file1.writeline(); file1.writeline("successfully completed"); file1.writeline(message); file1.writeline("=================================================================================================="); file1.writeline("=================================================================================================="); file1.writeline(); file1.close(); } catch { configuration config = configurationmanager.openexeconfiguration(application.executablepath); messagebox.show("please create folder: " + config.appsettings.settings["loglocation"].value); } } public void writelog(webexception ex) { try { configuration config = configurationmanager.openexeconfiguration(application.executablepath); string logfilelocation = config.appsettings.settings["loglocation"].value; system.io.streamwriter file1 = new system.io.streamwriter(logfilelocation + "\\log.txt", true); file1.writeline(); file1.writeline("ftp error"); file1.writeline("error : " + ex.tostring()); file1.writeline("ftp response : " + ((ftpwebresponse)ex.response).statusdescription); file1.writeline("filecounter : " + config.appsettings.settings["filecounter"].value); file1.writeline("=================================================================================================="); file1.writeline("=================================================================================================="); file1.writeline(); file1.close(); } catch { configuration config = configurationmanager.openexeconfiguration(application.executablepath); messagebox.show("please create folder: " + config.appsettings.settings["loglocation"].value); } } public void writelog(exception ex) { try { configuration config = configurationmanager.openexeconfiguration(application.executablepath); string logfilelocation = config.appsettings.settings["loglocation"].value; system.io.streamwriter file1 = new system.io.streamwriter(logfilelocation + "\\log.txt", true); file1.writeline(); file1.writeline("xml file error"); file1.writeline("error : " + ex.tostring()); file1.writeline("filecounter : " + config.appsettings.settings["filecounter"].value); file1.writeline("=================================================================================================="); file1.writeline("=================================================================================================="); file1.writeline(); file1.close(); } catch { configuration config = configurationmanager.openexeconfiguration(application.executablepath); messagebox.show("please create folder: " + config.appsettings.settings["loglocation"].value); } } } }
it giving me error @ line quantity = rd.getint32(i+4)
i know value int 32 database why m getting error.?
`
it because i+4
isn't int32
datatype. trying printing i+4
before , check column type matches in position , fix code accordingly.
Comments
Post a Comment