c# - Error : Object must implement IConvertible -
hi,
i new programming , appriciate if guys me. trying insert listbox items , textbox value each of listbox items database when below error.
if try insert list box items successful when try insert textbox value error. can please tell me doing wrong.
error message: object must implement iconvertible. description: unhandled exception occurred during execution of current web request. please review stack trace more information error , originated in code. exception details: system.invalidcastexception: object must implement iconvertible. source error: line 60: line 61: line 62: cmd.executenonquery(); line 63: line 64:
c# code
using system; using system.data; using system.configuration; using system.collections; using system.collections.specialized; using system.text; using system.web; using system.web.security; using system.web.ui; using system.web.ui.webcontrols; using system.web.ui.webcontrols.webparts; using system.data.sqlclient; using system.web.ui.htmlcontrols; public partial class test1 : system.web.ui.page { protected void page_load(object sender, eventargs e) { } private string getconnectionstring() { return system.configuration.configurationmanager.connectionstrings["rm_jan2011connectionstring"].connectionstring; } private void insertrecords(stringcollection sc) { sqlconnection conn = new sqlconnection(getconnectionstring()); stringbuilder sb = new stringbuilder(string.empty); foreach (string item in sc) { const string sqlstatement = "insert fileid(file_code, dept_code) values(@file_code, @dept_code)"; sb.appendformat("{0}('{1}'); ", sqlstatement, item); } try { conn.open(); sqlcommand cmd = new sqlcommand(sb.tostring(), conn); cmd.parameters.add("@file_code", sqldbtype.varchar); cmd.parameters["@file_code"].value = listbox2.items; cmd.parameters.add("@dept_code", sqldbtype.varchar); cmd.parameters["@dept_code"].value = dropdownlist1.text; cmd.executenonquery(); page.clientscript.registerclientscriptblock(typeof(page), "script", "alert ('records successfuly saved!');", true); } catch (system.data.sqlclient.sqlexception ex) { string msg = "insert error:"; msg += ex.message; throw new exception(msg); } { conn.close(); } } protected void button4_click(object sender, eventargs e) { int myint = convert.toint32(textbox1.text) + 1; (int = 1; < myint; i++) { listbox2.items.add(dropdownlist1.selecteditem.tostring() + i.tostring()); } } protected void button1_click(object sender, eventargs e) { stringcollection sc = new stringcollection(); foreach (listitem item in listbox2.items) { { sc.add(item.text); sc.add(dropdownlist1.text); } } insertrecords(sc); }
i want add values of listbox database.
secondlly if try use .
selecteditem
then following error.
insert error: incorrect syntax near 'cpd1'. incorrect syntax near 'cpd'. incorrect syntax near 'cpd2'. incorrect syntax near 'cpd'. incorrect syntax near 'cpd3'. incorrect syntax near 'cpd'.
any idea going wrong?
if listbox contains string items need change following line
cmd.parameters["@file_code"].value = listbox2.items
to
cmd.parameters["@file_code"].value = listbox2.selecteditem
if items complex objects add .tostring() @ end
upd: if want add listbox items need loop through item collection , execute insert query foreach item, this:
foreach(string item in listbox2.items) { sqlcommand cmd = new sqlcommand(sqlstatement, conn); cmd.parameters.add("@file_code", sqldbtype.varchar); cmd.parameters["@file_code"].value = item; cmd.parameters.add("@dept_code", sqldbtype.varchar); cmd.parameters["@dept_code"].value = dropdownlist1.text; cmd.executenonquery(); }
Comments
Post a Comment