Subject: storing the login name in my personal database
Posted By: saif44 Post Date: 3/3/2006 2:06:08 PM
Hi folks,

does anyone know how to pick the login name that the user type to access the website and store it in a personal database.

what i did:

i took the 'login name' from the toolbox and add it to my page. once the person logs in, his name is displayed on the 'login name'. in my case i want to take that log in name and store it in one of my database tables. so what i did was as follow:

i took the 'username id' from the properties which is 'loginname1' and assigned it to this code:
objCmd.Parameters.Add("@RequestedBy", Data.SqlDbType.VarChar).Value = LoginName1.ToString
this then will be stored in my table under the 'requestedby' column. however, data has been successfully entered but when i checked the table, i found under the 'requedby' column the following name: System.Web.UI.WebControls.LoginName.
i have big fate that someone knows how to do this. can you please?

Reply By: jbenson001 Reply Date: 3/3/2006 3:01:36 PM
You are accessing the object itself, not the user name textbox.  Try:
objCmd.Parameters.Add("@RequestedBy", Data.SqlDbType.VarChar).Value = LoginName1.Username

Reply By: saif44 Reply Date: 3/3/2006 4:30:57 PM
thank you very much jbenson001. what i would like to say here is that the loginname1 is dragged from the toolbox under the login tab. if you check it you will only see that it has a id property and not a text property.

so the problem here is how can i get the name of the loginname1 assigned to my column after i submit the data.

thanks for all the helps.

Reply By: Imar Reply Date: 3/3/2006 7:07:46 PM
The LoginName control is just to do what its name implies: display the login name and nothing more. So, it doesn't have properties like UserName.

Instead, use this:

string userName = User.Identity.Name;

or

string userName = Membership.GetUser().UserName;

to get the user's name.

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Reply By: jbenson001 Reply Date: 3/3/2006 11:20:49 PM
A login control has a username textbox and password textbox, a loginview DOES NOT, as Imar has stated.  So, if you are using the login control, you can access it.  You are probably best using Imar's code.  This way when you get the name, you know the user has been authenticated.

Reply By: saif44 Reply Date: 3/4/2006 1:12:57 PM
thank you very much for everyone.

the following script has worked:
objCmd.Parameters.Add("@RequestedBy", Data.SqlDbType.VarChar).Value = user.identity.name


Reply By: shario Reply Date: 6/8/2006 3:50:36 PM
I am having the same issue, however, I am new to codeing.  Either I am an idiot or this is so simple it is staring me in the face.

Originally I wanted to extend the aspnet_User table.  Not an option.  I need to store much more information about each user.  So... I created an Accounts table with the fields I needed ( pkAccountID, FirstName, lastName, homePhone, etc).  I added a field to store their username (accountUsername, nvarchar(256)) just like the aspnet_User.UserName field.

Okay...now, after their account is created they are redirected to an account information page that is attached to this database with a Details View, (default view INSERT).  I need to pass the loginName from the aspnet_User table into the accountUserName field so that I can use that as a filter for them to add and update their information later.

I need to know in very basic terms what code I need, exactly where to put it, etc.  This post is helpful, but WHERE do I put the code

objCmd.Parameters.Add("@RequestedBy", Data.SqlDbType.VarChar).Value = LoginName1.Username

and/OR

string userName = Membership.GetUser().UserName;


