question

akhterhussain-3167 avatar image
0 Votes"
akhterhussain-3167 asked YijingSun-MSFT answered

String was not recognized as a valid DateTime.

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.



dotnet-visual-basicdotnet-aspnet-general
· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

Hello

What does the value equal which should be a DateTime?

Generally one can use test a string to DateTime via DateTime.TryParse.

If you are not getting proper results use the debugger with a breakpoint.


0 Votes 0 ·
AlbertKallal-4360 avatar image
0 Votes"
AlbertKallal-4360 answered AlbertKallal-4360 edited

Ok, the way this works is a bit confusing.

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

                     <asp:BoundField DataField="BookingDate"   HeaderText="Book Date"   />

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("txtBDate")).Text;
    
             if (str == "")
             {
                 TextBox1.Text = "blank value";
             }
             else
             {
                 //TextBox1.Text = str;
                 TextBox1.Text = Convert.ToDateTime(str).ToString("yyyy-MM-dd");
             }
    
         }


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

5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

YijingSun-MSFT avatar image
0 Votes"
YijingSun-MSFT answered

Hi @akhterhussain-3167 ,
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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.