title: How to insert DropDownList selected value to GridView in asp.net?
body: <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using System.Web.UI;
  using System.Web.UI.WebControls;
  using System.Data;
  using System.Drawing;
  
  namespace NewDPOcopy
  {
      public partial class UpdatePaperCost : System.Web.UI.Page
      {
         Services.ServiceAuthentication ser = new Services.ServiceAuthentication();
         // Services.ServiceAuthentication ser = new Services.ServiceAuthentication();
  
          protected void Page_Load(object sender, EventArgs e)
          {
              fillgrodPaperType();
              ddlcatid.Visible = true;
              ddlcatid.DataSource = ser.Getpapercategory();
              ddlcatid.DataTextField = "Cat_Name";
              //ddlcatid.DataValueField = "Cat_id";
              ddlcatid.DataBind();
          }
  
          protected void fillgrodPaperType()
          {
              Services.PaperTypes[] PaperInfo = ser.GetALLPaperTypeInfo();
  
              DataTable dt = new DataTable();
              dt.Columns.Add(new DataColumn("pt_id", typeof(string)));
              dt.Columns.Add(new DataColumn("pt_name", typeof(string)));
              dt.Columns.Add(new DataColumn("pt_weight", typeof(int)));
  
              dt.Columns.Add(new DataColumn("pt_size", typeof(string)));
              dt.Columns.Add(new DataColumn("pt_cost_per_sheet", typeof(double)));
              dt.Columns.Add(new DataColumn("pt_status", typeof(string)));
  
  
              dt.Columns.Add(new DataColumn("pt_used_by_247", typeof(Boolean)));
              dt.Columns.Add(new DataColumn("pt_front_finish", typeof(string)));
              dt.Columns.Add(new DataColumn("pt_back_finish", typeof(string)));
  
              dt.Columns.Add(new DataColumn("pt_default", typeof(Boolean)));
              dt.Columns.Add(new DataColumn("pt_colour", typeof(string)));
              dt.Columns.Add(new DataColumn("pt_nexpress", typeof(Boolean)));
              dt.Columns.Add(new DataColumn("pt_igen", typeof(Boolean)));
              dt.Columns.Add(new DataColumn("Cat_Name", typeof(string)));
  
              for (int i = 0; i &lt; PaperInfo.Length; i++)
              {
                  DataRow dr = null;
                  dr = dt.NewRow();
                  dr["pt_id"] = PaperInfo[i].Pt_id;
                  dr["pt_name"] = PaperInfo[i].Pt_name;
                  dr["pt_weight"] = PaperInfo[i].Pt_weight;
  
                  dr["pt_size"] = PaperInfo[i].Pt_size;
                  dr["pt_cost_per_sheet"] = PaperInfo[i].Pt_cost_per_sheet;
                  // dr["pt_cost_per_sheet"] = 12;
                  dr["pt_status"] = PaperInfo[i].Pt_status;
  
                  dr["pt_used_by_247"] = PaperInfo[i].Pt_used_by_247;
                  dr["pt_front_finish"] = PaperInfo[i].Pt_front_finish;
                  dr["pt_back_finish"] = PaperInfo[i].Pt_back_finish;
  
                  dr["pt_default"] = PaperInfo[i].Pt_default;
                  dr["pt_colour"] = PaperInfo[i].Pt_colour;
                  dr["pt_nexpress"] = PaperInfo[i].Pt_nexpress;
                  dr["pt_igen"] = PaperInfo[i].Pt_igen;
                  dr["Cat_Name"] = PaperInfo[i].Cat_Id;
                  dt.Rows.Add(dr);
              }
  
              GrdvwPTID.DataSource = dt;
              GrdvwPTID.DataBind();
  
              //ddlPTID.DataSource = dt;
              //ddlPTID.DataTextField = "pt_name";
              //ddlPTID.DataValueField = "pt_id";
              //ddlPTID.DataBind();
          }    
  
          protected void ClearPaperType()
          {
              txtptid.Text = "";
              txtptname.Text = "";
              txtptweight.Text = "";
              txtptsize.Text = "";
              txtptcostpersheet.Text = "";
              txtptstatus.Text = "";
              txtptfontfinish.Text = "";
              txtptbackfinish.Text = "";
              txtptcolor.Text = "";
              ddlptdefault.ClearSelection();
              ddlptdefault.Items.FindByText("&lt;--select--&gt;").Selected = true;
              ddlptnexpress.ClearSelection();
              ddlptnexpress.Items.FindByText("&lt;--select--&gt;").Selected = true;
              ddlptusedby247.ClearSelection();
              ddlptusedby247.Items.FindByText("&lt;--select--&gt;").Selected = true;
              ddlptigen.ClearSelection();
              ddlptigen.Items.FindByText("&lt;--select--&gt;").Selected = true;
              lblMessagePTYPE.Visible = false;
              HiddenField2.Value = "";
          }
  
          protected void GrdvwPTID_SelectedIndexChanged(object sender, EventArgs e)
          {    
          }
  
          protected void GrdvwPTID_RowCommand(object sender, GridViewCommandEventArgs e)
          {    
              int index = Convert.ToInt32(e.CommandArgument);
              Session["INDEX"] = index;
  
              lblMessagePTYPE.Visible = false;
  
              txtptid.Text = GrdvwPTID.Rows[index].Cells[1].Text;
              txtptname.Text = GrdvwPTID.Rows[index].Cells[2].Text;
              txtptweight.Text = GrdvwPTID.Rows[index].Cells[3].Text;
              txtptsize.Text = GrdvwPTID.Rows[index].Cells[4].Text;
              txtptcostpersheet.Text = GrdvwPTID.Rows[index].Cells[5].Text;
              txtptstatus.Text = GrdvwPTID.Rows[index].Cells[6].Text;
  
              ddlptusedby247.ClearSelection();
              if (GrdvwPTID.Rows[index].Cells[7].Text == "True")
              {
                  ddlptusedby247.Items.FindByText("Yes").Selected = true;
              }
              else
              {
                  ddlptusedby247.Items.FindByText("No").Selected = true;
              }
  
              txtptfontfinish.Text = GrdvwPTID.Rows[index].Cells[8].Text;
              txtptbackfinish.Text = GrdvwPTID.Rows[index].Cells[9].Text;
              ddlptdefault.ClearSelection();
              if (GrdvwPTID.Rows[index].Cells[10].Text == "True")
              {
                  ddlptdefault.Items.FindByText("Yes").Selected = true;
              }
              else
              {
                  ddlptdefault.Items.FindByText("Yes").Selected = false;
              }
              txtptcolor.Text = GrdvwPTID.Rows[index].Cells[11].Text;
  
              ddlptnexpress.ClearSelection();
              if (GrdvwPTID.Rows[index].Cells[12].Text == "True")
              {
                  ddlptnexpress.Items.FindByText("Yes").Selected = true;
              }
              else
              {
                  ddlptnexpress.Items.FindByText("No").Selected = true;
              }
              ddlptigen.ClearSelection();
              if (GrdvwPTID.Rows[index].Cells[13].Text == "True")
              {
                  ddlptigen.Items.FindByText("Yes").Selected = true;
              }
              else
              {
                  ddlptigen.Items.FindByText("Yes").Selected = false;
              }
              //ddlcatid.ClearSelection();
              //if (GrdvwPTID.Rows[index].Cells[14].Text == "True")
              //{
              //    ddlcatid.Items.FindByText("Yes").Selected = true;
              //}
              //else
              //{
              //    ddlcatid.Items.FindByText("Yes").Selected = false;
              //}
              HiddenField2.Value = "EditPAPERTYPE";    
          }
  
          protected void btnsubmit_Click(object sender, EventArgs e)
          {
              if (txtptid.Text == "" || txtptname.Text == "" || txtptweight.Text == "" || txtptsize.Text == "" || txtptcostpersheet.Text == "" || txtptstatus.Text == "" || txtptfontfinish.Text == "" || txtptbackfinish.Text == "" || txtptcolor.Text == "" || ddlptdefault.SelectedItem.ToString() == "&lt;--select--&gt;" || ddlptnexpress.SelectedItem.ToString() == "&lt;--select--&gt;" || ddlptusedby247.SelectedItem.ToString() == "&lt;--select--&gt;" || ddlptigen.SelectedItem.ToString() == "&lt;--select--&gt;")
              {
                  lblMessagePTYPE.Visible = true;
                  lblMessagePTYPE.ForeColor = Color.Red;
                  lblMessagePTYPE.Text = "FILL ALL THE FIELDS AND CLICK HERE TO SUBMIT";    
              }
              else
              {    
                  Boolean ptused247;
  
                  if (ddlptusedby247.SelectedItem.ToString() == "Yes")
                  {
                      ptused247 = true;
                  }
                  else
                  {
  
                      ptused247 = false;
                  }
  
                  Boolean ptDefault;
  
                  if (ddlptdefault.SelectedItem.ToString() == "Yes")
                  {
                      ptDefault = true;
                  }
                  else
                  {
                      ptDefault = false;
                  }
  
                  Boolean ptNexPRESS;
  
                  if (ddlptnexpress.SelectedItem.ToString() == "Yes")
                  {
                      ptNexPRESS = true;
                  }
                  else
                  {
                      ptNexPRESS = false;
                  }
  
                  Boolean ptIgen;
  
                  if (ddlptigen.SelectedItem.ToString() == "Yes")
                  {
                      ptIgen = true;
                  }
                  else
                  {
                      ptIgen = false;
                  }   
  
                  Boolean catName;
  
                  if (ddlcatid.SelectedItem.ToString() == "Yes")
                  {
                      catName = true;
                  }
                  else
                  {
                      catName = false;
                  }
                  //insert Query Call...........
  
                  if (HiddenField2.Value != "EditPAPERTYPE")
                  {
                      bool InsertResultPT = ser.InsertPaperType(txtptid.Text, txtptname.Text, Convert.ToInt32(txtptweight.Text), txtptsize.Text, Convert.ToDouble(txtptcostpersheet.Text), txtptstatus.Text, ptused247, txtptfontfinish.Text, txtptbackfinish.Text, ptDefault, txtptcolor.Text, ptNexPRESS, ptIgen, catName);
                      if (InsertResultPT)
                      {
                          lblMessagePTYPE.Visible = true;
                          lblMessagePTYPE.ForeColor = Color.Green;
                          lblMessagePTYPE.Text = "PAPER TYPE VALUES INSERTED SUCCESSFULLY!";
                          fillgrodPaperType();
                      }
                      else
                      {
                          lblMessagePTYPE.Visible = true;
                          lblMessagePTYPE.ForeColor = Color.Red;
                          lblMessagePTYPE.Text = "YOU ENTERED WRONG VALUES !";
                      }
  
                      // ClearPaperType();
                      txtptid.Text = "";
                      txtptname.Text = "";
                      txtptweight.Text = "";
                      txtptsize.Text = "";
                      txtptcostpersheet.Text = "";
                      txtptstatus.Text = "";
                      txtptfontfinish.Text = "";
                      txtptbackfinish.Text = "";
                      txtptcolor.Text = "";
                      ddlptdefault.ClearSelection();
                      ddlptdefault.Items.FindByText("&lt;--select--&gt;").Selected = true;
                      ddlptnexpress.ClearSelection();
                      ddlptnexpress.Items.FindByText("&lt;--select--&gt;").Selected = true;
                      ddlptusedby247.ClearSelection();
                      ddlptusedby247.Items.FindByText("&lt;--select--&gt;").Selected = true;
                      ddlptigen.ClearSelection();
                      ddlptigen.Items.FindByText("&lt;--select--&gt;").Selected = true;
                      HiddenField2.Value = "";
                  }    
                  else
                  {
                      //Update Query Call...........
                      bool UpdateResult = ser.UpdatePaperTypes(txtptid.Text, txtptname.Text, Convert.ToInt16(txtptweight.Text), txtptsize.Text, Convert.ToDouble(txtptcostpersheet.Text), txtptstatus.Text, ptused247, txtptfontfinish.Text, txtptbackfinish.Text, ptDefault, txtptcolor.Text, ptNexPRESS, ptIgen,catName);
                      if (UpdateResult)
                      {
                          lblMessagePTYPE.Visible = true;
                          lblMessagePTYPE.ForeColor = Color.Green;
                          lblMessagePTYPE.Text = "PAPER TYPE VALUES UPDATED SUCCESSFULLY!";
                          fillgrodPaperType();
                          txtptid.Text = "";
                          txtptname.Text = "";
                          txtptweight.Text = "";
                          txtptsize.Text = "";
                          txtptcostpersheet.Text = "";
                          txtptstatus.Text = "";
                          txtptfontfinish.Text = "";
                          txtptbackfinish.Text = "";
                          txtptcolor.Text = "";
                          ddlptdefault.ClearSelection();
                          ddlptdefault.Items.FindByText("&lt;--select--&gt;").Selected = true;
                          ddlptnexpress.ClearSelection();
                          ddlptnexpress.Items.FindByText("&lt;--select--&gt;").Selected = true;
                          ddlptusedby247.ClearSelection();
                          ddlptusedby247.Items.FindByText("&lt;--select--&gt;").Selected = true;
                          ddlptigen.ClearSelection();
                          ddlptigen.Items.FindByText("&lt;--select--&gt;").Selected = true;
                          HiddenField2.Value = "";
                      }
                      else
                      {
                          lblMessagePTYPE.Visible = true;
                          lblMessagePTYPE.ForeColor = Color.Red;
                          lblMessagePTYPE.Text = "Updation failed please try again.";    
                      }
  
                      HiddenField2.Value = "";
                  }    
              }
  
              txtptid.Text = "";
              txtptname.Text = "";
              txtptweight.Text = "";
              txtptsize.Text = "";
              txtptcostpersheet.Text = "";
              txtptstatus.Text = "";
              txtptfontfinish.Text = "";
              txtptbackfinish.Text = "";
              txtptcolor.Text = "";
              ddlptdefault.ClearSelection();
              ddlptdefault.Items.FindByText("&lt;--select--&gt;").Selected = true;
              ddlptnexpress.ClearSelection();
              ddlptnexpress.Items.FindByText("&lt;--select--&gt;").Selected = true;
              ddlptusedby247.ClearSelection();
              ddlptusedby247.Items.FindByText("&lt;--select--&gt;").Selected = true;
              ddlptigen.ClearSelection();
              ddlptigen.Items.FindByText("&lt;--select--&gt;").Selected = true;
              HiddenField2.Value = "";
          }
  
          protected void btndelete_Click1(object sender, EventArgs e)
          {
              if (HiddenField2.Value != "EditPAPERTYPE")
              {    
                  lblMessagePTYPE.Visible = true;
                  lblMessagePTYPE.ForeColor = Color.Red;
                  lblMessagePTYPE.Text = "PLEASE SELECT THE RECORD FROM TABLE!";
              }
              else
              {    
                  bool deletePaperType = ser.RemovePaperType(GrdvwPTID.Rows[Convert.ToInt16(Session["INDEX"].ToString())].Cells[1].Text);
  
                  if (deletePaperType)
                  {
                      lblMessagePTYPE.Visible = true;
                      lblMessagePTYPE.ForeColor = Color.Green;
                      lblMessagePTYPE.Text = "PAPER TYPE VALUES HAS BEEN DELETED SUCCESSFULLY!";
                      HiddenField2.Value = "";
                  }
                  else
                  {
                      lblMessagePTYPE.Visible = true;
                      lblMessagePTYPE.ForeColor = Color.Red;
                      lblMessagePTYPE.Text = "No match found !";
                      HiddenField2.Value = "";
                  }
                  // ClearPaperType();
                  txtptid.Text = "";
                  txtptname.Text = "";
                  txtptweight.Text = "";
                  txtptsize.Text = "";
                  txtptcostpersheet.Text = "";
                  txtptstatus.Text = "";
                  txtptfontfinish.Text = "";
                  txtptbackfinish.Text = "";
                  txtptcolor.Text = "";
                  ddlptdefault.ClearSelection();
                  ddlptdefault.Items.FindByText("&lt;--select--&gt;").Selected = true;
                  ddlptnexpress.ClearSelection();
                  ddlptnexpress.Items.FindByText("&lt;--select--&gt;").Selected = true;
                  ddlptusedby247.ClearSelection();
                  ddlptusedby247.Items.FindByText("&lt;--select--&gt;").Selected = true;
                  ddlptigen.ClearSelection();
                  ddlptigen.Items.FindByText("&lt;--select--&gt;").Selected = true;
                  fillgrodPaperType();    
              }    
          }
  
          protected void btncancel_Click(object sender, EventArgs e)
          {
              ClearPaperType();
          }
      }
  }
  </code></pre>
  
  <p>I have the above code in that in Page_lode event its binding the value to dropedown but when i am going to insert but its not inserting and not showing into grideview but in dropedown all values are binded how to insert into grideview? and  dr["Cat_Name"] = PaperInfo[i].Cat_Id; is null only its not taking values but in my db cat_id values are there.</p>