title: how to save multiple dynamic textbox and dropdownlist values in database in asp.net
body: <p>How do I save multiple dynamic textboxes and dropdownlist values in a database using asp.net?</p>
  
  <p><strong>For that I used code below as follows :</strong></p>
  
  <pre><code>public partial class StockEntry : System.Web.UI.Page
  {
      protected System.Web.UI.WebControls.DropDownList Ddl_Item;
      protected System.Web.UI.WebControls.TextBox Txt_Quantity;
      protected System.Web.UI.WebControls.DropDownList Ddl_Unit;
      protected System.Web.UI.WebControls.TextBox Txt_Available;
  
      protected void Page_Load(object sender, EventArgs e)
      {
          if (!Page.IsPostBack)
          {
              GoodMasterDetails();
              UnitMasterDetails();
          }
      }
  
    protected void Button1_Click(object sender, EventArgs e)
      {
          if (ViewState["countTimes"] == null)
          {
              countTimes = 1;
          }
          else
          {
              countTimes = Convert.ToInt32(ViewState["countTimes"]);
          }
  
          for (int i = 0; i &lt; countTimes; i++)
          {
              Ddl_Item = new DropDownList();
              Ddl_Item.ID = "Ddl_Item";
              Ddl_Item.Width = 150 + i;
              Ddl_Item.Style[" margin-right"] = "14px";
              Ddl_Item.Style[" margin-left"] = "10px";
              BLL_StockEntry BLL = new BLL_StockEntry();
              DataTable dt = new DataTable();
              dt = BLL.GoodsMasterDetails();
              if (dt.Rows.Count &gt; 0)
              {
                  Ddl_Item.DataSource = dt;
                  Ddl_Item.DataTextField = "GM_NAME";
                  Ddl_Item.DataValueField = "GM_ID";
                  Ddl_Item.DataBind();
                  Ddl_Item.Items.Insert(0, new ListItem("---Select---", string.Empty));
              }
  
              Txt_Quantity = new TextBox();
              Txt_Quantity.ID = "Txt_Quantity" + countTimes;
              Txt_Quantity.Width = 150 + i;
              Txt_Quantity.Style[" margin-right"] = "14px";
              //Txt_Quantity.Style[" margin-left"] = "0px";
  
              Ddl_Unit = new DropDownList();
              Ddl_Unit.ID = "Ddl_Unit" + countTimes;
              Ddl_Unit.Width = 150 + i;
              Ddl_Unit.Style[" margin-right"] = "14px";
              Ddl_Unit.Style[" margin-left"] = "10px";
              BLL_StockEntry BLLL = new BLL_StockEntry();
              DataTable dt1 = new DataTable();
              dt1 = BLLL.UnitsMasterDetails();
              if (dt1.Rows.Count &gt; 0)
              {
                  Ddl_Unit.DataSource = dt1;
                  Ddl_Unit.DataTextField = "UM_UNITNAME";
                  Ddl_Unit.DataValueField = "UM_ID";
                  Ddl_Unit.DataBind();
                  Ddl_Unit.Items.Insert(0, new ListItem("---Select---", string.Empty));
              }
  
              Txt_Available = new TextBox();
              Txt_Available.ID = "Txt_Available" + countTimes;
              Txt_Available.Width = 150 + i;
              Txt_Available.Style[" margin-right"] = "14px";
              //Txt_Available.Style[" margin-left"] = "10px";
  
              dvContainer.Controls.Add(Ddl_Item);
              dvContainer.Controls.Add(Txt_Quantity);
  
              dvContainer.Controls.Add(Ddl_Unit);
              dvContainer.Controls.Add(Txt_Available);
  
          }
          countTimes = countTimes + 1;
  
          ViewState.Add("countTimes", countTimes);
      }
  
   private void StockEntryBind(string mode)
      {
          PL.RESTURENT.PL_StockEntry objpl = new PL.RESTURENT.PL_StockEntry();
  
          objpl.StockEntryId = HiddenField1.Value.ToString();
  
          objpl.Date = DateTime.Parse(txt_date.Text);
  
          objpl.Time = txt_time.Text;
  
          objpl.Godown = Ddl_Godown.SelectedItem.Text;
  
          objpl.Item = Ddl_Items.SelectedItem.Text;//static
  
          string str = string.Empty;//dynamic
          DropDownList ddlitem = (DropDownList)dvContainer.FindControl("Ddl_Item") as DropDownList;
          str = ddlitem.SelectedItem.Text;
  
          objpl.Quantity = Txt_Quantity.Text;
  
          objpl.Unit = Ddl_Unit.SelectedItem.Text;
  
          objpl.AvailableStock = Txt_Available.Text;
  
          objpl.mode = mode;
  
          BLL_StockEntry objbll = new BLL_StockEntry();
          if (objbll.StockEntryTransaction(objpl))
          {
              ScriptManager.RegisterStartupScript(this, this.GetType(), "", "Alert('Pay    Slip Inserted Succesfully...')", true);
          }
      }
  </code></pre>
  
  <p>I have tried only for (dropdownlist)Ddl_Item to insert data in database but</p>
  
  <pre><code>str = ddlitem.SelectedItem.Text; 
  </code></pre>
  
  <p>Error: <code>Object reference not set to an instance of an object</code>.</p>
  
  <p>Your help will be highly appreciated.</p>
  
  <p>I am declaring method to call in button event through mode type using dynamic stored procedure.</p>