Here is my code:
<body>
    <form id="form1" runat="server">
    <div>
        &nbsp;Welcome
        <asp:LoginName ID="lblLogInName" runat="server" />
        !<br />
        <br />
    </div>
        &nbsp; &nbsp; &nbsp;
        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="SqlDataSource1"
            DefaultMode="Insert" Height="50px" Width="125px" DataKeyNames="loginName" AutoGenerateInsertButton="True" DataMember="DefaultView" EmptyDataText="No Data">
            <Fields>
                <asp:TemplateField HeaderText="accountUserName" SortExpression="accountUserName">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("accountUserName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        <asp:Label ID="lblAcctUserName" runat="server" OnDataBinding="Page_Load" Text='<%# Eval("accountUserName") %>'></asp:Label>
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("accountUserName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="accountFirstName" HeaderText="accountFirstName" SortExpression="accountFirstName" />
                <asp:BoundField DataField="accountLastName" HeaderText="accountLastName" SortExpression="accountLastName" />
            </Fields>
        </asp:DetailsView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:connectionName %>"
            DeleteCommand="DELETE FROM [tblUserDetails] WHERE [accountID] = @accountID" InsertCommand="INSERT INTO tblUserDetails(accountUserName, accountFirstName, accountLastName) VALUES (@UserName, @accountFirstName, @accountLastName)"
            ProviderName="<%$ ConnectionStrings:connectionName.ProviderName %>"
            SelectCommand="SELECT accountUserName, accountFirstName, accountLastName FROM tblUserDetails"
            UpdateCommand="UPDATE [tblUserDetails] SET [accountUserName] = 'TEST', [accountFirstName] = @accountFirstName, [accountLastName] = @accountLastName WHERE [accountID] = @accountID">
            <DeleteParameters>
                <asp:Parameter Name="accountID" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="UserName" />
                <asp:Parameter Name="accountFirstName" Type="String"  />
                <asp:Parameter Name="accountLastName" Type="String" />
                <asp:Parameter Name="accountID" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="UserName"  DefaultValue = "Test"
            />
                <asp:Parameter Name="accountFirstName" Type="String" />
                <asp:Parameter Name="accountLastName" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>
        &nbsp;
    </form>
</body>

Once they leave this screen they will not go back, will never change their username, etc.  I only need to create an entry in this table ONE time so that I can use it for future.

Anyone who can help me would be soooo appreciated.  I am overdue on this project, my client is furious and I have searched high and low on specific instructions on how to do this for two days.

Thank you!!!



Reply By: Imar Reply Date: 6/8/2006 4:10:54 PM
Hi shario,

What you can do, is override the value of the UserName property inside the Inserting event of the DetailsView:

Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, _
   ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) _
   Handles DetailsView1.ItemInserting

  e.Values("UserName") = User.Identity.Name

End Sub

The Values collection holds a collection with name / value items thar are to be sent to the database. By accessing the UserName item you can override / set its value with the user's name.

Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Reply By: shario Reply Date: 6/9/2006 1:14:53 AM
Imar -

OMG!!! How much do I love you?  That worked perfectly.  I noticed that you answer a lot of postings and I want to thank you, thank you, thank you!

I started the next text and...Next challenge.  I thought when that when I had a field in the database that matched the username, I could use a WHERE clause in my database to filter (ie.  accountUserName = username)... This is not working.

Here is the scenario
  1. User creates account.
  2. Login lands at the form we just created and enters detailed  information in tblAccounts.  Username populates accountUserName field (thanks to you).  Submit takes them to a page accountDetails.aspx, a detailsview that is filtered by username.  
3.  User Logs out
4.  When User logs back in, the login takes them back to accountDetails.aspx filtered by the username.  They can edit details if needed. (WHERE accountUserName = username).

I created the accountDetails.aspx page and filtered the datasource by session field username.

Here is the code from the form...

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
        DataKeyNames="" DataSourceID="SqlDataSource1" Height="50px" Width="125px">
        <Fields>
            <asp:BoundField DataField="accountID" HeaderText="accountID" InsertVisible="False"
                ReadOnly="True" SortExpression="accountID" />
            <asp:BoundField DataField="accountUserName" HeaderText="accountUserName" SortExpression="accountUserName" />
            <asp:BoundField DataField="accountFirstName" HeaderText="accountFirstName" SortExpression="accountFirstName" />
            <asp:BoundField DataField="accountLastName" HeaderText="accountLastName" SortExpression="accountLastName" />
        </Fields>
    </asp:DetailsView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:connCPFAAEvents %>"
        DeleteCommand="DELETE FROM [tblUserDetails] WHERE [accountID] = @accountID" InsertCommand="INSERT INTO [tblUserDetails] ([accountUserName], [accountFirstName], [accountLastName]) VALUES (@accountUserName, @accountFirstName, @accountLastName)"
        ProviderName="<%$ ConnectionStrings:connectionName.ProviderName %>"
        SelectCommand="SELECT [accountID], [accountUserName], [accountFirstName], [accountLastName] FROM [tblUserDetails] WHERE ([accountUserName] = @accountUserName)"
        UpdateCommand="UPDATE [tblUserDetails] SET [accountUserName] = @accountUserName, [accountFirstName] = @accountFirstName, [accountLastName] = @accountLastName WHERE [accountID] = @accountID">
        <DeleteParameters>
            <asp:Parameter Name="accountID" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="accountUserName" Type="String" />
            <asp:Parameter Name="accountFirstName" Type="String" />
            <asp:Parameter Name="accountLastName" Type="String" />
            <asp:Parameter Name="accountID" Type="Int32" />
        </UpdateParameters>
        <SelectParameters>
            <asp:SessionParameter Name="accountUserName" SessionField="username" Type="String" />
        </SelectParameters>
        <InsertParameters>
            <asp:Parameter Name="accountUserName" Type="String" />
            <asp:Parameter Name="accountFirstName" Type="String" />
            <asp:Parameter Name="accountLastName" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>


Thank you again!






P.S. Just to clarify for visitors to this post, I am using code-behind.  All I had to do was open the webpage.aspx.vb file and paste the text into the page.

Reply By: Imar Reply Date: 6/9/2006 1:31:11 AM
Hi shario,

You're welcome. Glad it's working now.

Not sure if you are asking a new question, or explaining to others how you made it work. If it's a question, you seem to be using a Session field in the WHERE clause for the SELECT command. Are you sure that this session variable contains the user's name?

Alternatively, you can perform the same trick as I showed you earlier with the Selecting event:
  Protected Sub ObjectDataSource1_Selecting(ByVal sender As Object, _
          ByVal e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) _
          Handles ObjectDataSource1.Selecting
    e.InputParameters("UserName") = User.Identity.Name
  End Sub
Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Reply By: shario Reply Date: 6/9/2006 3:33:26 PM
Thank you again!  I pasted the code into the page.  I was using a sqldatasource, so I changed it to an ObjectDataSource to match your code.

1.  I'm not sure if the parameter needs to be in the DataSet if it is in the select query.
2.  I'm getting an error "End of Statement" expected.

Here is the new code.

<asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataKeyNames="accountID"
        DataSourceID="ObjectDataSource1" Height="50px" Width="125px">
        <Fields>
            <asp:BoundField DataField="accountID" HeaderText="accountID" InsertVisible="False"
                ReadOnly="True" SortExpression="accountID" />
            <asp:BoundField DataField="accountUserName" HeaderText="accountUserName" SortExpression="accountUserName" />
            <asp:BoundField DataField="accountFirstName" HeaderText="accountFirstName" SortExpression="accountFirstName" />
            <asp:BoundField DataField="accountLastName" HeaderText="accountLastName" SortExpression="accountLastName" />
        </Fields>
    </asp:DetailsView>
    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" DeleteMethod="Delete"
        InsertMethod="Insert" OldValuesParameterFormatString="original_{0}" SelectMethod="GetData"
        TypeName="dsAccountDetailsTableAdapters.tblUserDetailsTableAdapter" UpdateMethod="Update">
        <DeleteParameters>
            <asp:Parameter Name="Original_accountID" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="accountUserName" Type="String" />
            <asp:Parameter Name="accountFirstName" Type="String" />
            <asp:Parameter Name="accountLastName" Type="String" />
            <asp:Parameter Name="Original_accountID" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="accountUserName" Type="String" />
            <asp:Parameter Name="accountFirstName" Type="String" />
            <asp:Parameter Name="accountLastName" Type="String" />
        </InsertParameters>
    </asp:ObjectDataSource>



Reply By: Imar Reply Date: 6/9/2006 4:00:52 PM
Sorry about that. This should work equally well with a SqlDataSource:
Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, _
        ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) _
        Handles SqlDataSource1.Selecting
  e.Command.Parameters("UserName").Value = User.Identity.Name
