none
Recuperar un dato desde una BD(SQL) a TEXTBOX RRS feed

  • Pregunta

  • Estoy viendo la manera de mediante una consulta recuperar un dato desde SQL para que este me aparezca en un textbox.

    Os dejo el codigo para que, si podeis me echeis una mano, GRACIAS.

    Private Sub ComboBox4_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox4.SelectedIndexChanged
    
            Dim envasebebida As String
            envasebebida = ComboBox4.SelectedItem.ToString
    
    
            Dim sqlConnection1 As New SqlConnection("Data Source=FRANCISCOXPS27\FRANCISCO;Initial Catalog=RESTAURANTE;User ID=sa;Password=*****")
            Dim cmd As New SqlCommand
            Dim reader As SqlDataReader
            cmd.CommandText = "SELECT  volumen_bebida  FROM configuracion_envase_bebida  WHERE envase_bebida = @envasebebida "
            cmd = New SqlCommand(cmd.CommandText, sqlConnection1)
            'Variables
            cmd.Parameters.Add("@envasebebida", SqlDbType.NVarChar, 255).Value = envasebebida
            sqlConnection1.Open()
            reader = cmd.ExecuteReader()
    
    
            TextBox1.Text = reader.ToString
            sqlConnection1.Close()
            reader.Close()
    
        End Sub


    • Editado Frank0401 martes, 8 de diciembre de 2015 18:59
    martes, 8 de diciembre de 2015 18:59

Respuestas

  • Hola Frank0401,

    Si sólo es un dato el que recuperas entonces basta con usar ExecuteScalar();

    Private Sub ComboBox4_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ComboBox4.SelectedIndexChanged
    
            Dim envasebebida As String
            envasebebida = ComboBox4.SelectedItem.ToString
    
    
            Dim sqlConnection1 As New SqlConnection("Data Source=FRANCISCOXPS27\FRANCISCO;Initial Catalog=RESTAURANTE;User ID=sa;Password=*****")
            
            Dim ConsultaSQL = "SELECT  volumen_bebida  FROM configuracion_envase_bebida  WHERE envase_bebida = @envasebebida"
    
            Dim cmd As New SqlCommand(ConsultaSQL, sqlConnection1)
    
            'Variables
            cmd.Parameters.Add("@envasebebida", SqlDbType.NVarChar, 255).Value = envasebebida
            
            sqlConnection1.Open()
            TextBox1.Text = cmd.ExecuteScalar()
            sqlConnection1.Close()
    
        End Sub




    martes, 8 de diciembre de 2015 19:10