Solved

A way to insert a value into a SqlDataSource??

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

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Lots of people ask this question on how to extend the “MembershipProvider” to make use of custom authentication like using existing database or make use of some other way of authentication. Many blogs show you how to extend the membership provider c…
Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…
This tutorial demonstrates a quick way of adding group price to multiple Magento products.

747 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

12 Experts available now in Live!

Get 1:1 Help Now