End Sub
Where do you get the error you mentioned? At run time or at compile time?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Reply By: shario Reply Date: 6/9/2006 5:34:04 PM
I get the error at compile...

Reply By: Imar Reply Date: 6/10/2006 2:37:01 AM
Also with the Selecting event of a SqlDataSource?

Something else must be going wrong. Doesn't Visual Studio tell you where the error occurred, making it easier to see what went wrong?

Can you post your code?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Reply By: brenwyn Reply Date: 6/12/2006 4:16:49 PM
I tried the following code from this thread.

Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, _
   ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) _
   Handles DetailsView1.ItemInserting

  e.Values("UserName") = User.Identity.Name

End Sub

I am using C#, but I encountered an error telling me that e.Values is a property and I am using it as a method.  Did I miss something on the translation?

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
    {
        e.Values("postedBy") = Membership.GetUser().UserName;
    }

Reply By: Imar Reply Date: 6/12/2006 4:22:28 PM
e.Values("postedBy") is VB syntax for a collection indexer. In C#, the compiler thinks you're trying to call a method named Values and pass it the literal string "postedBy:.

Use [] instead:

e.Values["postedBy"]

Cheers,

Imar
Reply By: shario Reply Date: 6/14/2006 2:30:00 PM
Hi Imar!

I had to take a brain break from this.  The error comes up...

"End of Statement expected" Line 7, Column 51

The following underlined text is underlined in a blue squiggle:

    Protected Sub ObjectDataSource1_Selecting(ByVal sender As Object, _
              ByVal e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) _
              Handles ObjectDataSource1.Selecting e.InputParameters("UserName") = User.Identity.Name
    End Sub


Here is the page code:

    <form id="form1" runat="server">
    <div>
        &nbsp;Welcome
        <asp:LoginName ID="lblLogInName" runat="server" />
        !<br />
        <br />
    </div>
        &nbsp; &nbsp; &nbsp;
        <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False" DataSourceID="SqlDataSource1"
            DefaultMode="Insert" Height="50px" Width="125px" DataKeyNames="loginName" DataMember="DefaultView" EmptyDataText="No Data">
            <Fields>
                <asp:TemplateField HeaderText="accountUserName" SortExpression="accountUserName">
                    <EditItemTemplate>
                        <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("accountUserName") %>'></asp:TextBox>
                    </EditItemTemplate>
                    <InsertItemTemplate>
                        <asp:Label ID="lblAcctUserName" runat="server" OnDataBinding="Page_Load" Text='<%# Eval("accountUserName") %>'></asp:Label>
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:Label ID="Label1" runat="server" Text='<%# Bind("accountUserName") %>'></asp:Label>
                    </ItemTemplate>
                </asp:TemplateField>
                <asp:BoundField DataField="accountFirstName" HeaderText="accountFirstName" SortExpression="accountFirstName" />
                <asp:BoundField DataField="accountLastName" HeaderText="accountLastName" SortExpression="accountLastName" />
                <asp:TemplateField ShowHeader="False">
                    <InsertItemTemplate>
                        <asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Insert"
                            OnClick="Button1_Click" Text="Insert" />&nbsp;<asp:Button ID="Button2" runat="server"
                                CausesValidation="False" CommandName="Cancel" Text="Cancel" Visible="False" />
                    </InsertItemTemplate>
                    <ItemTemplate>
                        <asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="New"
                            Text="New" />
                    </ItemTemplate>
                </asp:TemplateField>
            </Fields>
        </asp:DetailsView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:connCPFAAEvents %>"
            DeleteCommand="DELETE FROM [tblUserDetails] WHERE [accountID] = @accountID" InsertCommand="INSERT INTO tblUserDetails(accountUserName, accountFirstName, accountLastName) VALUES (@UserName, @accountFirstName, @accountLastName)"
            ProviderName="<%$ ConnectionStrings:cpfaa.orgConnectionString1.ProviderName %>"
            SelectCommand="SELECT accountUserName, accountFirstName, accountLastName FROM tblUserDetails"
            UpdateCommand="UPDATE [tblUserDetails] SET [accountUserName] = 'TEST', [accountFirstName] = @accountFirstName, [accountLastName] = @accountLastName WHERE [accountID] = @accountID">
            <DeleteParameters>
                <asp:Parameter Name="accountID" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="UserName" />
                <asp:Parameter Name="accountFirstName" Type="String"  />
                <asp:Parameter Name="accountLastName" Type="String" />
                <asp:Parameter Name="accountID" Type="Int32" />
            </UpdateParameters>
            <InsertParameters>
                <asp:Parameter Name="UserName"  DefaultValue = "Test"/>
                <asp:Parameter Name="accountFirstName" Type="String" />
                <asp:Parameter Name="accountLastName" Type="String" />
            </InsertParameters>
        </asp:SqlDataSource>
        &nbsp;
    </form>

Reply By: Imar Reply Date: 6/14/2006 3:42:58 PM
You're missing a crucial break, between the method declaration and its implementation:

Protected Sub ObjectDataSource1_Selecting(ByVal sender As Object, _
    ByVal e As System.Web.UI.WebControls.ObjectDataSourceSelectingEventArgs) _
              Handles ObjectDataSource1.Selecting

  e.InputParameters("UserName") = User.Identity.Name

