?
Solved

how to put the current logged user on databound field

Posted on 2014-01-16
4
Medium Priority
?
390 Views
Last Modified: 2014-01-16
I have a details view with all databound fields

there is one field named ModifiedBy that I want to assign the current logged user to
<asp:BoundField DataField="ModifiedBy" HeaderText="Modified By" SortExpression="ModifiedBy" ApplyFormatInEditMode="True" ReadOnly="true" />

Open in new window


I am trying to do it this way in the code behind:

    Private Sub dvRecipeItem_DataBound(sender As Object, e As EventArgs) Handles dvRecipeItem.DataBound

        Dim strModifiedBy As String = System.Web.Security.Membership.GetUser.UserName.Trim
        CType(Me.dvRecipeItem.Rows(0).FindControl("ModifiedBy"), TextBox).Text = strModifiedBy
    End Sub

Open in new window


but I get a:

System.NullReferenceException: Object reference not set to an instance of an object

on line

CType(Me.dvRecipeItem.Rows(0).FindControl("ModifiedBy"), TextBox).Text = strModifiedBy


Would anyone be able to offer some help on what is my problem?

thank you
0
Comment
Question by:metropia
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
4 Comments
 
LVL 52

Expert Comment

by:Carl Tawn
ID: 39786273
I would imagine that it isn't able to find a control called "ModifiedBy" in the first row. Either that or it isn't able to convert it to a textbox.

Can you post the markup that is produced for that field at runtime? Either that or you could try something like:
CType(Me.dvRecipeItem.Rows(0).Cells(IndexOfYourBoundCell).Controls(0), TextBox).Text = strModifiedBy

Open in new window

0
 

Author Comment

by:metropia
ID: 39786320
here is the html:
<table id="ContentPlaceHolder1_dvRecipeItem">
	<tr><td>Id</td><td>8</td></tr>
	<tr><td>Item Number</td><td>10441</td></tr>
	<tr><td>VersionNumber</td><td>6</td></tr>
	<tr><td>Modified Date Time</td><td>7/12/2013 17:00:00</td></tr>
	<tr><td>Modified By</td><td>enard@clove.net</td></tr>
</table>

Open in new window


the aspx (right now the value comes from the entity data source
<Fields>
<asp:BoundField DataField="Id" HeaderText="Id" ReadOnly="True" SortExpression="Id" ApplyFormatInEditMode="True" />
<asp:BoundField DataField="ItemNumber" HeaderText="Item Number" SortExpression="ItemNumber" ApplyFormatInEditMode="True" />
<asp:BoundField DataField="VersionNumber" HeaderText="VersionNumber" SortExpression="VersionNumber" ApplyFormatInEditMode="True" />
<asp:BoundField DataField="ModifiedDateTime" HeaderText="Modified Date Time" SortExpression="ModifiedDateTime" ApplyFormatInEditMode="True" ReadOnly="true"/>
<asp:BoundField DataField="ModifiedBy" HeaderText="Modified By" SortExpression="ModifiedBy" ApplyFormatInEditMode="True" ReadOnly="true" />
</Fields>

Open in new window

vb.net code
Private Sub dvRecipeItem_DataBound(sender As Object, e As EventArgs) Handles dvRecipeItem.DataBound

Dim strModifiedBy As String = System.Web.Security.Membership.GetUser.UserName.Trim
CType(Me.dvRecipeItem.Rows(4).FindControl("ModifiedBy"), TextBox).Text = strModifiedBy

End Sub

Open in new window


Thank you.
0
 
LVL 52

Accepted Solution

by:
Carl Tawn earned 2000 total points
ID: 39786376
Well the markup would suggest that you aren't rendering a textbox at all. In which case you just need to change the text of the cell itself:
Me.dvRecipeItem.Rows(4).Cells(IndexOfYourCell).Value = strModifiedBy

Open in new window

0
 

Author Comment

by:metropia
ID: 39786403
Yes, that was the fix. although I had to replace .Value with .Text at the end

Thank you!
0

Featured Post

Linux Academy Android App Now Supports Chromecast

We have some fantastic news for our Android fans. We’re so excited to announce that the Linux Academy Android app is now available with Chromecast support. That’s right – simply download the latest update of the Linux Academy App and start casting your favorite course videos!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

The ECB site provides FX rates for major currencies since its inception in 1999 in the form of an XML feed. The files have the following format (reducted for brevity) (CODE) There are three files available HERE (http://www.ecb.europa.eu/stats/exch…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
In this video we outline the Physical Segments view of NetCrunch network monitor. By following this brief how-to video, you will be able to learn how NetCrunch visualizes your network, how granular is the information collected, as well as where to f…
In this video you will find out how to export Office 365 mailboxes using the built in eDiscovery tool. Bear in mind that although this method might be useful in some cases, using PST files as Office 365 backup is troublesome in a long run (more on t…
Suggested Courses

770 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question