0

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

Object reference not set to an instance of an object

my aspx page code:

  <link rel="stylesheet" href="//code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">
   <link rel="stylesheet" href="\GridMaster\Styles\">
    <script src="https://code.jquery.com/jquery-1.12.4.js"></script>
  <script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
     <script>
         $(function () {
             $("#datepicker").datepicker({
                 changeMonth: true,
                 changeYear: true
             });
         });
     </script>
 <td class="dateBox">
   <input type="text" id="datepicker" />
 </td>

Aspx.cs page:

 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 > 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;
                }



        }
Maistrenko Vitalii
  • 944
  • 1
  • 7
  • 14
divikdiya
  • 213
  • 2
  • 4
  • 11
  • You need to debug and check, what is the value coming up in `dt.Rows[0][2]` That should be coming as null. Thus, calling `ToString` is throwing that error. – Rahul Singh Feb 14 '18 at 07:14
  • yes, the value is null. how to solve that – divikdiya Feb 14 '18 at 09:19
  • Check why DB is returning null. If `Null` is fine and you want to display something when DB is returning null the change your code to:- `dt.Rows[0][2]?.ToString() ?? "";` (Assuming you are using C# 6) – Rahul Singh Feb 14 '18 at 09:21
  • please could you update my code to run ? – divikdiya Feb 14 '18 at 09:46

1 Answers1

0

Your html contol is a client side control. You cannot access it from server side code. You can either use,

string datePickerValue = Request.Form["datepicker"];

or

// aspx
<asp:TextBox ID="datepicker" type="text" runat="server"></asp:TextBox>

// aspx.cs
string datePickerValue = datepicket.Text;
Jaliya Udagedara
  • 730
  • 6
  • 11