c# - checkListBox selectedindex and sql mapping -


i have combo box , checklistbox in windows form application. in combo box, mapped items sql table using query , displayed combo box

select * job_table 

sql table

job_detail

    --------------------     jobid   |   jobname     -------------------     1       |   mcdonald                2       |   buger king     3       |   security officer     4       |   manager     5       |   teacher 

code:

 string query = "select jobid, jobname job_detail";                 sqldataadapter da = new sqldataadapter();                 myconn.open();                 datatable dt = new datatable();                  sqlcommand command = new sqlcommand(query, myconn);                  sqldatareader reader = command.executereader();                  dt.load(reader);                  datarow dr;                 dr= dt.newrow();                 dr.itemarray = new object[] { 0, "<----------select job -------> " };                 dt.rows.insertat(dr, 0);                  applicationcombobox.datasource = dt;                 applicationcombobox.valuemember = "jobid";                 applicationcombobox.displaymember = "jobname"; 

after combobox binding, call method call fill_workerchecklist display list of worker on job.

    -----------------------------     workerid   |   workername   |                  -----------------------------     1          |   amy          |     2          |   jim          |     3          |   peter        |     4          |   pam          |     5          |   david        |     6          |   sara         |     7          |   laura        |     8          |   bob          | 

i joined 2 tables relationship: 1-n: 1 worker can have many job using join query:

select * worker_detail wd         left join job_detail jd         on wd.jobid = jd.jobid       -----------------------------------------     workerid   |   workername   |  jobid                ------------------------------------------     1          |   amy          |   1     2          |   jim          |   1     3          |   peter        |   2     4          |   pam          |   5     5          |   david        |   3     6          |   sara         |   2     7          |   laura        |   4     8          |   bob          |   4 

to display workername checklistbox, thinking filter combobox.selectedindex, not idea because happen if 1 job cut, record messed up

this query thought of using sql injection

select * worker_detail wd         left join job_detail jd         on wd.jobid = jd.jobid wd.jobid = '"+combobox.selectedindex+"' 

my original code:

            sqlcommand mycommand = new sqlcommand(query, myconn);              sqldatareader dr = mycommand.executereader();              combobox.items.clear();              string s = "select all";             combobox.items.add(s);                while (dr.read())             {                 string workername = dr.isdbnull(1) ? string.empty : dr.getstring(1);                 checklistbox.items.add(name);             } 

you can see problem here. if job somehow cut? jobid gone , combobox index won't work. want know is there better way map workerid instead of index?

to understand table flow, may check sqlfiddle. understand primary , transaction table.

and main problem don't understand different between selectedindex , selectedvalue property of control. can read whole difference here, reference let me add important content answer.

selectedindex

selectedindex property returns index of selected item in control. index starts 0. first item in control has 0 index , second item has 1 index , on.

selectedvalue

well, property people confused most. of them thinks selectedvalue property returns text of item have selected. yes returns text when have not specified value separately. value in these controls not shown user. let's take example, adding items listbox1 dynamically like-

listbox1.items.add("iknowledgeboy"); 

when text , value of item becomes "iknowledgeboy". so, when say-

listbox1.selectvalue;

then "iknowledgeboy" sure. when binding listbox1 database need specify datatextfield , datavaluefield of listbox1. @ time, value of each item in listbox1 or dropdown or checkboxlist or radiobuttonlist integer. so, if say-

listbox1.selectedvalue; 

then won't text seeing rather integer value.

hope you..


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -