Solved

A way to insert a value into a SqlDataSource??

Posted on 2008-10-29
3
246 Views
Last Modified: 2012-06-21
Hi,

I am inserting a new record from the detailsview of insert mode.
I have a SqlDataSource to insert a new record.
If possible, I would like to insert a user's value coming from the WindowsIdentity.GetCurrentUser(),Name.
<asp:SqlDataSource ...
InsertCommand="Insert into table(type, title,name) values(@type,@title,'admin')
....</asp:SqlDataSource>

Instead of 'admin', I would like to grab the userID using WindowsIdentity.GetCurrentUser(),Name.
 and attach to that sqldatasource.
0
Comment
Question by:dkim18
[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
3 Comments
 
LVL 18

Accepted Solution

by:
carlnorrbom earned 500 total points
ID: 22838951
Hi,

I guess You could do like:

In code-behind, put the username in a session variable such as:

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Session("UserName") = System.Security.Principal.WindowsIdentity.GetCurrent().Name
    End Sub

In aspx page, declare a session parameter to pass the username to the sql datasource such as:

        <asp:SqlDataSource ID="SqlDataSource1" runat="server"
                           InsertCommand="INSERT INTO Table (Type, Title, Name) values (@Type,@Title,@UserName)"
                           InsertCommandType="Text">
            <InsertParameters>
                <asp:Parameter Name="Type" Type="String" />
                <asp:Parameter Name="Title" Type="String" />
                <asp:SessionParameter Name="UserName" Type="String" SessionField="UserName" />
            </InsertParameters>                          
        </asp:SqlDataSource>

That should do it.

/Carl.
0
 

Author Comment

by:dkim18
ID: 22854080
Thanks much Carl.

THat SessionParameter worked for me.
I don't know if that is proper way of doing thing but for now i think i will use that.
0
 
LVL 18

Expert Comment

by:carlnorrbom
ID: 22854116
Hi,

Well, as far as I know there is no other, more "proper" way of doing it. You need to to pass the value from the user context to a variable which can be consumed by the datasource, then you are limited to a number possible choices, using the SessionParameter is probably the easiest, most "clean" implementation.

/Carl.
0

Featured Post

Revamp Your Training Process

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action.

Question has a verified solution.

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

I recently went through the process of creating a Calendar Control of events with the basis of using a database to keep track of the dates that are selectable, one requirement was to have the selected date pop-up in a simple lightbox.  At first this…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
There's a multitude of different network monitoring solutions out there, and you're probably wondering what makes NetCrunch so special. It's completely agentless, but does let you create an agent, if you desire. It offers powerful scalability …
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…

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