c# - Re-populating checkboxlist in Edit Mode from SQL comma-separated string is not selecting checkboxes -


i not able pre-populate checkboxlist3 in app. have retrieved string data database , passed array routine fails with:

nullreferenceexception:object not set instance of object error @ line: listitem currentcheckbox = cb3.items.findbyvalue(items[i].tostring()); 

readmystring() method uses sqldatareader read , split string column sql table pass values example: "a, b, c" string array identify of 6 checkboxes should selected. code reference: [http://mikesdotnetting.com/article/53/saving-a-user's-checkboxlist-selection-and-re-populating-the-checkboxlist-from-saved-data]

html code:

<asp:gridview id="gridview1" runat="server"                autogeneratecolumns="false" cellpadding="4" datakeynames="myid"                  datasourceid="sdsmytable1" forecolor="#333333" gridlines="none"                onrowdeleted="gridview1_rowdeleted"                onrowdeleting="gridview1_rowdeleting"                onrowupdated="gridview1_rowupdated"                 onrowupdating="gridview1_rowupdating"                onselectedindexchanged="gridview1_selectedindexchanged"                onrowediting="gridview1_rowediting"                onrowcancelingedit="gridview1_rowcancelingedit"                onrowdatabound="gridview1_rowdatabound">     <rowstyle backcolor="#eff3fb" />      <columns>         <asp:commandfield showdeletebutton="true" showeditbutton="true" />          <asp:boundfield datafield="myid" headertext="myid" readonly="true"                          sortexpression="myid" />          <asp:boundfield datafield="date1" headertext="date driver lic issued"                          sortexpression="date1" />          <asp:templatefield headertext="chooseone" sortexpression="chooseone">             <itemtemplate>                 <asp:label id="label1" runat="server" text='<%# bind("chooseone") %>'></asp:label>             </itemtemplate>             <edititemtemplate>                                 <asp:radiobuttonlist id="radiobuttonlist2" runat="server" selectedvalue='<%# bind("chooseone") %>' font-size="small"                                       repeatdirection="horizontal">                     <asp:listitem value="1">1</asp:listitem>                     <asp:listitem value="2">2</asp:listitem>                     <asp:listitem value="3">3</asp:listitem>                 </asp:radiobuttonlist>             </edititemtemplate>          </asp:templatefield>         <asp:templatefield headertext="mycommaseparatedstring" sortexpression="mycommaseparatedstring">             <itemtemplate>                 <asp:label id="label2" runat="server" text='<%# bind("mycommaseparatedstring") %>'></asp:label>             </itemtemplate>             <edititemtemplate>                 <asp:textbox id="textbox2" runat="server" text='<%# bind("mycommaseparatedstring") %>'></asp:textbox>                 <asp:checkboxlist id="checkboxlist3" runat="server" font-size="small"                                     repeatdirection="horizontal">                                                   <asp:listitem value="a">a</asp:listitem>                     <asp:listitem value="b">b</asp:listitem>                     <asp:listitem value="c">c</asp:listitem>                                        <asp:listitem value="d">d</asp:listitem>                     <asp:listitem value="e">e</asp:listitem>                     <asp:listitem value="f">f</asp:listitem>                            </asp:checkboxlist>             </edititemtemplate> 

c# code:

    protected void gridview1_rowdatabound(object sender, gridviewroweventargs e)         {             //// establish datarow dig minutia             datarowview drv = e.row.dataitem datarowview;              // if 1: rowtype of gridview control datarow              if (e.row.rowtype == datacontrolrowtype.datarow)             {                 //if 2: datarow in edit mode                 if ((e.row.rowstate & datacontrolrowstate.edit) > 0)                 {                   radiobuttonlist rb2 = (radiobuttonlist)e.row.findcontrol("radiobuttonlist2");                   checkboxlist cb3 = (checkboxlist)e.row.findcontrol("checkboxlist3"); //checkboxlist cb3 = (checkboxlist)gridview1.rows[gridview1.editindex].findcontrol("checkboxlist3");                   checkboxlist cb4 = (checkboxlist)e.row.findcontrol("checkboxlist4");                      // create instance of sqlconn class                                 sqlconnclass getmystr= new sqlconnclass();                      //return datareader read endorsements column database                     sqldatareader rdr = getmystr.selectmystring(getmyid());                      try                     {                         // column                         if (rb2 != null)                         {                             rb2.selectedvalue = drv[2].tostring();                         }                          // column                                             //checkboxlist cb3 = (checkboxlist)e.row.findcontrol("checkboxlist3");                         if (cb3 != null)//if (cb3 contains 6 checkboxes (i, n, h, x, t, k)                         {                             //bind checkbox manually                             cb3.datasource = sdsdatasource;                             cb3.datatextfield = "mycommaseparatedstring ";                             cb3.datavaluefield = "mycommaseparatedstring ";                             cb3.databind();                              // if row exists                             if (rdr.hasrows)//(dt.rows.count > 0 && dt != null)                             {                                 //start reading                                 rdr.read();                  //extract comma separated string datareader one-dimensional array                                     string[] items = rdr.getstring(0).split(',');                 //returns upper bound indexes of first dimension of array                                 (int = 0; <= items.getupperbound(0); i++)                                 {               // currentcheckbox null occurs:                listitem currentcheckbox = cb3.items.findbyvalue(items[i].tostring());                                      if (currentcheckbox != null)                                     {                                         currentcheckbox.selected = true;                                     }                                     //cb3.selectedvalue = drv[3].tostring();                                     //}                                 }// end of                                  //close sqldatareader                                 rdr.close();                             }                          // column                         //checkboxlist cb4 = (checkboxlist)e.row.findcontrol("checkboxlist4");                         if (cb4 != null)                         {                             cb4.selectedvalue = drv[4].tostring();                         }                          }// end of if                     }// end of try                      catch (indexoutofrangeexception ex2)                     {     system.diagnostics.debug.writeline(ex2.message + "; " + ex2.source + "; " + ex2.targetsite);                     }//end of catch                                 }// end of if (rowstate)             } // end of if (rowtype)         }// end of method 

what have tried: calling readmystring() method gridview1_rowdatabound event and/or gridview1_rowediting event.

since checkboxlist3 empty begin , seems not want null, added line cb3.items[i].value = items[i]; error moves new line instead. must missing something.

can see i've errored? must putting code in wrong methods...?

you trying find control in gridview. controls inside gridview rows. need this:

checkboxlist cb3 = (checkboxlist) gridview1.rows[gridview1.editindex].findcontrol("checkboxlist3"); 

trim items[i] below:

listitem currentcheckbox = cb3.items.findbyvalue(items[i].trim()); 

Comments

Popular posts from this blog

python - Subclassed QStyledItemDelegate ignores Stylesheet -

java - HttpClient 3.1 Connection pooling vs HttpClient 4.3.2 -

SQL: Divide the sum of values in one table with the count of rows in another -