Solved

A way to insert a value into a SqlDataSource??

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

Online Training Solution

Drastically shorten your training time with WalkMe's advanced online training solution that Guides your trainees to action. Forget about retraining and skyrocket knowledge retention rates.

Question has a verified solution.

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

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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…

726 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