locked
UserName not inserting RRS feed

  • Question

  • User-1938163999 posted

    I have a formview that is working fine for with the exception of the username is not inserting on a new record.  The username is visible on login so I know that is working.  I'm attaching my sql code as well as the code behind as I don't know what I'm missing.

    Thanks,

    Leesha

     <asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:Personal %>"
                                                DeleteCommand="DELETE FROM [tblPhotoOrderComments] WHERE [CommentID] = @CommentID"
                                                InsertCommand="INSERT INTO [tblPhotoOrderComments] ([Comment], [username], [CommentTimeStamp], [WeddingName]) VALUES (@Comment, @username, GetDate(), 'KaraRick')"
                                                SelectCommand="SELECT [Comment], [username], [CommentTimeStamp], [CommentID], [WeddingName] FROM [tblPhotoOrderComments]  WHERE ([Username] = @Username)" OnInserting="SqlDataSource1_Inserting"
                                                UpdateCommand="UPDATE [tblPhotoOrderComments] SET [Comment] = @Comment, [username] = @username, [CommentTimeStamp] = GetDate(), [WeddingName] = 'JoeyLee' WHERE [CommentID] = @CommentID">
                                                 <SelectParameters>
                                                    <asp:Parameter Name="UserName" Type="String" />
                                                </SelectParameters>
                                                <DeleteParameters>
                                                    <asp:Parameter Name="CommentID" Type="Int32" />
                                                </DeleteParameters>
                                                <UpdateParameters>
                                                    <asp:Parameter Name="Comment" Type="String" />
                                                    <asp:Parameter Name="username" Type="String" />
                                                    <asp:Parameter Name="CommentTimeStamp" Type="DateTime" />
                                                    <asp:Parameter Name="WeddingName" Type="String" />
                                                    <asp:Parameter Name="CommentID" Type="Int32" />
                                                </UpdateParameters>
                                                <InsertParameters>
                                                    <asp:Parameter Name="Comment" Type="String" />
                                                    <asp:Parameter Name="username" Type="String" />
                                                    <asp:Parameter Name="CommentTimeStamp" Type="DateTime" />
                                                    <asp:Parameter Name="WeddingName" Type="String" />
                                                </InsertParameters>
                                            </asp:SqlDataSource>  
    
    CODE BEHIND - 
    
     Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load
    
                  SqlDataSource4.SelectParameters("UserName").DefaultValue = HttpContext.Current.User.Identity.Name
        End Sub
       
        Protected Sub SqlDataSource4_Inserting(ByVal sender As Object, ByVal e As SqlDataSourceCommandEventArgs)
    
            e.Command.Parameters("@UserName").Value = HttpContext.Current.User.Identity.Name
    
        End Sub
    
    
    


     

    Wednesday, November 24, 2010 6:03 PM

