0

I need to access a session variable called "usuario" created from another class.

First, I have the following class called Default.aspx.vb

Protected Sub btnvalidar_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnvalidar.Click
    Dim fila As DataRow
    Dim nombre As String = ""

            For Each fila In tabla.Rows
                nombre = fila("referencia")
            Next
            Session.Add("usuario", nombre)

I need to access this variable from the following class called CConexion.vb

        _adaptador.SelectCommand = New MySqlCommand("select * from empresas where usuario=Session("usuario")", _conexion)
DavidM
  • 307
  • 2
  • 6
  • 22

1 Answers1

1

Your entire sql statement is enclosed in quotes. You want to add the session variable to the string instead:

"select * from empresas where usuario= '" + Session("usuario") +"'"

(You really should be using parameterized stored procedures)

TG01
  • 195
  • 2
  • 9
  • I did it but there is still an error in the variable, which is not declared, and I can't figure out how – DavidM Apr 05 '16 at 18:03
  • Session state may not be available in your CConexion.vb class. I can't tell for sure based on your code snippets. You may have to find another way to pass the variable over. Maybe add a property named usario in that class and pass the value in. – TG01 Apr 05 '16 at 18:14
  • Take a look at this answer: http://stackoverflow.com/questions/621549/how-to-access-session-variables-from-any-class-in-asp-net?rq=1 – TG01 Apr 05 '16 at 18:15