End Sub

Cheers,

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Reply By: shario Reply Date: 6/14/2006 4:11:08 PM
The Process -> They create account, land on page completeAccount.aspx where the username field is populated.  They fill in their details (First Name and Last Name for now).  On submit, they are redirected to page userDetails.aspx  where their details are confirmed.

Okay...so code for page completeAccount.aspx:

<asp:DetailsView ID="DetailsView1" runat="server" DataSourceID="SqlDataSource1" Height="50px"
        Width="125px" AutoGenerateRows="False" DefaultMode="Insert">
        <Fields>
            <asp:BoundField DataField="accountID" HeaderText="accountID" InsertVisible="False"
                ReadOnly="True" SortExpression="accountID" />
            <asp:BoundField DataField="userName" HeaderText="userName" SortExpression="userName" />
            <asp:BoundField DataField="accountFirstName" HeaderText="accountFirstName" SortExpression="accountFirstName" />
            <asp:BoundField DataField="accountLastName" HeaderText="accountLastName" SortExpression="accountLastName" />
            <asp:TemplateField ShowHeader="False">
                <InsertItemTemplate>
                    <asp:Button ID="Button1" runat="server" CausesValidation="True" CommandName="Insert"
                        OnClick="Button1_Click" Text="Insert" />&nbsp;<asp:Button ID="Button2" runat="server"
                            CausesValidation="False" CommandName="Cancel" Text="Cancel" />
                </InsertItemTemplate>
                <ItemTemplate>
                    <asp:Button ID="Button1" runat="server" CausesValidation="False" CommandName="New"
                        Text="New" />
                </ItemTemplate>
            </asp:TemplateField>
        </Fields>
    </asp:DetailsView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:conn %>"
        DeleteCommand="DELETE FROM [tblUserDetails] WHERE [accountID] = @accountID" InsertCommand="INSERT INTO [tblUserDetails] ([userName], [accountFirstName], [accountLastName]) VALUES (@userName, @accountFirstName, @accountLastName)"
        SelectCommand="SELECT [accountID], [userName], [accountFirstName], [accountLastName] FROM [tblUserDetails]"
        UpdateCommand="UPDATE [tblUserDetails] SET [userName] = @userName, [accountFirstName] = @accountFirstName, [accountLastName] = @accountLastName WHERE [accountID] = @accountID">
        <DeleteParameters>
            <asp:Parameter Name="accountID" Type="Int32" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="userName" Type="String" />
            <asp:Parameter Name="accountFirstName" Type="String" />
            <asp:Parameter Name="accountLastName" Type="String" />
            <asp:Parameter Name="accountID" Type="Int32" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="userName" Type="String" />
            <asp:Parameter Name="accountFirstName" Type="String" />
            <asp:Parameter Name="accountLastName" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>

CODE BEHIND for completeAccount.aspx

Partial Class adminTest_completeAccount
    Inherits System.Web.UI.Page

    Protected Sub DetailsView1_ItemInserting(ByVal sender As Object, _
       ByVal e As System.Web.UI.WebControls.DetailsViewInsertEventArgs) _
       Handles DetailsView1.ItemInserting
        e.Values("userName") = User.Identity.Name

    End Sub

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Response.Redirect("userDetails.aspx")
    End Sub
End Class

CODE FOR userDetails.aspx

