Silverlight combobox insert data into sql table


i want bind combobox data sql server table. in table id , name. when combobox success binding want to, in application choose combobox , button "ok" insert sql server table value int (i'm using sql procedure).


public observablecollection<comboboxpw> readcomboboxpw(int id_pw) { observablecollection<comboboxpw> result = new observablecollection<comboboxpw>(); string nwconn = system.configuration.configurationmanager.connectionstrings["myconnectionstring"].connectionstring; sqldatareader dr; sqlconnection conn = new sqlconnection(nwconn); try { sqlcommand cmd = new sqlcommand(); cmd.commandtype = commandtype.storedprocedure; cmd.connection = conn; cmd.commandtext = "combobox_pw"; cmd.parameters.addwithvalue("id_pw", id_pw); conn.open(); dr = cmd.executereader(); while (dr.read()) { comboboxpw lista = new comboboxpw(); lista.pw = dr.getstring(0); lista.idpw = dr.getstring(0); result.add(lista); } dr.close(); return result; } catch (sqlexception e) { comboboxpw lista = new comboboxpw(); lista.pw = e.message; result.add(lista); return result; } finally { conn.close(); }; }


my class:

public class comboboxpw      {          public string pw { get; set; }          public string idpw { get; set; }        }

xaml:

<combobox x:name="comboboxpw" horizontalalignment="left" margin="40,117,0,0" verticalalignment="top" width="366"                    selecteditem="{binding readcomboboxpw}" displaymemberpath="pw"  selectedvaluepath="idpw" selectionchanged="comboboxpw_selectionchanged"  />

button ok:

 private void okbutton_click(object sender, routedeventargs e)          {                this.dialogresult = true;                servicereference1.service1client webservice = new servicereference1.service1client();  webservice.insertpwasync(comboboxpw.selectedvalue); 

above error (selectedvalue):

"gets or sets value of selected item, obtained using system.windows.controls.primitives.selector.selectedvaluepath."


procedure in sql:

procedure [dbo].[combobox_pw] @id_pw int begin set nocount on; select nazwa_pw, id_pw [dbo].[dostepne_pw] where (id_pw = @id_pw or @id_pw = 0) end

insert sql table:

public int insertpw(int pw)
        {
            string nwconn = system.configuration.configurationmanager.connectionstrings["myconnectionstring"].connectionstring;
            sqlconnection conn = new sqlconnection(nwconn);

            try
            {
                sqlcommand cmd = new sqlcommand();
                cmd.commandtype = commandtype.storedprocedure;
                cmd.connection = conn;
                cmd.commandtext = "zapisz_pw";
                cmd.parameters.addwithvalue("pw", pw);
                conn.open();
                int result = cmd.executenonquery();
                conn.close();
                if (result > 0)
                {
                    return result;
                }
                else
                {
                    return 0;
                }
            }
            catch (sqlexception e)
            {
                int result = 0;
                return result;
            }
            finally
            {
                conn.close();
            };

silverlight runs in browser sandbox unless it's full trust out of browser.

you can't read data out database browser.

and there isn't simple "do 1 line" fits forum post.

you want service wcf ria subject in itself.

brian noyes did set of articles on silverlight you'll need use wayback time machine find them.

here's last of series:

https://web.archive.org/web/20140119185226/http://www.silverlightshow.net/items/wcf-ria-services-part-5-metadata-and-shared-classes.aspx

a more basic article:

http://www.c-sharpcorner.com/uploadfile/mahadesh/silverlight-5-wcf-ria-services-part-1/

 


hope helps.

technet articles: wpf: layout lab; technet articles



Silverlight  >  WCF RIA Services with Silverlight



Comments

Popular posts from this blog

Conditional formatting a graph vertical axis in SSRS 2012 charts

Register with Power BI failed

SQL server replication error Cannot find the dbo or user defined function........