String was not recognized as a valid DateTime.

Analyst_SQL 3,551 Reputation points
2021-08-16T12:23:34.727+00:00

I am retrieving data into gridview ,using template field and selecting data from gridview using event of selectedindex changed,

i have column of date,in which null value also have,

                     <asp:TemplateField HeaderText="Approve_Date" >
                                <ItemTemplate>
                                    <asp:Label ID="App_Date" runat="server" Text='<%#Eval("App_Date")%>'></asp:Label>
                                </ItemTemplate>
             </asp:TemplateField>

when i selecting row using below code

    txtappdate.Text = Convert.ToDateTime((GVballist.SelectedRow.FindControl("App_Date") as Label).Text).ToString("yyyy-MM-d").Replace("&nbsp;", null);

then it is giving error String was not recognized as a valid DateTime.
txtappdate.text Text Mode is date.

ASP.NET
ASP.NET
A set of technologies in the .NET Framework for building web applications and XML web services.
3,310 questions
VB
VB
An object-oriented programming language developed by Microsoft that is implemented on the .NET Framework. Previously known as Visual Basic .NET.
2,601 questions
{count} votes

Accepted answer
  1. Albert Kallal 4,896 Reputation points
    2021-08-16T19:15:59.6+00:00

    Ok, the way this works is a bit confusing.

    If you use auto generated column, OR YOU use databound fields, eg like this:

                        &lt;asp:BoundField DataField=&#34;BookingDate&#34;   HeaderText=&#34;Book Date&#34;   /&gt;
    

    Then of course you do NOT use FindControl, but in fact use the .Cells[] colleciton.

    ONLY WHEN you use .Cells[] collection does the text value for a null return that "&nbsp;"

    But, WHEN you use a textbox, label etc.? (and FindControl)?

    Then a null does return a empty string.

    eg this:

           protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
            {
                string str = ((Label)GridView1.SelectedRow.FindControl(&#34;txtBDate&#34;)).Text;
    
                if (str == &#34;&#34;)
                {
                    TextBox1.Text = &#34;blank value&#34;;
                }
                else
                {
                    //TextBox1.Text = str;
                    TextBox1.Text = Convert.ToDateTime(str).ToString(&#34;yyyy-MM-dd&#34;);
                }
    
            }
    

    So in fact, since you ARE using a templated field, then you test for "" for nulls. But you are correct that when the grid renders, asp.net DOES CONVERT nulls to that "&nbsp;" since some browsers will not render a table correct without a blank in that cell.

    So:

    For cells[], and a null? (yes nulls convert to HTML space). And YES you do test for that HTML space like (some value == "&nbsp;".

    For templated controls such as label, text box? The null value is a empty string.

    So, test for "" and you should be fine. And yes, it is VERY easy to mess this up, since you might have some code, databound fields - they DO return that HTML blank space, but templated controls do not use the cells[] collection, and they don't return HTML spaces, but an real empty string for null values.

    Regards, Albert D. Kallal (Access MVP 2003-2017) Edmonton, Alberta Canada

    1 person found this answer helpful.
    0 comments No comments

1 additional answer

Sort by: Most helpful
  1. Yijing Sun-MSFT 7,071 Reputation points
    2021-08-17T10:05:52.967+00:00

    Hi @Analyst_SQL ,
    According to your problems,I reproduced it. First,you need to make sure if the Label of App_Date isn't null. It may cause your problem.And then you could use ParseExact() to parse the data.
    123903-new-text-document-3.txt
    Best regards,
    Yijing Sun


    If the answer is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our  documentation  to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments