Public/Global Variable recommendation for URL Param and SQL

Ask a Question related to ASP.NET General, Design and Development.

  1. #1

    Default Public/Global Variable recommendation for URL Param and SQL

    Please let me know the preferred way to do the following.
    Accept a Variable in the calling page's URL of an ID. Set
    that variable to be global scope of the class so that it
    can be used throughout the functions from Page Load.
    Would like to send the ID variable as a SQL parameter to
    stored procedures for Select, Update, and Insert.

    What I have done so far to get around it is declare it in
    the SQL Select statements of the Web form code, then
    create a new parameter for the stored procedure, then edit
    the value to have the variable. However, Visual Studio
    keeps overwriting this code with the GUI tools, just
    looking at the properties.

    Please advise. Thank you, Rob
    ----
    Public Class WebForm1
    Inherits System.Web.UI.Page

    'RW - Global Employee ID to be used for URL EmployeeID
    Parameter.
    Public EmployeeID

    #Region " Web Form Designer Generated Code "
    ....
    '
    'SqlSelectCommand1
    '
    'RW - Attempt to globally define EmployeeID from
    URL paramater for stored procedure
    If Not (Request.Params("EmployeeID") Is Nothing)
    Then
    EmployeeID = Int32.Parse(Request.Params
    ("EmployeeID"))
    Else
    EmployeeID = "302"
    End If
    '/RW
    Me.SqlSelectCommand1.CommandText
    = "[spGetEmpAttach]"
    Me.SqlSelectCommand1.CommandType =
    System.Data.CommandType.StoredProcedure
    Me.SqlSelectCommand1.Connection = Me.SqlConnection1
    Me.SqlSelectCommand1.Parameters.Add(New
    System.Data.SqlClient.SqlParameter("@RETURN_VALUE" ,
    System.Data.SqlDbType.Int, 4,
    System.Data.ParameterDirection.ReturnValue, False, CType
    (0, Byte), CType(0, Byte), "",
    System.Data.DataRowVersion.Current, Nothing))
    'RW
    Me.SqlSelectCommand1.Parameters.Add(New
    System.Data.SqlClient.SqlParameter("@EmployeeID",
    System.Data.SqlDbType.Int, 4,
    System.Data.ParameterDirection.Input, False, CType(0,
    Byte), CType(0, Byte), "",
    System.Data.DataRowVersion.Current, EmployeeID))
    '/RW


    Rob Wire Guest

  2. Similar Questions and Discussions

    1. global variable
      I have made a site where I need to be able to include a file containing an array of settings in many pages. This array needs to have global scope...
    2. global variable problem
      I want a counter progCounter to count the number of times that the frame is entered. Ultimately, I would like it to count the number of times that...
    3. Global variable and tempo
      set the member of sprite 10 to member("loopValSel") or sprite(10).member=member("loopValSel") hth -- ---------------- -- Ned...
    4. Global Variable declaration
      For some reason this wont compile, it says that i need to declare the variable before using it on prepareMovie global sm204
    5. Help Please: 'Warning 5001 global variable "iF_timer" already defined in global scope
      Hi there, I have just started to get this error: 'Warning 5001 global variable "iF_timer" already defined in global scope The variable name...
  3. #2

    Default RE: Public/Global Variable recommendation for URL Param and SQL

    >I've started trying to define the following, but get
    >a "Specified cast is not valid." error on the SQL
    >parameter. Am I on the right track? Thank you in
    >advance, Rob.
    >---
    > Private Sub Page_Load(ByVal sender As System.Object,
    >ByVal e As System.EventArgs) Handles MyBase.Load
    > 'RW - Session state for insert function, then
    >fills the dataset with stored procedure
    > GetURL()
    > If IsPostBack Then
    > DsAttachmentDtls1 = CType(Session
    >("SessionAttachmentDtls"), dsAttachmentDtls)
    > Else
    > SqlDataAdapter1.Fill(DsAttachmentDtls1)
    > Session("SessionAttachmentDtls") =
    >DsAttachmentDtls1
    > DataGrid1.DataBind()
    > End If
    > End Sub
    > 'RW - Global Employee ID to be used for URL EmployeeID
    >Parameter.
    > Public EmployeeID
    > Public Sub GetURL()
    > 'RW - Attempt to globally define EmployeeID from
    >URL paramater for stored procedure
    > If Not (Request.Params("EmployeeID") Is Nothing)
    >Then
    > EmployeeID = Request.Params("EmployeeID")
    > Else
    > EmployeeID = "302"
    > End If
    > '/RW
    > Me.SqlSelectCommand1.Parameters.Item
    >("@EmployeeID") = CType(EmployeeID,
    >System.Data.SqlClient.SqlParameter)
    > End Sub
    >
    >>-----Original Message-----
    >>Please let me know the preferred way to do the
    >following.
    >>Accept a Variable in the calling page's URL of an ID.
    >Set
    >>that variable to be global scope of the class so that it
    >>can be used throughout the functions from Page Load.
    >>Would like to send the ID variable as a SQL parameter to
    >>stored procedures for Select, Update, and Insert.
    >>
    >>What I have done so far to get around it is declare it in
    >>the SQL Select statements of the Web form code, then
    >>create a new parameter for the stored procedure, then
    >edit
    >>the value to have the variable. However, Visual Studio
    >>keeps overwriting this code with the GUI tools, just
    >>looking at the properties.
    >>
    >>Please advise. Thank you, Rob
    >>----
    >>Public Class WebForm1
    >> Inherits System.Web.UI.Page
    >>
    >> 'RW - Global Employee ID to be used for URL
    >EmployeeID
    >>Parameter.
    >> Public EmployeeID
    >>
    >>#Region " Web Form Designer Generated Code "
    >>....
    >> '
    >> 'SqlSelectCommand1
    >> '
    >> 'RW - Attempt to globally define EmployeeID from
    >>URL paramater for stored procedure
    >> If Not (Request.Params("EmployeeID") Is Nothing)
    >>Then
    >> EmployeeID = Int32.Parse(Request.Params
    >>("EmployeeID"))
    >> Else
    >> EmployeeID = "302"
    >> End If
    >> '/RW
    >> Me.SqlSelectCommand1.CommandText
    >>= "[spGetEmpAttach]"
    >> Me.SqlSelectCommand1.CommandType =
    >>System.Data.CommandType.StoredProcedure
    >> Me.SqlSelectCommand1.Connection =
    >Me.SqlConnection1
    >> Me.SqlSelectCommand1.Parameters.Add(New
    >>System.Data.SqlClient.SqlParameter("@RETURN_VALU E",
    >>System.Data.SqlDbType.Int, 4,
    >>System.Data.ParameterDirection.ReturnValue, False, CType
    >>(0, Byte), CType(0, Byte), "",
    >>System.Data.DataRowVersion.Current, Nothing))
    >> 'RW
    >> Me.SqlSelectCommand1.Parameters.Add(New
    >>System.Data.SqlClient.SqlParameter("@EmployeeID" ,
    >>System.Data.SqlDbType.Int, 4,
    >>System.Data.ParameterDirection.Input, False, CType(0,
    >>Byte), CType(0, Byte), "",
    >>System.Data.DataRowVersion.Current, EmployeeID))
    >> '/RW
    >>
    >>
    >>.
    >>
    >
    Hi Rob,

    Regarding the casting error; I assume you're getting the error on this line:
    > Me.SqlSelectCommand1.Parameters.Item("@EmployeeID" ) = CType(EmployeeID, System.Data.SqlClient.SqlParameter)
    In your CType function call, you need to specify a particular SqlDBType. Assuming EmployeeID is going to be an integer, then you will need to change you
    code as follows:
    > Me.SqlSelectCommand1.Parameters.Item("@EmployeeID" ) = CType(EmployeeID, System.Data.SqlClient.SqlParameter.SqlDbType.Int)

    Thanks,
    Rick[MSFT]
    --

    This posting is provided "AS IS" with no warranties, and confers no rights. Use of included script samples are subject to the terms specified at
    [url]http://www.microsoft.com/info/cpyright.htm[/url]

    Note: For the benefit of the community-at-large, all responses to this message are best directed to the newsgroup/thread from which they originated.

    Rick Rainey[MSFT] Guest

Posting Permissions

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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139