c# - Input string was not in a correct format in SqlCommand -
here code used , popup exception @ 3rd command.commandtext assignment same way used in 2nd command.commandtext assignment,
sqlcommand command = conn.createcommand(); conn.open(); //1st command.commandtext = query; sqldatareader reader = command.executereader(); arraylist almaingrid = new arraylist(); while (reader.read()) { supporttable table = new supporttable(); table.laysheetno = reader.getvalue(0).tostring(); table.plnlaystarttime = reader.getdatetime(1).tostring(); table.plnlayendtime = reader.getvalue(2).tostring(); table.laytableid = reader.getvalue(3).tostring();// reader.getvalue(3).tostring(); table.layteamid = reader.getvalue(4).tostring(); almaingrid.add(table); } reader.close(); foreach (supporttable table in almaingrid) { //2nd command.commandtext = string.format("select ctdesc cuttable ctid ={0}", int.parse(table.laytableid)); string tabledesc = (string)command.executescalar(); table.layteamid = tabledesc; //3rd-in command.commandtext command.commandtext = string.format("select teamdesc team teamid ={0}", int.parse(table.layteamid)); string teamdesc = (string)command.executescalar(); table.layteamid = teamdesc; } dgvmain.datasource = almaingrid;
when assign table.layteamid in line couple of lines above seeing exception:
table.layteamid = tabledesc;
i expect tabledesc
assigning value table.layteamid
cannot parsed int
, blows when try parse here:
command.commandtext = string.format("select teamdesc team teamid ={0}", int.parse(table.layteamid));
note:
this bad way form queries concatenating strings. leave vulnerable sql injection attacks if aren't careful. use parameterized queries sanitize queries before execute them on database.
Comments
Post a Comment