Object reference not set to instance of an object error SharePoint DateTimeControl selectedDate

Respondido Object reference not set to instance of an object error SharePoint DateTimeControl selectedDate

  • giovedì 9 agosto 2012 02:15
     
     

    Hi,

    I have a DateTimeControl and in a GridView. I want to display selectedDate inside the textbox after click on a button. But OnClick event of the button I got 'Object reference not set to instance of an object' error. Below are the codes:

     protected void Test_Click(object sender, EventArgs e)
        {
            DateTimeControl DateAndTimeIn = gvRoom.FindControl("SPTimeIn") as DateTimeControl;

            txtTest.Text = DateAndTimeIn .SelectedDate.ToString();
        }

    Any help on this issue would be appreciated. Thanks


    wawa87

Tutte le risposte

  • venerdì 10 agosto 2012 03:06
     
     

    Hi,

    I have a DateTimeControl and in a GridView. I want to display selectedDate inside the textbox after click on a button. But OnClick event of the button I got 'Object reference not set to instance of an object' error. Below are the codes:

     protected void Test_Click(object sender, EventArgs e)
        {
            DateTimeControl DateAndTimeIn = gvRoom.FindControl("SPTimeIn") as DateTimeControl;

            txtTest.Text = DateAndTimeIn .SelectedDate.ToString();
        }

    Any help on this issue would be appreciated. Thanks


    wawa87

    Any feedback on this item yet?

    wawa87

  • venerdì 10 agosto 2012 04:13
     
     

    Hello wawa87,

    have you checked that the value of DateAndTimeIn object,

    i think you are getting null value,

    can you please confirm this?


    Hiren Patel | Please click "Propose As Answer" if this post solves your problem or "Vote As Helpful" if this post has been useful to you.

  • venerdì 10 agosto 2012 05:13
     
     

    I've tried to check either the control return null value but I still getting the same error message.

    DateTimeControl DateAndTimeIn = gvRoom.FindControl("SPTimeIn") as DateTimeControl;
            if (DateAndTimeIn .SelectedDate != null)
            {
                Response.Write("not null");
                //txtTest.Text = DateAndTimeIn .SelectedDate.ToString();
            }
            else
            {
                Response.Write("null");
            }


    wawa87

  • venerdì 10 agosto 2012 05:28
     
     

    Yes the error will come because of the value of DateAndTimeIn  is null and in if condition you are trying to access the property SelectedDate thats why you are receiving error.

    i want to know that where you have put that DataTime control in gridview , header template or item tempate ?

    if possible then also provide the source code for the gridview for .aspx or .ascx file and also code behind to it will easy to short out the issue.


    Hiren Patel | Please click "Propose As Answer" if this post solves your problem or "Vote As Helpful" if this post has been useful to you.

  • venerdì 10 agosto 2012 08:22
     
      Contiene codice

    Here you can how to find the control if have put it in item template of the grid view

    <asp:GridView ID="grd" runat="server" AutoGenerateColumns="False" 
    			onrowdatabound="grd_RowDataBound">
    			<Columns>
    				<asp:TemplateField>
    					<HeaderTemplate>
    						<table>
    							<tr>
    								<td>
    									<asp:Label ID="lblFnameHeader" runat="server"></asp:Label>
    								</td>
    								<td>
    									<asp:Label ID="lblLNameHeader" runat="server"></asp:Label>
    								</td>
    								<td>
    									<asp:Label ID="lblOperation" runat="server" Text="Operation"></asp:Label>
    								</td>
    							</tr>
    						</table>
    					</HeaderTemplate>
    					<ItemTemplate>
    						<table>
    							<tr>
    								<td>
    									<asp:Label ID="lblFname" runat="server" Text='<%# Bind("Fname") %>'></asp:Label>
    								</td>
    								<td>
    									<asp:Label ID="lblLName" runat="server" Text='<%# Bind("Lname") %>'></asp:Label>
    								</td>
    								<td>
    									<asp:Button ID="btn" runat="server" Text='<%# Bind("Fname") %>' />
    								</td>
    							</tr>
    						</table>
    					</ItemTemplate>
    				</asp:TemplateField>
    			</Columns>
    		</asp:GridView>

    code behind

    public void BindGrid()
    	{
    		DataTable dt = new DataTable();
    		DataColumn col1 = new DataColumn("Fname", typeof(string));
    		DataColumn col2 = new DataColumn("Lname", typeof(string));
    
    		dt.Columns.Add(col1);
    		dt.Columns.Add(col2);
    		DataRow row = dt.NewRow();
    		row["Fname"] = "name1";
    		row["Lname"] = "lnamel";
    		dt.Rows.Add(row);
    		DataRow row2 = dt.NewRow();
    		row["Fname"] = "name2";
    		row["Lname"] = "lname2";
    		dt.Rows.Add(row2);
    
    		grd.DataSource = dt;
    		grd.DataBind();
    	}
    	protected void grd_RowDataBound(object sender, GridViewRowEventArgs e)
    	{
    		if (e.Row.RowType == DataControlRowType.DataRow)
    		{
    			Label lblFname = e.Row.FindControl("lblFname") as Label;
    			Label lblLname = e.Row.FindControl("lblLname") as Label;
    		}
    	}


    Hiren Patel | Please click "Propose As Answer" if this post solves your problem or "Vote As Helpful" if this post has been useful to you.

  • sabato 11 agosto 2012 10:48
     
     Con risposta

    Hi Hiren,

    Thanks for the guide. I'm able to generate new row for textbox or label in my gridview but not for the SP datetimepicker. As for the resolution, I have used jQuery datepicker and it solved my issue. 


    wawa87