Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > ASP .Net > Public/Global Variable recommendation for URL Param and SQL

Reply
Thread Tools

Public/Global Variable recommendation for URL Param and SQL

 
 
Rob Wire
Guest
Posts: n/a
 
      07-29-2003
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


 
Reply With Quote
 
 
 
 
Rick Rainey[MSFT]
Guest
Posts: n/a
 
      07-29-2003
>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
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
HTML::Template->param() : You gave me an odd number of parameters to param()! Dave Perl Misc 5 04-26-2011 02:44 AM
Overload by deriv class param; call w base class param ectoplasm C++ 12 07-28-2005 08:20 AM
URL param to XSL - request for a confirmation Emanuele D'Arrigo XML 3 11-29-2004 02:11 PM
Difference between xsl:param and xsl:variable? Karsten Weinert XML 3 07-05-2004 04:58 PM
XSLT: How to replace param name with this param's value ? Geathaa XML 2 07-30-2003 06:48 AM



Advertisments