Solved

how to put the current logged user on databound field

Posted on 2014-01-16
4
382 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

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

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 a recent question (https://www.experts-exchange.com/questions/29004105/Run-AutoHotkey-script-directly-from-Notepad.html) here at Experts Exchange, a member asked how to run an AutoHotkey script (.AHK) directly from Notepad++ (aka NPP). This video…

808 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