Welcome Back
    <asp:LoginName ID="LoginName1" runat="server" />
    !<br />
    <br />
    <br />
    <asp:DetailsView ID="DetailsView1" runat="server" AutoGenerateRows="False"
        DataSourceID="SqlDataSource1" Height="50px" Width="125px">
    </asp:DetailsView>
    <br />
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues"
        ConnectionString="<%$ ConnectionStrings:conn %>" DeleteCommand="DELETE FROM [tblUserDetails] WHERE [accountID] = @original_accountID AND [userName] = @original_userName AND [accountFirstName] = @original_accountFirstName AND [accountLastName] = @original_accountLastName"
        InsertCommand="INSERT INTO [tblUserDetails] ([userName], [accountFirstName], [accountLastName]) VALUES (@userName, @accountFirstName, @accountLastName)"
        OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT [accountID], [userName], [accountFirstName], [accountLastName] FROM [tblUserDetails]"
        UpdateCommand="UPDATE [tblUserDetails] SET [userName] = @userName, [accountFirstName] = @accountFirstName, [accountLastName] = @accountLastName WHERE [accountID] = @original_accountID AND [userName] = @original_userName AND [accountFirstName] = @original_accountFirstName AND [accountLastName] = @original_accountLastName">
        <DeleteParameters>
            <asp:Parameter Name="original_accountID" Type="Int32" />
            <asp:Parameter Name="original_userName" Type="String" />
            <asp:Parameter Name="original_accountFirstName" Type="String" />
            <asp:Parameter Name="original_accountLastName" Type="String" />
        </DeleteParameters>
        <UpdateParameters>
            <asp:Parameter Name="userName" Type="String" />
            <asp:Parameter Name="accountFirstName" Type="String" />
            <asp:Parameter Name="accountLastName" Type="String" />
            <asp:Parameter Name="original_accountID" Type="Int32" />
            <asp:Parameter Name="original_userName" Type="String" />
            <asp:Parameter Name="original_accountFirstName" Type="String" />
            <asp:Parameter Name="original_accountLastName" Type="String" />
        </UpdateParameters>
        <InsertParameters>
            <asp:Parameter Name="userName" Type="String" />
            <asp:Parameter Name="accountFirstName" Type="String" />
            <asp:Parameter Name="accountLastName" Type="String" />
        </InsertParameters>
    </asp:SqlDataSource>

CODE BEHIND FOR userDetails.aspx


Partial Class adminTest_userDetails
    Inherits System.Web.UI.Page


    Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting
        e.Command.Parameters("UserName").Value = User.Identity.Name
    End Sub

End Class


Thanks Again!

Reply By: Imar Reply Date: 6/14/2006 4:16:56 PM
You're welcome. Does it work now?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Reply By: shario Reply Date: 6/14/2006 4:42:47 PM
Sorry, Imar, you've been so helpful!

No, it isn't working.  The code I posted does not include the parameter from the page completeAccount.aspx to userDetails.aspx.  I added the parameter to the redirect code (response.redirect(userDetails.aspx?username={0}).  When I did this, I get an exception.

I am using the SQLDataSource code....

    Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, _
            ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) _
            Handles SqlDataSource1.Selecting
        e.Command.Parameters("UserName").Value = User.Identity.Name
    End Sub

This is the exception error:

System.IndexOutOfRangeException was unhandled by user code
  Message="An SqlParameter with ParameterName 'UserName' is not contained by this SqlParameterCollection."
  Source="System.Data"
  StackTrace:
       at System.Data.SqlClient.SqlParameterCollection.GetParameter(String parameterName)
       at System.Data.Common.DbParameterCollection.get_Item(String parameterName)
       at adminTest_userDetails.SqlDataSource1_Selecting(Object sender, SqlDataSourceSelectingEventArgs e) in C:\Documents and Settings\shari.PEAKBIZ\My Documents\Visual Studio 2005\WebSites\cpfaa\adminTest\userDetails.aspx.vb:line 8
       at System.Web.UI.WebControls.SqlDataSourceView.OnSelecting(SqlDataSourceSelectingEventArgs e)
       at System.Web.UI.WebControls.SqlDataSourceView.ExecuteSelect(DataSourceSelectArguments arguments)
       at System.Web.UI.DataSourceView.Select(DataSourceSelectArguments arguments, DataSourceViewSelectCallback callback)
       at System.Web.UI.WebControls.DataBoundControl.PerformSelect()
       at System.Web.UI.WebControls.BaseDataBoundControl.DataBind()
       at System.Web.UI.WebControls.DetailsView.DataBind()
       at System.Web.UI.WebControls.BaseDataBoundControl.EnsureDataBound()
       at System.Web.UI.WebControls.DetailsView.EnsureDataBound()
       at System.Web.UI.WebControls.CompositeDataBoundControl.CreateChildControls()
       at System.Web.UI.Control.EnsureChildControls()
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Control.PreRenderRecursiveInternal()
       at System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint)


Reply By: shario Reply Date: 6/14/2006 6:06:31 PM
Maybe the problem is in the select query...I've tried it as a session, as a querystring...several of the options and none have worked.  What should this be if it is based on the selecting event code you provided?

        <SelectParameters>
            <asp:SessionParameter Name="userName" SessionField="username" Type="String" />
        </SelectParameters>

