Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    New to the CF scene
    Join Date
    Mar 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Code behind variable for use in <asp: / > Tags ???

    Hello,

    I am have been trying to use a variable from a code behind page in my asp page in order to check for a row on a sql server.

    I am getting the information to the codebehind page just fine and am able to display it on the page with no problems.

    My problem is when I try to use the variable into a sql statement or into an asp parameter, I am not able to access the value of the variable.

    here is my code:

    Code Behind:

    Code:
    Partial Class index
    
    Inherits System.Web.UI.Page
    
    'I have hard Coded the values into the variables for testing purposes
    
    
    Protected NAME As String = "Default"                  
    Protected USERID As Integer = 9999
    
    Protected REGION As String = "Default Region"
    
    Protected CORPID As Integer = 9
    
    Protected CORPNAME As String = "Dummy Corp"
    
    Protected CORTITLE As String = "Schlep"
    
    
    
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    
    NAME = Request.ServerVariables("HTTP.NAME")
    
    UserID = Request.ServerVariables("HTTP.UserID")
    
    Region = Request.ServerVariables("HTTP.Region")
    
    CorpID = Request.ServerVariables("HTTP.CorpID")
    
    CorpName = Request.ServerVariables("HTTP.CorpName")
    
    CorpTitle = Request.ServerVariables("HTTP.CorpTitle")
    
    ------------------------------------------------------------------------------------------------------------------
    
    'Cheesy code (This is the only way I have been able to get it to work)
    
    Me.TextBox1.Text = USERID
    
    Me.TextBox2.Text = NAME
    
    Me.TextBox1.Visible = False
    
    Me.TextBox2.Visible = False
    
    ' End of Cheesy work around.
    
    SqlDataSource1.Insert()
    
    'Testing Variables to web page
    
    Response.Write(NAME & USERID & CORPNAME)
    
     'Variable values display fine to page
    
    ------------------------------------------------------------------------------------------------------------------
    
    End Sub
    
    End Class

    Page Code:

    Code:
     'This page is nested into a Master Page
    
    <%@ Page Language="VB" MasterPageFile="~/MasterPage.master" AutoEventWireup="false" CodeFile="index.aspx.vb" Inherits="index" title="Untitled Page" %>
    
    'PlaceHolder
    
    <asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    
    
    
    
    
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:InsertConnectionString2 %>"
    
    InsertCommand="if not exists (select userID from dbo.UserStatistics where userID = @UserID)begin INSERT INTO dbo.UserStatistics(UserID,Name,corpID,corpName,visits,LastVisit)VALUES(@UserID ,@Name ,10,'Default Corp',0,getdate())end">
    
    <InsertParameters>
    
    <asp:ControlParameter Name = "UserID" ControlID = "TextBox1" />
    
    <asp:ControlParameter Name = "Name" ControlID = "TextBox2" />
    
    
    
    
    
    </InsertParameters>
    
    </asp:SqlDataSource>
    
    Sample Text on page.
    
    <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>   
    
    'More Testing of variables to the page   
    
    <%=NAME%>        <%=UserID %>      <%Response.Write(Corp)%>
    
    
    
    </asp:Content>

    'The above code works and does just what I want it to, it grabs a variable from the Code behind page and passes it into a Parameter, which is then used in an insertCommand

    in order to make this work, I am having to add a text box to the page, fill it with the value from the code behind page, Hide it, then pass the value to the insertcommand via a ControlParameter.

    This seems very cheesy to me, however when I attempt to use <%=Name%> to fill the parameter, I get casting errors, or I end up with "<%=Name%>" stored in my database.

    I guess what I am looking for is some advice as to a better way of doing this, it just does not make sence to me to do it this way, creating controls on the page, filling them, then hiding them from the page users just does not feel right.

    Is there a way to call the variables from within the <asp: /> tags ?



    Thanks in advance for any assistance.

    Swindla
    Last edited by vinyl-junkie; 03-22-2007 at 01:03 PM. Reason: Added code tags

  • #2
    Senior Coder nikkiH's Avatar
    Join Date
    Jun 2005
    Location
    Near Chicago, IL, USA
    Posts
    1,973
    Thanks
    1
    Thanked 32 Times in 31 Posts
    You've got a control parameter there.
    I think you wanted just a normal parameter. You can set the value on page load.
    http://pluralsight.com/blogs/fritz/a.../16/18054.aspx

    If this post contains any code, I may or may not have tested it. It's probably just example code, so no getting knickers in a bunch over a typo, OK? If it doesn't have basic error checking in it, such as object detection or checking if objects are null before using them, put that in there. I'm giving examples, not typing up your whole app for you. You run code at your own risk.
    Bored? Visit
    http://www.kaelisspace.com/


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •