Use variable from the code behind

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • skiabox
    New Member
    • Oct 2007
    • 6

    Use variable from the code behind

    I am using this variable from the code behind :



    Code:
    public string UserID; //globally declare the UserId 
        protected void RadGrid1_PreRender(object sender, EventArgs e)
        {
            MembershipUser myObject = Membership.GetUser();
            UserID = myObject.ProviderUserKey.ToString();    
        }
    I am trying to use the variable in the INSERT command in the aspx file but it seems that the following command does not work,any ideas why?

    Code:
    InsertCommand="INSERT INTO table (column1, column2) VALUES (value1, '<%=UserID %>')"

    Thanks!
  • MrMancunian
    Recognized Expert Contributor
    • Jul 2008
    • 569

    #2
    Originally posted by skiabox
    I am using this variable from the code behind :




    public string UserID; //globally declare the UserId
    protected void RadGrid1_PreRen der(object sender, EventArgs e)
    {
    MembershipUser myObject = Membership.GetU ser();
    UserID = myObject.Provid erUserKey.ToStr ing();
    }

    I am trying to use the variable in the INSERT command in the aspx file but it seems that the following command does not work,any ideas why?

    InsertCommand=" INSERT INTO table (column1, column2) VALUES (value1, '<%=UserID %>')"


    Thanks!
    The variable holding the userID is inserted as a string now, so it litterally tries to enter <%=UserID %>. You'll have to concatenate the value. Something like this:

    Code:
    InsertCommand="INSERT INTO table (column1, column2) VALUES (value1, '" & <%=UserID %> & "')"
    Steven

    Comment

    • skiabox
      New Member
      • Oct 2007
      • 6

      #3
      If I use this one it causes an error to the whole SqlDataSource tag!
      :(
      Thnx anyway!

      Comment

      • jkmyoung
        Recognized Expert Top Contributor
        • Mar 2006
        • 2057

        #4
        Assuming this is Java and not VB, use + instead of &.

        Comment

        • Plater
          Recognized Expert Expert
          • Apr 2007
          • 7872

          #5
          yes, try this:
          Code:
          InsertCommand="INSERT INTO table (column1, column2) VALUES (value1, '" + <%=UserID %> + "')"
          And if not, maybe this:
          Code:
          InsertCommand="INSERT INTO table (column1, column2) VALUES (value1, '" + UserID + "')"

          Comment

          Working...