Public/Global Variable recommendation for URL Param and SQL

  • Thread starter Thread starter Rob Wire
  • Start date Start date
R

Rob Wire

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
 
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_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


.

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
http://www.microsoft.com/info/cpyright.htm

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.
 
Back
Top