Click here to Skip to main content
15,881,089 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have a code that i want it to add dates on the database but when i execute the code it says
Object reference not set to an instance of an object.


here is my jquery calender control
C#
<head>
       <meta charset="utf-8" />
       <title>jQuery UI Datepicker - Default functionality</title>
       <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css" />
       <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
       <script src="http://code.jquery.com/ui/1.10.3/jquery-ui.js"></script>
       <link rel="stylesheet" href="/resources/demos/style.css" />
       <script>


           $(document).ready(function () {
               <%-- Dates for election--%>
               $("#dt1").datepicker({
                   dateFormat: "dd-M-yy",
                   minDate: 0,
                   onSelect: function (date) {
                       var date2 = $('#dt1').datepicker('getDate');
                       date2.setDate(date2.getDate() + 1);
                       $('#dt2').datepicker('setDate', date2);
                       //sets minDate to dt1 date + 1
                       $('#dt2').datepicker('option', 'minDate', date2);
                   }
               });
               $('#dt2').datepicker({
                   dateFormat: "dd-M-yy",
                   minDate: 1,
                   onClose: function () {
                       var dt1 = $('#dt1').datepicker('getDate');
                       var dt2 = $('#dt2').datepicker('getDate');
                       //check to prevent a user from entering a date below date of dt1
                       if (dt2 <= dt1) {
                           var minDate = $('#dt2').datepicker('option', 'minDate');
                           $('#dt2').datepicker('setDate', minDate);
                       }
                   }
               });
               <%-- Dates for Party--%>
               $("#pt1").datepicker({
                   dateFormat: "dd-M-yy",
                   minDate: 0,
                   onSelect: function (date) {
                       var date2 = $('#pt1').datepicker('getDate');
                       date2.setDate(date2.getDate() + 1);
                       $('#pt2').datepicker('setDate', date2);
                       //sets minDate to dt1 date + 1
                       $('#pt2').datepicker('option', 'minDate', date2);
                   }
               });
               $('#pt2').datepicker({
                   dateFormat: "dd-M-yy",
                   minDate: 1,
                   onClose: function () {
                       var dt1 = $('#pt1').datepicker('getDate');
                       var dt2 = $('#pt2').datepicker('getDate');
                       //check to prevent a user from entering a date below date of dt1
                       if (dt2 <= dt1) {
                           var minDate = $('#pt2').datepicker('option', 'minDate');
                           $('#pt2').datepicker('setDate', minDate);
                       }
                   }
               });
           });


       </script>
   </head>




C#
<table border="0" cellpadding="2" style="width: 700px;">
                    <asp:Label ID="Label1" runat="server" ForeColor="Green" Text=""></asp:Label>
                    <tr>
                        <td style="color: #5D7B9D;" align="center">Election Name:</td>
                        <td>
                            <asp:TextBox ID="txtElectionName" AutoPostBack="true" runat="server" Style="color: #5D7B9D; width: 150px; border-color: #5D7B9D; border-width: 1px; border-style: Solid;"></asp:TextBox>
                            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" ControlToValidate="txtElectionName" ForeColor="Red" runat="server" ErrorMessage="Please enter election name."></asp:RequiredFieldValidator>
                        </td>
                    </tr>
                    <%-- Election start date here --%>
                    <tr>
                        <td style="color: #5D7B9D;" align="center">Election Start Date:</td>
                        <td>
                             <input type="text" id="dt1" />
                      

                        </td>
                    </tr>

                    <tr>
                        <td style="color: #5D7B9D;" align="center">Election End Date:</td>
                        <td>
                             <input type="text" id="dt2" />
             

                        </td>
                    </tr>

                    <tr>
                        <td style="color: #5D7B9D;" align="center">Adding a Party Start Date:</td>
                        <td>
                            <input type="text" id="pt1" />
                   

                        </td>
                    </tr>

                    <tr>
                        <td style="color: #5D7B9D;" align="center">Adding a Party End Date:</td>
                        <td>
                             <input type="text" id="pt2" />
             

                        </td>
                    </tr>


                    <tr>
                        <td align="right">
                            <br />
                            <asp:Label ID="lblResults" runat="server" Text=""></asp:Label>
                            <asp:Button ID="AddElection" runat="server" Text="Submit" OnClick="AddElection_Click" Style="color: white; width: 100px; background-color: #284775; border-color: #5D7B9D; border-width: 1px; border-style: Solid;" />
                        </td>
                    </tr>

                </table>



Here is my code behind

C#
protected void AddElection_Click(object sender, EventArgs e)
   {
       string estart = Request.Form["dt1"];
       string eend = Request.Form["dt2"];
       string pstart = Request.Form["pt1"];
       string pend = Request.Form["pt2"];





       NewElection elec = new NewElection(txtElectionName.Text, Request["estart"].ToString(), Request["eend"].ToString(), Request["pstart"].ToString(), Request["pend"].ToString());
       elections.AddElections(elec);
       Label1.Text = "Thank you for adding an election.";


   }
Posted
Updated 11-Jul-21 22:40pm
Comments
Jameel VM 9-Sep-13 11:57am    
in which format you get the date to server?
[no name] 9-Sep-13 12:02pm    
format is nvarchar if this is what u asking
Jameel VM 9-Sep-13 12:11pm    
no.for example dd-mm-yyyy like that.what value you got in estart and eend?
[no name] 9-Sep-13 12:21pm    
this is my format '10-Sep-2013' , i tryed to use a pointbreck but it return NULL but there are values in the textarea

1 solution

You should provide name attribute to the control for getting values by using Request.Form[].For example
C#
<input type="text">name="dt1" id="dt1" /> string estart = Request.Form["dt1"];</input>

Hope this helps
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900