Problems when performing a dynamic query

0

This is my code and I want to dynamically make a query to my database without having to put values to the parameters department and university since that data I get from a dropdownlist, as I could in that case I am working in visual studio

protected void Page_Load(object sender, EventArgs e)
        {
            DataTable Tabla;
            if (!IsPostBack)
            {
                //SqlConnection conexionSQL = new SqlConnection("Data Source=(localdb)\bdprueba; Initial Catalog=BDUbicaciones;Integrated Security=True");
                SqlConnection conexionSQL = new SqlConnection("Data Source=192.168.1.107;Initial Catalog=GestionVoluntarios;User ID=st;Password=juli12");
                SqlCommand cmd = new SqlCommand();
                cmd.CommandText = "select id,Latitud,Longitud,Nombres,Apellidos,Direccion,(select Nombre from dbo.tbwbDepartamento  where id=Departamento)Departamento,Telefono,Correo from tbwbVoluntario where  (status=1) and Departamento ='19' and Universidad ='15' and latitud <>'0' and longitud <>'0'";
                cmd.CommandType = CommandType.Text;
                cmd.Connection = conexionSQL;
                conexionSQL.Open();

                Tabla = new DataTable();
                Tabla.Load(cmd.ExecuteReader());
                Repeater1.DataSource = Tabla;
                Repeater1.DataBind();
                conexionSQL.Close();
                }

        }
    
asked by visión crack 30.07.2018 в 23:15
source

1 answer

0

If I'm not understanding you, you will have two drop-down in your application, one for departments and another for universities. Assuming that these were dropDepartments and dropUniversities, the query would look something like this:

cmd.CommandText = "select id,Latitud,Longitud,Nombres,Apellidos,Direccion,(select Nombre from dbo.tbwbDepartamento  where id=Departamento)Departamento,Telefono,Correo from tbwbVoluntario where  (status=1) and Departamento ='" + dropDepartamentos.SelectedValue + "' and Universidad ='" + dropUniversidades.SelectedValue + "' and latitud <>'0' and longitud <>'0'";

I hope it serves you.

Greetings.

    
answered by 07.12.2018 в 09:45