Solved

A way to insert a value into a SqlDataSource??

Posted on 2008-10-29
3
242 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
  • 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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Hidden Field Value 10 60
Allow space in this pattern 2 47
LINQ - C# to VB convertion 12 46
Syntax Error 2 44
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

21 Experts available now in Live!

Get 1:1 Help Now