Session variable as parameter value

Results 1 to 2 of 2

Thread: Session variable as parameter value

  1. #1
    Join Date
    Dec 1969

    Default Session variable as parameter value

    When the user log ito the site the login procedure create a set of session variables. One of them is Session("CompanyCode") which returns properly in a label in my page.<BR><BR>I have a simple stored procedure like this:<BR><BR>&#060;code&#062;<BR>CREATE PROCEDURE SelectPrices<BR>(@CompanyCode NVARCHAR (10))<BR>AS<BR><BR>SELECT * FROM tblPrices<BR>WHERE CompanyCode=@CompanyCode<BR>GO<BR>&#060;/code&#062;<BR><BR>Then I want to fill a datagrid with the company prices and want to use the Session("CompanyCode") variable as <BR><BR>the parameter value. <BR><BR>But the "Catch objError as Exception" returns the following error message:<BR><BR>Error accessing database.<BR>Object reference not set to an instance of an object.<BR>WebVB <BR>MSK <BR><BR>MSK being the Session("CompanyCode") value.<BR><BR><BR>Here is my code:<BR>&#060;code&#062;<BR>Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load<BR><BR>Dim myConnection As SqlConnection<BR> myConnection = New SqlConnection(ConfigurationSettings.AppSettings("M yConn"))<BR> Dim myCommand As SqlCommand<BR> myCommand = New SqlCommand("SelectPrices", myConnection)<BR> myCommand.CommandText = "SelectPrices"<BR> myCommand.CommandType = CommandType.StoredProcedure<BR><BR> Dim sp As SqlParameter = myCommand.Parameters.Add(New SqlParameter("@CompanyCode", SqlDbType.NVarChar, 10))<BR> sp.Value = Session("CompanyCode")<BR><BR> Try<BR> myConnection.Open()<BR> PriceGrid.DataSource = myCommand.ExecuteReader()<BR> PriceGrid.DataBind()<BR> myConnection.Close()<BR><BR> Catch objError As Exception<BR><BR> ErrorMsg.InnerHtml = "Error accessing database.&#060;br /&#062;" _<BR> & objError.Message & "&#060;br /&#062;" & objError.Source<BR> Exit Sub<BR> End Try<BR> End Sub<BR>&#060;/code&#062;<BR>I have also try to refer to the object in assingning the Session variable to a label(CompanyCode) and changing <BR><BR>the parameter value like this :<BR>&#060;code&#062;<BR>sp.value = CompanyCode.Text<BR>&#060;/code&#062;<BR>But I receive the same error message.<BR><BR>Any Idea how to pass the session variable to the parameter??<BR><BR>Rgds.<BR><BR>Plm

  2. #2
    Join Date
    Dec 1969

    Default RE: Session variable as parameter value

    This is how I would do it.<BR><BR> Dim oParams As OleDbParameterCollection = oCmd.Parameters<BR> oParams.Add("@CompanyCode", OleDbType.VarChar, 50)<BR> oParams("@CompanyCode").Value = Session("CompanyCode")<BR>

Posting Permissions

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