Load data in a text box from sql, when changing a combo box or a DataTimePicker in C #, Visual Studio MVC

3

making an application that records the daily information of what was done on a farm during the day. The screen is like this:

In which the intention is that when changing a combo box or the datetimePicker date data is loaded from sql.

I already have the stored procedure. But I do not know how to make the data load in c #.

Code in C #, visual studio:

        //Al cambiar la fecha o el combo finca, se ponen los datos si es que hay, en los txt
    private void CargarDistribucionDiariaDatosFinca()
    {
        try
        {
            using (GestorDistribucionDiaria laDistribucionDiaria = new GestorDistribucionDiaria())
            {
                int id;
                int.TryParse(cbx_nombreFinca.SelectedValue.ToString(), out id);
                DateTime fecha = Convert.ToDateTime(dtp_finca.Value.Date.ToString("yyyy-MM-dd"));


                txt_frutaEstablecida.Text = Convert.ToString(laDistribucionDiaria.ListarDistribucionDiariaDatosFinca(id, fecha));
                txt_atrasoCoyol.Text = Convert.ToString(laDistribucionDiaria.ListarDistribucionDiariaDatosFinca(id, fecha));

            }
        }
        catch (Exception e)
        {
            MessageBox.Show("Error de SQL: " + e.Message);
        }
    }'

This is the code I use to call the stored procedure:

//Metodo listar LOTES los textbox, respecto al combo box de la finca y la fecha
    public DataTable ListarDistribucionDiariaDatosFinca(int id_finca, DateTime fecha)
    {
        miComando = new SqlCommand();
        Console.WriteLine(" Gestor ListarDistribucionDiariaDatosFinca");

        miComando.CommandText = "ListarDistribucionDiariaDatosFinca";

        miComando.Parameters.Add("@id_finca", SqlDbType.Int);
        miComando.Parameters["@id_finca"].Value = id_finca;

        miComando.Parameters.Add("@fecha", SqlDbType.Date);
        miComando.Parameters["@fecha"].Value = fecha;

        DataSet laDistribucionDiaria = new DataSet();
        this.abrirConexion();
        laDistribucionDiaria = this.seleccionarInformacion(miComando);
        DataTable miTablaDatos = laDistribucionDiaria.Tables[0];

        return miTablaDatos;
    }
    
asked by Luis Duarte 27.09.2018 в 19:53
source

1 answer

1

Well, you must create the connection to your BD from C# , assuming you already did it and that the method you added in your question does what you need and what you want is to invoke that method every time you change a value of any control you should only add these functions to the eventos of your controls:

private void cbx_nombreFinca_SelectedIndexChanged(object sender, EventArgs e)
{
    try
    {
        CargarDistribucionDiariaDatosFinca();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

private void dtp_finca_ValueChanged(object sender, EventArgs e)
{
    try
    {
        CargarDistribucionDiariaDatosFinca();
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message);
    }
}

You select the properties of ComboBox and in the events tab you look for SelectedIndexChanged there you assign the event, similarly you do with DateTimePicker .

You can guide yourself, you have to do something similar to this:

public DataTable ListarDistribucionDiariaDatosFinca(int id_finca, DateTime fecha)
{
    SqlConnection connection = new SqlConnection("CONNECTION_STRING");
    connection.Open();
    SqlCommand command = new SqlCommand("ListarDistribucionDiariaDatosFinca", connection);
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.AddWithValue("@id_finca", id_finca);
    command.Parameters.AddWithValue("@fecha", fecha);
    SqlDataAdapter adapter = new SqlDataAdapter(command);
    DataTable table = new DataTable();
    adapter.Fill(table);
    connection.Close();
    return table;
}
    
answered by 27.09.2018 / 21:52
source