I am creating a gridview that allows the user to enter the date (example Date of Completion) of a specific artifact (There are multiple). If I try to enter a null or empty value it crashes. I have looked high and low for an answer to this. The data they enter is updated to a sql server database. I have switched the data back and forth between between date and nvarchar. Also, the value has to bind so it updates the database in the ASP.net.
Code
ASP.NET
<asp:TemplateField HeaderText="Verify Info Prod & Maturity Level" SortExpression="IPMaturity"> <EditItemTemplate> <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("IPMaturity") %>' Width="75"></asp:TextBox> <asp:CheckBox ID="CheckBox1" runat="server" AutoPostBack="true" Text="Complete" Checked='<%# Bind ("CheckBox1") %>'></asp:CheckBox> </EditItemTemplate> <ItemTemplate> <asp:Label ID="lblIPMat" runat="server" Text='<%# Eval("IPMaturity") %>' Width="75"></asp:Label> </ItemTemplate> <HeaderStyle CssClass="verticaltext" Height="140px" Width="88px" HorizontalAlign="Center" VerticalAlign="Bottom" /> </asp:TemplateField>
Code C#
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { DateTime date1 = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "IPMaturity")); if (date1 >= DateTime.Now) e.Row.Cells[4].BackColor = System.Drawing.Color.White; if (date1 < DateTime.Now) e.Row.Cells[4].BackColor = System.Drawing.Color.Red; } }
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
You may be looking for a nullable DateTime: DateTime?
The System.DateTime
type is a value type so it does not allow null. The Nullable
class enables setting the value to null
.
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { string ipMaturity = DataBinder.Eval(e.Row.DataItem, "IPMaturity"); DateTime? date1 = !string.IsNullOrEmpty(ipMaturity) ? Convert.ToDateTime(ipMaturity) : (DateTime?)null; if (date1.HasValue && date1 >= DateTime.Now) e.Row.Cells[4].BackColor = System.Drawing.Color.White; if (date1.HasValue && date1 < DateTime.Now) e.Row.Cells[4].BackColor = System.Drawing.Color.Red; } }
Now that we are using nullable DateTime
, the if
statements are updated to check for null
to avoid setting the background color until a value is provided.
I am not that familiar with DataBinder.Eval
so apologies if that does not return a string
, but hopefully the DateTime?
will set you in the right direction.
The field in the database will also need to be nullable if you need to save null
as a value in the database.
Method 2
I don’t have the necessary items to test it, but I think an If
statement testing it it’s null
would work. If that doesn’t work, I would use a try/catch
which I also put below. If you know the exception being thrown, it’d be nice to catch that.
If Code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { if (e.Row.RowType == DataControlRowType.DataRow) { if (e.Row.DataItem != null) { DateTime date1 = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "IPMaturity")); if (date1 >= DateTime.Now) e.Row.Cells[4].BackColor = System.Drawing.Color.White; if (date1 < DateTime.Now) e.Row.Cells[4].BackColor = System.Drawing.Color.Red; }else { // Data was null, so do something? } } }
Try/Catch Code:
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e) { try { if (e.Row.RowType == DataControlRowType.DataRow) { DateTime date1 = Convert.ToDateTime(DataBinder.Eval(e.Row.DataItem, "IPMaturity")); if (date1 >= DateTime.Now) e.Row.Cells[4].BackColor = System.Drawing.Color.White; if (date1 < DateTime.Now) e.Row.Cells[4].BackColor = System.Drawing.Color.Red; } } catch { // Gracefully handle the exception here } }
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0