Reply By: shario Reply Date: 6/17/2006 2:28:28 PM
Hi Imar -

Have you had a chance to look over the new post, or does anyone else have an idea?  Thank you!

Reply By: Imar Reply Date: 6/18/2006 7:17:30 AM
Are you storing the user name in a Session field called UserName? If not, this code will never work.

Did you try a normal parameter, like this:

<asp:Parameter Name="UserName" />

in combination with the selecting event?

Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
While typing this post, I was listening to: Mantra by Tool (Track 4 from the album: Lateralus) What's This?
Reply By: saf Reply Date: 12/7/2006 12:54:42 PM
Hi Imar

I need to do something similar and I know the answer is in this post somewhere I just can't seem to put it in the right place..

All I want to do is use the currently logged in user's name (user.identity.name) in an sqldatasource control as a select parameter (e.g where studentname = user.identity.name) I can't figure out how to code the query to use this????  I searched and read posts on using expression builders and created custom parameters but there must be an easier way.  only authenticated users get to my page, user.identity.name returns the name I want to query my database by....please help.

saf

Reply By: Imar Reply Date: 12/7/2006 4:47:58 PM
Hi saf,

Did you try my suggestion from my last post in this thread? E.g. set up a param like this:

<asp:Parameter Name="UserName" />

and then use the Selecting event of the data source to fill that parameter with the user's name?

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.
Reply By: saf Reply Date: 12/7/2006 7:59:06 PM
Afraid I'm having the same problem as shario. I get error message:

An SqlParameter with ParameterName 'SchoolName' is not contained by this SqlParameterCollection.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.IndexOutOfRangeException: An SqlParameter with ParameterName 'SchoolName' is not contained by this SqlParameterCollection.

Source Error:


Line 20:
Line 21:     Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) Handles SqlDataSource1.Selecting
Line 22:         e.Command.Parameters("SchoolName").Value = User.Identity.Name
Line 23:
Line 24:     End Sub

What am I doing wrong???.  Here is my sqldatasource:

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
        ConnectionString="<%$ ConnectionStrings:LunchOrderConnectionString %>" SelectCommand="SELECT [CategoryName], [CategoryItemName], [Price] FROM [vw_OrderForm] WHERE ([SchoolName] = @SchoolName)">
        <SelectParameters>
            <asp:Parameter Name="SchoolName" Type="String" />
           
        </SelectParameters>
    </asp:SqlDataSource>

As always, I appreciate your time and patience....

Reply By: saf Reply Date: 12/8/2006 2:57:03 AM
Nevermind....

I just used a hidden field valued with user.identity.name instead.  Works just the same...Can't believe I didn't think of that 10 hours ago....

Thanks anyway

Reply By: Imar Reply Date: 12/8/2006 7:13:56 AM
I just played around with this a little, and it seems that for SQL parameters you need to prefix the name with an @ symbol, just as in the query. The following code worked for me:
<asp:SqlDataSource ID="SqlDataSource1" runat="server" 
  ConnectionString="YourConnectionString"
  SelectCommand="SELECT [CreateAuthor] FROM [Content] WHERE ([CreateAuthor] = @CreateAuthor)">
  <SelectParameters>
    <asp:Parameter Name="CreateAuthor" Type="Object" />
  </SelectParameters>
</asp:SqlDataSource>
Then fill the parameter in the Selecting event like this:
Protected Sub SqlDataSource1_Selecting(ByVal sender As Object, _
      ByVal e As System.Web.UI.WebControls.SqlDataSourceSelectingEventArgs) _
      Handles SqlDataSource1.Selecting

  e.Command.Parameters.Item("@CreateAuthor").Value = _
      Membership.GetUser().ProviderUserKey

End Sub
Instead of the Guid from Membership.GetUser().ProviderUserKey you can of course have a String parameter and fill it with  User.Identity.Name.


Hope this helps,

Imar
---------------------------------------
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Everyone is unique, except for me.
Author of ASP.NET 2.0 Instant Results and Beginning Dreamweaver MX / MX 2004
Want to be my colleague? Then check out this post.

Go to topic 52559

Return to index page 98
Return to index page 97
Return to index page 96
Return to index page 95
Return to index page 94
Return to index page 93
Return to index page 92
Return to index page 91
Return to index page 90
Return to index page 89