c# - How to retrive value of listbox.valuemember property -
i beginner in c# programming. have filter result in mode:
listamicizie1.displaymember = "viewname"; // it's alias listamicizie1.valuemember = "idutente"; //it's primary key in db table listamicizie1.datasource = ds.tables["utenti"]; //db table
now must register in db table value of result of listbox , use code
string idu = string.empty; string value = string.empty; foreach (var item in listamicizie1.valuemember) { (int j = 0; j < listamicizie1.items.count; j++) { idu = listamicizie1.selectedvalue; value += idu + ","; } } value = value.trimend(','); value = value.trimend(','); idutente = user.text; string cnnstr = system.configuration.configurationsettings.appsettings["cnnstr"].tostring(); string querysql2 = " insert amicizia (idutente1, [idamici]) values ('" + idutente + "', '" + value + "')"; sqlconnection myconn = new sqlconnection(cnnstr); sqlcommand mycmd = new sqlcommand(querysql2, myconn); try { myconn.open(); mycmd.executenonquery(); } catch (exception ex) { messagebox.show(ex.tostring()); } { myconn.close(); } } this.close(); }
the program running, when open second db table (amicizia), value in idamici isn't primary key ( idutente in first table),but i,d,u,t,e,n,t,e,.
how retrive original value of idutente? thanks
(sorry if write whit errors, i'm not english :) )
do not use value.trimend(',');
use value.replace(',', '');
also practice use parameters
instead of concatenated string.
string idu = string.empty; string value = string.empty; foreach (var item in listamicizie1.valuemember) { (int j = 0; j < listamicizie1.items.count; j++) { idu = listamicizie1.selectedvalue; value += idu + ","; } } //value = value.trimend(','); //value = value.trimend(','); value = value.replace(',', ''); value = value.replace(' ', ''); //add line if not want space. idutente = user.text; string cnnstr = system.configuration.configurationsettings.appsettings["cnnstr"].tostring(); string querysql2 = " insert amicizia (idutente1, idamici) values ('" + @idutente, @value)"; sqlconnection myconn = new sqlconnection(cnnstr); sqlcommand mycmd = new sqlcommand(querysql2, myconn); try { myconn.open(); mycmd.parameters.addwithvalue("@idutente", idutente); mycmd.parameters.addwithvalue("@value", value); mycmd.executenonquery(); } catch (exception ex) { messagebox.show(ex.tostring()); } { myconn.close(); } } this.close(); }
added:
you can change code value += idu + ",";
when generating value
, see code
foreach (var item in listamicizie1.valuemember) { (int j = 0; j < listamicizie1.items.count; j++) { idu = listamicizie1.selectedvalue; value += idu.trim(idu); } } //value = value.trimend(','); //value = value.trimend(',');
now no need call replace
adding items list box **
try code.
string cnnstr = system.configuration.configurationsettings.appsettings["cnnstr"].tostring(); string querysql2 = " insert amicizia (idutente1, idamici) values ('" + @idutente, @value)"; sqlconnection myconn = new sqlconnection(cnnstr); sqlcommand mycmd = new sqlcommand(querysql2, myconn); myconn.open(); (int i=0; < listamicizie1.items.count; i++) { var myitem = listamicizie1.items[i]; var dataarray = ( myitem datarowview).row.itemarray; //messagebox.show(dataarray[0].tostring()); idutente = user.text; //i don't know storing here. value = dataarray[0].tostring(); //dataarray[0] should have key try { mycmd.parameters.addwithvalue("@idutente", idutente); mycmd.parameters.addwithvalue("@value", value); mycmd.executenonquery(); mycmd.parameters.clear(); } catch (exception ex) { messagebox.show(ex.tostring()); return; } { myconn.close(); } }
Comments
Post a Comment