title: ASP.NET c# Checkboxlist in a template field
body: <p>I a newbie to asp.net. I have created a DB table in sqlserver with 3 columns; <code>ImageID, Filename &amp; Score</code>.</p>
  <p>FileName is <code>C:\pics\beads.png or C:\pics\moreimages\scenary.jpeg</code>.</p>
  <p>Using C# asp.net, I have created a GridView. This Gridview should populate</p>
  <p>(column 1)ImageID and get the image from the Filename (column2) and I have 2 Checkboxlist created as shown below which should accept score for the images from user and save it into the DB table.</p>
  <p><strong>Question 1</strong> .</p>
  <p>I am able to populate Image ID, but Images are not getting populated.</p>
  <p><strong>Question 2.</strong></p>
  <p>I do not know how to access the <code>checkboxlist</code> since it is in template. The checked is in the default select and doesn't change even if I click on it. Which method/property should be used to do save the selection to the database. Here's my code</p>
  <pre><code>&lt;form id=&quot;form1&quot; runat=&quot;server&quot;&gt;
      &lt;p style=&quot;height: 391px&quot;&gt;
          &lt;asp:GridView ID=&quot;GridView1&quot; runat=&quot;server&quot; Caption=&quot;Logos&quot; Height=&quot;299px&quot; Width=&quot;577px&quot; AutoGenerateColumns=&quot;False&quot;&gt;
              &lt;Columns&gt;
                  &lt;asp:BoundField DataField=&quot;ImageID&quot; HeaderText=&quot;ImageID&quot; /&gt;
                  &lt;asp:ImageField DataImageUrlField=&quot;FileName&quot; ControlStyle-Width=&quot;100&quot;
                      ControlStyle-Height=&quot;100&quot; HeaderText=&quot;Image&quot; AlternateText=&quot;No image&quot;&gt;
                      &lt;ControlStyle Height=&quot;100px&quot; Width=&quot;100px&quot;&gt;&lt;/ControlStyle&gt;
                  &lt;/asp:ImageField&gt;
                  &lt;asp:TemplateField HeaderText=&quot;Score&quot; AccessibleHeaderText=&quot;CheckBoxList&quot; ValidateRequestMode=&quot;Enabled&quot;&gt;
                      &lt;ItemTemplate&gt;
                          &lt;asp:CheckBoxList runat=&quot;server&quot; AutoPostBack=&quot;true&quot; RepeatColumns=&quot;3&quot; ID=&quot;test&quot;&gt;
                              &lt;asp:ListItem Selected=&quot;True&quot; Value=&quot;5&quot;&gt;Good&lt;/asp:ListItem&gt;
                              &lt;asp:ListItem Value=&quot;0&quot;&gt;Not Good &lt;/asp:ListItem&gt;
                              &lt;asp:ListItem Value=&quot;3&quot;&gt;OK&lt;/asp:ListItem&gt;
                          &lt;/asp:CheckBoxList&gt;
                      &lt;/ItemTemplate&gt;
                  &lt;/asp:TemplateField&gt;
              &lt;/Columns&gt;
          &lt;/asp:GridView&gt;         
  
          &lt;asp:Button ID=&quot;Button1&quot; runat=&quot;server&quot; OnClick=&quot;Button1_Click&quot; Text=&quot;Save&quot; Width=&quot;130px&quot; /&gt;
          &lt;asp:Button ID=&quot;Button2&quot; runat=&quot;server&quot; OnClientClick=&quot;javaScript:window.close(); return false;&quot; Text=&quot;Exit&quot; Width=&quot;102px&quot; /&gt;
  
          &lt;asp:TextBox ID=&quot;TextBox1&quot; runat=&quot;server&quot;&gt;&lt;/asp:TextBox&gt;
  
      &lt;/p&gt;
  &lt;/form&gt;
  </code></pre>
  
  <hr />
  <pre><code>public string sqlSel = @&quot;SELECT TOP 3 [ImageID],FileName FROM [db1].[ImagesTest] where [Score] is null&quot;;
  
  protected void Page_Load(object sender, EventArgs e)
  {
     
      using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings[&quot;SQLConnectionString&quot;].ConnectionString))
          {
              connection.Open();
              SqlCommand cmdSel = new SqlCommand(sqlSel, connection);
              SqlDataReader reader1 = cmdSel.ExecuteReader();
             
              while (reader1.Read())
              {
                  DataSet ds = GetData(sqlSel);
  
                  if (ds.Tables.Count &gt; 0)
                  {
                      GridView1.DataSource = ds;
                      GridView1.DataBind();              
                  }
                  else
                  {
                      Response.Write(&quot;Unable to connect to the database.&quot;);
                  }
  
              }
              
          }
      
  }
  
  private DataSet GetData(string cmdSel)
  {
     
      String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings[&quot;SQLConnectionString&quot;].ConnectionString;
      DataSet ds = new DataSet();
  
      try
      {
          SqlConnection con = new SqlConnection(strConnString);
          SqlDataAdapter sda = new SqlDataAdapter(cmdSel,con);
          sda.Fill(ds);
          
      }
      catch (Exception ex)
      {
          Response.Write(&quot;IN EXCEPTION &quot;+ex.Message);
          return null;
      }
  
      return ds;
  }
  </code></pre>
  <p>Thanks for ur time
  Rashmi</p>