I have a field (ldate) in a table that typically displays data in the following format:
2007-11-12 20:30:47.000
In the front end I want the time and the date separately. But as I try to cast it to a datetime object it is giving me a System.NullReferenceException. What I tried to do was just this:
Protected Sub gvTopics_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs) Handles gvTopics.RowDataBound Dim img As Image = gvTopics.FindControl("imgIcon") Dim lbDate, lbTime As Label lbDate = gvTopics.FindControl("lbDate") lbTime = gvTopics.FindControl("lbTime") Dim dt As New DateTime If e.Row.RowType = DataControlRowType.DataRow Then Debug.Print("parsed = {0}", DateTime.Parse(e.Row.DataItem("ldate")).ToShortTimeString) 'this works!!! dt = DateTime.Parse(e.Row.DataItem("ldate")) lbDate.Text = dt.ToShortDateString 'throws error at this line lbTime.Text = dt.ToShortTimeString End If End Sub
Is there a solution where in I can extract the time and date from inside this event procedure itself?
programming blog
![]() |
0 |
![]() |
My guess is that gvTopics.FindControl("lbDate") is not able to find that control. And, hence lbDate is never referenced an object. As a result, when you are trying to access lbDate.Text, it is giving NullReferenceException.
- Surya
![]() |
0 |
![]() |
deostroll:
In the front end I want the time and the date separatelyuse this sqlquery to retrieve only time
SELECT use this query to retrieve only date SELECT CONVERT(VARCHAR(10),GETDATE(),111) store the result of this query in a ctrl and dispaly it in your code, set a breakpoint where you are getting error and trace out the error to find out whether the lbdate.text got any values or not |
![]() |
0 |
![]() |
dtnetDeveloper: seems you are correct. The markup for my gridview is as follows.
1 <asp:GridView ID="gvTopics" runat="server" AutoGenerateColumns="False" CssClass="gvclass">
2 <Columns>
3 <asp:TemplateField>
4 <ItemTemplate>
5 <asp:Image ID="imgIcon" runat="server" />
6 </ItemTemplate>
7 </asp:TemplateField>
8 <asp:TemplateField HeaderText="subject">
9 <ItemTemplate>
10 <asp:HyperLink Font-Names="Verdana, Helvetica, Arail" Font-Size="8pt" ID="hlnkPost" 11 runat="server">HyperLink</asp:HyperLink><br />
12 <asp:Label ID="lbStatDate" Font-Size="7pt" runat="server" Text="Label"></asp:Label>
13 </ItemTemplate>
14 </asp:TemplateField>
15 <asp:BoundField DataField="sby" HeaderText="author" />
16 <asp:BoundField DataField="PostsCount" HeaderText="posts" />
17 <asp:TemplateField HeaderText="last update">
18 <ItemTemplate>
19 <asp:Label ID="lblDate" runat="server" Text="Label"></asp:Label>
20 <br />
21 <asp:Label ID="lblTime" runat="server" Text="Label"></asp:Label>
22 </ItemTemplate>
23 </asp:TemplateField>
24 </Columns>
25 </asp:GridView>Can you tell me how to access the controls inside of them... I have not had that much luck.
programming blog
![]() |
0 |
![]() |
RowDataBound is the wrong event to do the FindControl in. Why? Because the controls have not been rendered yet.
Are you using a Codebehind DataSet or Source? If so, try using the ItemCreated event instead.
Are you using a <asp:SqlDataSource>?? If so, it's really simple in that you can use the same datacolumn twice like your code behind as follows:
<asp:Label runat="server" ID="lbDate" Text='<%# DataBinder.Eval(Container.DataItem, "Date", "{0:d}")%>' />
<asp:Label runat="server" ID="lbTime" Text='<%# DataBinder.Eval(Container.DataItem, "Date", "{0:t}")%>' />
If this post helped you, Mark As Answer.
![]() |
0 |
![]() |
deostroll:
Can you tell me how to access the controls inside of them...For Each row As GridViewRow In gvtopics.Rows Dim mylbldate As label = DirectCast(row.FindControl("lbldate"), label)
dim mylbltime as label =DirectCast(row.FindControl("lbltime"), label)
End If
Mark as Answer if any posts helps you
To Make your dreams come true, Don't Sleep
Remember that no person is an island. Communicate your thoughts and desires honestly, and encourage others to communicate honestly with you. Practice understanding and motivating other people.
Cheers,
RR
![]() |
0 |
![]() |
Hi deostroll,
If you use gvTopics.FindControl("lbDate") in RowDataBound method , you will always get null reference. You can use e.Row.FindControl("controlid") instead.
See my sample,
1 Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) 2 If Not IsPostBack Then 3 Dim table As New DataTable() 4 table.Columns.Add("time") 5 Dim dr As DataRow = table.NewRow() 6 dr("time") = "7-11-12" 7 table.Rows.Add(dr) 8 Me.GridView1.DataSource = table 9 GridView1.DataBind() 10 End If 11 12 13 End Sub 14 15 Protected Sub GridView1_RowDataBound(ByVal sender As Object, ByVal e As GridViewRowEventArgs) 16 17 18 If e.Row.RowType = DataControlRowType.DataRow Then 19 Dim test As Label = DirectCast(Me.GridView1.FindControl("lbDate"), Label) 20 'null 21 Dim lbl As Label = DirectCast(e.Row.FindControl("lbDate"), Label) 22 23 lbl.Text = DirectCast(e.Row.DataItem, DataRowView)("time").ToString() 24 End If 25 End Sub
1 <asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" Width="343px" OnRowDataBound="GridView1_RowDataBound"> 2 <Columns> 3 <asp:TemplateField> 4 <ItemTemplate> 5 <asp:Label ID="lbDate" runat="server" Text="Label"></asp:Label> 6 </ItemTemplate> 7 </asp:TemplateField> 8 </Columns> 9 </asp:GridView>
Samu Zhang
Microsoft Online Community Support
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question.
![]() |
0 |
![]() |
There is another less intuitive way. It goes like this:
dim lbl as Label = e.Row.Cells(0).Controls(0)
The only problem is the index of Controls(). I had to find the exact index through debugging, and checking what was the type of each control in each cell.
programming blog
![]() |
0 |
![]() |
deostroll:
There is another less intuitive way. It goes like this:
dim lbl as Label = e.Row.Cells(0).Controls(0)
The only problem is the index with Controls(). I had to find the exact index through debugging, and checking what was the type of each control in each cell.
I would avoid this approach like the plague! It is very prone to breaking. If a new column gets added to the display (with a lower index number), all your control logic becomes invalidated. It's nasty to fix.
![]() |
0 |
![]() |