Solved

how to put the current logged user on databound field

Posted on 2014-01-16
4
374 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
  • 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 500 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Parsing a CSV file is a task that we are confronted with regularly, and although there are a vast number of means to do this, as a newbie, the field can be confusing and the tools can seem complex. A simple solution to parsing a customized CSV fi…
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…
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…

707 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

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now