title: asp.net DataList select DropDownList value
body: <p>I am making an application about article publication. I have 2 tables 
  "artikulli"</p>
  
  <pre><code>id                   int    Unchecked
  tema                 varchar(250)   Checked
  abstrakti            text   
  data_publikimit      date   
  path                 varchar(350)  
  keywords             varchar(350)  
  kategoria_id         int   
  departamenti_id      int   
  </code></pre>
  
  <p>"kategorite"</p>
  
  <pre><code>id          int Unchecked
  emertimi    varchar(350)    
  </code></pre>
  
  <p>I want to display in a dropdown list all values of field "emertimi" and when the user selects one value to save id of that value in table "artikulli".
  I have done the following, but I have the problem with syntax because I am using DATALIST.</p>
  
  <pre><code>public partial class AddArticle : System.Web.UI.Page
  {
      string connection = System.Configuration.ConfigurationManager.ConnectionStrings["ConnectionStringDatabase"].ConnectionString;
      protected void Page_Load(object sender, EventArgs e)
      {
          Page.Form.Attributes.Add("enctype", "multipart/form-data");
          try
          {
  
              if (!IsPostBack)
              {
  
                  Bind();
              }
  
          }
          catch (Exception ex)
          {
              Response.Write("Error:" + ex.ToString());
  
          }
      }
  
      public void Bind()
      {
          SqlConnection con = new SqlConnection(connection)
          string Qry = "select * from kategoria";
          SqlDataAdapter da = new SqlDataAdapter(Qry, con);
          DataSet ds = new DataSet();
          DropDownList drpdKategoria = e.Item.FindControl("drpdKategoria") as DropDownList;
          con.Open();
          da.Fill(ds);
          drpdKategoria.DataSource = ds;
          drpdKategoria.DataValueField = "id";  // Value of bided list in your dropdown in your case it will be CATEGORY_ID
          drpdKategoria.DataTextField = "emertimi"; // this will show Category name in your dropdown
          drpdKategoria.DataBind();
          con.Close();
          con.Dispose();
          ds.Dispose();
          da.Dispose();  
      }
  
      protected void datalist2_ItemCommand(object source, DataListCommandEventArgs e)
      {
          if (e.CommandName.Equals("Insert"))
          {
              TextBox txtTema = e.Item.FindControl("txtTema") as TextBox;
              TextBox txtAbstrakti = e.Item.FindControl("txtAbstrakti") as TextBox;
              TextBox txtData = e.Item.FindControl("txtData") as TextBox;
              TextBox txtKeywords = e.Item.FindControl("txtKeywords") as TextBox;
              DropDownList drpdKategoria = e.Item.FindControl("drpdKategoria") as DropDownList;
  
              SqlConnection conn = new SqlConnection(connection);
              SqlCommand command = new SqlCommand();
              command.Connection = conn;
              command.CommandText = "Insert into artikulli(tema,abstrakti,data_publikimit,path,keywords,kategoria_id) values (@tema,@abstrakti,@data,@filename,@keywords,@kategoria)";
              command.Parameters.Add(new SqlParameter("@tema", txtTema.Text));
              command.Parameters.Add(new SqlParameter("@abstrakti", txtAbstrakti.Text));
              command.Parameters.Add(new SqlParameter("@data", txtData.Text));
              command.Parameters.Add(new SqlParameter("@keywords", txtKeywords.Text));
              command.Parameters.Add(new SqlParameter("@kategoria", drpdKategoria.SelectedValue));
  
              FileUpload FileUploadArtikull = (FileUpload)e.Item.FindControl("FileUploadArtikull");
  
              if (FileUploadArtikull.HasFile)
              {
                  int filesize = FileUploadArtikull.PostedFile.ContentLength;
                  if (filesize &gt; 4194304)
                  {
                      ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Maximumi i madhesise se file qe lejohet eshte 4MB');", true);
                  }
                  else
                  {
                      string filename = "artikuj/" + Path.GetFileName(FileUploadArtikull.PostedFile.FileName);
                      //add parameters
                      command.Parameters.AddWithValue("@filename", filename);
  
                      conn.Open();
                      command.ExecuteNonQuery();
                      conn.Close();
                      Bind();
  
                      FileUploadArtikull.SaveAs(Server.MapPath("~/artikuj\\" + FileUploadArtikull.FileName));
                      Response.Redirect("dashboard.aspx");
  
                  }
              }
              else
              {
                  ScriptManager.RegisterStartupScript(this, this.GetType(), "popup", "alert('Ju nuk keni ngarkuar asnje artikull');", true);
              }
  
          }
  
      }
  </code></pre>
  
  <p>AddArtikull.aspx</p>
  
  <pre><code>&lt;%@ Page Language="C#" AutoEventWireup="true" CodeFile="AddArtikull.aspx.cs" Inherits="AddArticle" MasterPageFile="~/MasterPage2.master" %&gt;
     &lt;asp:Content ID="content2" ContentPlaceholderID=ContentPlaceHolder2 runat="server"&gt;
         &lt;asp:DataList ID="datalist2" runat="server" 
             onitemcommand="datalist2_ItemCommand" &gt;
              &lt;FooterTemplate&gt; 
              &lt;section id="main" class="column"  runat="server" style="width:900px;"&gt; 
                   &lt;article class="module width_full" style="width:900px;"&gt;
                  &lt;header&gt;&lt;h3 style="text-align: center"&gt;Shto Artikull&amp;nbsp;&lt;/h3&gt;&lt;/header&gt;
              &lt;div id="Div1" class="module_content" runat="server"&gt;         
                  &lt;asp:Label ID="Label1" runat="server" style="font-weight: 700"&gt;Tema&lt;/asp:Label&gt;
                  &lt;fieldset&gt;           
                      &lt;asp:TextBox  ID="txtTema" runat="server"&gt;&lt;/asp:TextBox&gt;
                      &amp;nbsp;&amp;nbsp;
                      &lt;asp:RequiredFieldValidator ID="RequiredFieldValidatorTema" runat="server" 
                          ControlToValidate="txtTema" Display="Dynamic" 
                          ErrorMessage="Kjo fushe eshte e nevojshme" style="color: #CC0000"&gt;&lt;/asp:RequiredFieldValidator&gt;
                  &lt;/fieldset&gt;&lt;br /&gt;
                  &amp;nbsp;&lt;asp:Label ID="Label6" runat="server" style="font-weight: 700"&gt;Abstrakti&lt;/asp:Label&gt;
                  &lt;fieldset&gt;
                  &lt;asp:TextBox  ID="txtAbstrakti" style="height:250px;" Textmode="Multiline" runat="server"&gt;&lt;/asp:TextBox&gt;                 
                      &amp;nbsp;&amp;nbsp;
                      &lt;asp:RequiredFieldValidator ID="RequiredFieldValidatorAbstrakti" runat="server" 
                          ControlToValidate="txtAbstrakti" ErrorMessage="Kjo fushe eshte e nevojshme" 
                          style="color: #CC0000"&gt;&lt;/asp:RequiredFieldValidator&gt;
                  &lt;/fieldset&gt;
                  &lt;asp:Label ID="lblData" runat="server" style="font-weight: 700"&gt;Data e Publikimit&lt;/asp:Label&gt;
                  &lt;fieldset&gt;
                      &lt;asp:TextBox  ID="txtData" runat="server"&gt;&lt;/asp:TextBox&gt;                   
                  &lt;/fieldset&gt;
                  &lt;asp:Label ID="lblKeywords" runat="server" style="font-weight: 700"&gt;Keywords&lt;/asp:Label&gt;
                  &lt;fieldset&gt;
                      &lt;asp:TextBox  ID="txtKeywords" runat="server"&gt;&lt;/asp:TextBox&gt;                   
                  &lt;/fieldset&gt;
                  &lt;br /&gt;
  
                  &lt;asp:Label ID="Label5" runat="server" style="font-weight: 700"&gt;Kategoria&lt;/asp:Label&gt;
                  &lt;div&gt;
                      &lt;asp:DropDownList ID="drpdKategoria" runat="server" AutoPostBack="false"&gt;&lt;/asp:DropDownList&gt;
                      &lt;asp:Button ID="Button1" runat="server" Text="Insert" 
                        /&gt;
                  &lt;/div&gt;&lt;br /&gt; 
                  &lt;strong&gt;Ngarko Artikull&lt;/strong&gt;&lt;br /&gt;
                  &lt;asp:FileUpload ID="FileUploadArtikull" runat="server" /&gt;&lt;br /&gt;
                  &lt;br /&gt;             
                      &lt;asp:Button ID="btnInsert" runat="server" CommandName="Insert" Text="Shto" /&gt;
                      &lt;/div&gt; 
                      &lt;/article&gt; 
                  &lt;/section&gt; 
          &lt;/FooterTemplate&gt;
         &lt;/asp:DataList&gt;
    &lt;/asp:Content&gt;
  </code></pre>
  
  <p>It shows this error:</p>
  
  <blockquote>
    <p>Compiler Error Message: CS0103: The name 'e' does not exist in the
    current context
    In this line:</p>
  </blockquote>
  
  <pre><code> DropDownList drpdKategoria = e.Item.FindControl("drpdKategoria") as DropDownList;
  </code></pre>