title: How to solve Object reference not set to an instance of an object for input field in asp.net?
body: <p>I want to store date to database.I am  clicking the textbox it show an calender after date choose it stored into textbox nut not stored in Sql Database. It shows an error </p>
  
  <blockquote>
    <p>Object reference not set to an instance of an object</p>
  </blockquote>
  
  <p>my aspx page code:</p>
  
  <pre><code>  &lt;link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css"&gt;
     &lt;link rel="stylesheet" href="\GridMaster\Styles\"&gt;
      &lt;script src="https://code.jquery.com/jquery-1.12.4.js"&gt;&lt;/script&gt;
    &lt;script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"&gt;&lt;/script&gt;
       &lt;script&gt;
           $(function () {
               $("#datepicker").datepicker({
                   changeMonth: true,
                   changeYear: true
               });
           });
       &lt;/script&gt;
   &lt;td class="dateBox"&gt;
     &lt;input type="text" id="datepicker" /&gt;
   &lt;/td&gt;
  </code></pre>
  
  <p>Aspx.cs page:</p>
  
  <pre><code> private void BindTextBoxvalues()
          {
              string constr = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
              SqlConnection con = new SqlConnection(constr);
              SqlCommand cmd = new SqlCommand("select * from Student where RegNo=" + num, con);
              DataTable dt = new DataTable();
              SqlDataAdapter da = new SqlDataAdapter(cmd);
              da.Fill(dt);
              if (dt.Rows.Count &gt; 0)
              {
                  RegNo.Text = dt.Rows[0][0].ToString();
                  Name.Text = dt.Rows[0][1].ToString();
                  datepicker.Text = dt.Rows[0][2].ToString();//Object reference not set to an instance of an object
  
  
              }
          }
   protected void Add_Click(object sender, EventArgs e)
          {            
                  string constr = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
                  SqlConnection con = new SqlConnection(constr);
  
                  SqlCommand cmd1 = new SqlCommand("sp", con);
                  cmd1.CommandType = CommandType.StoredProcedure;
                  con.Open();
                  cmd1.Parameters.AddWithValue("@RegNo", RegNo.Text);
                  cmd1.Parameters.AddWithValue("@Name", Name.Text);
                  cmd1.Parameters.AddWithValue("@DOB", datepicker.Text);//Object reference not set to an instance of an object 
  
  
                  try
                  {
                      cmd1.ExecuteNonQuery();
                      Label2.Visible = true;
  
                  }
                  catch (Exception ex)
                  {
                      throw ex;
                  }
  
  
  
          }
  </code></pre>