Solved

how to put the current logged user on databound field

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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

773 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