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