Answers

  • User-1938163999 posted

    Ok, I finally figured out what you meant by this, fixed the sqlDataSource so the "OnInserting" now reads correctly (although instead of sqlDataSource4 its now 3 as I've redone the page in an effort to get it to work................but it still is not inserting the user name on edit.  Changes everything else, just not the username.  I've got a formvview that works fine and I keep looking back and forth at the code and can't find what is different on that. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 26, 2010 9:56 AM

All replies

  • User187056398 posted

    Case sensitive issue?

    <asp:Parameter Name="username"

    Should it be UserName? 

    Wednesday, November 24, 2010 6:22 PM
  • User-1938163999 posted

    That makes perfect sense, but alas it didn't work. I'm attaching my edited code in case there was something else.

    Leesha

    <asp:SqlDataSource ID="SqlDataSource4" runat="server" ConnectionString="<%$ ConnectionStrings:Personal %>"
                                                DeleteCommand="DELETE FROM [tblPhotoOrderComments] WHERE [CommentID] = @CommentID"
                                                InsertCommand="INSERT INTO [tblPhotoOrderComments] ([Comment], [username], [CommentTimeStamp], [WeddingName]) VALUES (@Comment, @UserName, GetDate(), 'KaraRick')"
                                                SelectCommand="SELECT [Comment], [username], [CommentTimeStamp], [CommentID], [WeddingName] FROM [tblPhotoOrderComments]  WHERE ([Username] = @Username)" OnInserting="SqlDataSource1_Inserting"
                                                UpdateCommand="UPDATE [tblPhotoOrderComments] SET [Comment] = @Comment, [username] = @username, [CommentTimeStamp] = GetDate(), [WeddingName] = 'JoeyLee' WHERE [CommentID] = @CommentID">
                                                 <SelectParameters>
                                                    <asp:Parameter Name="UserName" Type="String" />
                                                </SelectParameters>
                                                <DeleteParameters>
                                                    <asp:Parameter Name="CommentID" Type="Int32" />
                                                </DeleteParameters>
                                                <UpdateParameters>
                                                    <asp:Parameter Name="Comment" Type="String" />
                                                    <asp:Parameter Name="username" Type="String" />
                                                    <asp:Parameter Name="CommentTimeStamp" Type="DateTime" />
                                                    <asp:Parameter Name="WeddingName" Type="String" />
                                                    <asp:Parameter Name="CommentID" Type="Int32" />
                                                </UpdateParameters>
                                                <InsertParameters>
                                                    <asp:Parameter Name="Comment" Type="String" />
                                                    <asp:Parameter Name="UserName" Type="String" />
                                                    <asp:Parameter Name="CommentTimeStamp" Type="DateTime" />
                                                    <asp:Parameter Name="WeddingName" Type="String" />
                                                </InsertParameters>
                                            </asp:SqlDataSource>  


     

    Wednesday, November 24, 2010 8:59 PM
  • User187056398 posted

    Instead of this:
      e.Command.Parameters("@UserName").Value = HttpContext.Current.User.Identity.Name  

    You could try:
       SqlDataSource4.InsertParameters("@UserName").DefaultValue = HttpContext.Current.User.Identity.Name  

    (I don't have VS available right now to test these suggestions)

    You should also check the value of HttpContext.Current.User.Identity.Name

    Wednesday, November 24, 2010 10:33 PM
  • User-1938163999 posted

    Hi,

    That didn't do it either. Re checking the value ot HttpContext.Current.User.Identity.Name - I'm not sure how to do t hat except to verify that the user name is present in the LoginView Control and saves on other pages in the insert command.

    So confused,

    Leesha 

    Wednesday, November 24, 2010 11:00 PM
  • User187056398 posted

    checking the value ot HttpContext.Current.User.Identity.Name - I'm not sure how to do t hat
     

    Put a breakpoint on the line.  When the debugger hits it, use the watch window to examine the variable.

    Thursday, November 25, 2010 8:54 AM
  • User626880745 posted

  • Protected Sub SqlDataSource4_Inserting(ByVal sender As ObjectByVal e As SqlDataSourceCommandEventArgs)  
  •   
  •        e.Command.Parameters("@UserName").Value = HttpContext.Current.User.Identity.Name  
  •   
  •    End Sub  

I don't see SqlDataSource4_Inserting wired up, actually you've used:

ername)" OnInserting="SqlDataSource1_Inserting"


Thursday, November 25, 2010 12:27 PM
  • User-1469158370 posted

    Add a Data Key to the sql data source and try it.It will work

    Thursday, November 25, 2010 12:31 PM
  • User-1938163999 posted

    I'm not sure what this means or what I should do.   

    Thursday, November 25, 2010 3:00 PM
  • User-1938163999 posted

    This is the first time I've done this. I tried putting a breakpoint on the insert line in the sql code but never saw the brown circle or the line so instead I tried it on the code behind page. That seemed to work however I have no idea if I'm doing this correctly. I'm used to debugging in excel which is totall different. I hit the debug button but that only showed other errors with the site (ugh) not specific to this page. 

    Thursday, November 25, 2010 3:04 PM
  • User-1938163999 posted

    Ok, I finally figured out what you meant by this, fixed the sqlDataSource so the "OnInserting" now reads correctly (although instead of sqlDataSource4 its now 3 as I've redone the page in an effort to get it to work................but it still is not inserting the user name on edit.  Changes everything else, just not the username.  I've got a formvview that works fine and I keep looking back and forth at the code and can't find what is different on that. 

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, November 26, 2010 9:56 AM