Filling gridview with stored procedure

0

I am trying to fill a grid with data of sp I'm working with data, business and presentation layer

This is where I call the business and then bring the data from the data layer (the sp ) BuscarListaPersonal(); the HTML is already echoed and adapted.

Then I throw this error when loading it:

  

No field or property per_emp_per found in the origin of   Selected data.

But that field is in my sp and the strange thing is that this is the last field of my sp , which only shows data (it is a select), it does not have input variables porsiaca

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Negocio;
using System.IO;
using System.Data;
using System.Windows.Forms;
using System.Text;
using System.Web.UI.HtmlControls;

namespace Presentacion.Modulos.Mantencion
{
    public partial class Empleados_ADM : System.Web.UI.Page
    {
        int Rut;
        string Apellido;
        int codsuc;
        int codemp;
        string CadenaConexion = MasterPage.CadenaConexion;

        protected void Page_Load(object sender, EventArgs e)
        {

            if (!IsPostBack)
            {
                cargarDropEmpresa();
                cargarDropSucursal();
                BuscarListaPersonal();

                PanelMsje.Visible = false;
                GridP.Visible = true;
            }



            //Validacion de Usuario para mostrar contenido
            if (((string)Session["Tipo"]) == "1") // = Administrador
            {
                BuscarListaPersonal();
            }
            else
            {
                // si no es adminsitrador redireccionamos a la pagina principal
                ClientScript.RegisterStartupScript(this.GetType(), "Alert", "<SCRIPT LANGUAGE='javascript'> alert('No tiene permisos para este contenido!');  document.location=('/Modulos/Principal.aspx');</SCRIPT>");
            }
        }


        //------Carga el droplist 
        protected void cargarDropSucursal()
        {

            DataTable Sucursal = new DataTable();
            Sucursal = AccesoLogica.BuscarParametros_PorTabla(Convert.ToInt16(Session["Tab_Sucursales"].ToString()), CadenaConexion);
            DropSucursal.DataSource = Sucursal;
            DropSucursal.DataValueField = "PAR_COD_PAR";
            DropSucursal.DataTextField = "PAR_DES_PAR";
            DropSucursal.DataBind();
            Session["Codsuc"] = DropSucursal.SelectedIndex;
        }
        protected void cargarDropEmpresa()
        {

            DataTable Empresa = new DataTable();
            Empresa = AccesoLogica.BuscarParametros_PorTabla(Convert.ToInt16(Session["Tab_Empresas"].ToString()), CadenaConexion);
            DropEmpresa.DataSource = Empresa;
            DropEmpresa.DataValueField = "PAR_COD_PAR";
            DropEmpresa.DataTextField = "PAR_DES_PAR";
            DropEmpresa.DataBind();
            Session["Codemp"] = DropEmpresa.SelectedIndex;
        }

        //------Carga de gridView por sucursal o Apellido si apellido o rut esta vacío solo filtra por sucursal si no hay ninguna sucursal busca por todos
        public void BuscarListaPersonal()
        {

            GridP.DataSource = AccesoLogica.Buscar_Empleadosadmin(CadenaConexion);
            GridP.DataBind();



        }


        //------LLena la grilla dependiendo del codigo de sucursal extraido del droplist
        protected void DropSucursal_SelectedIndexChanged(object sender, EventArgs e)
        {

            DropSucursal.SelectedIndexChanged += new System.EventHandler(this.DropSucursal_SelectedIndexChanged);
            Session["Codsuc"] = DropSucursal.SelectedIndex;
            //BuscarListaPersonal();
        }

        ////------LLena la grilla dependiendo del codigo de Empresa extraido del droplist //cswsawdadfawodiuhawdoihwdopadhopwid
        protected void DropEmpresa_SelectedIndexChanged(object sender, EventArgs e)
        {

            DropEmpresa.SelectedIndexChanged += new System.EventHandler(this.DropEmpresa_SelectedIndexChanged);
            Session["Codemp"] = DropEmpresa.SelectedIndex;
            // BuscarListaPersonal();
        }



        ///-----------------------------Codigo Agregado EF
        ///-----Paginacón de la grilla
        protected void GridP_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.GridP.PageIndex = e.NewPageIndex;
            // BuscarListaPersonal();    
        }

        //------Exportar el Informe de empleados
        protected void BtnInforme_Click(object sender, ImageClickEventArgs e)
        {
            //// Session["Suc"] = DropSucursal.SelectedIndex;
            Response.Redirect("~/Modulos/Mantencion/Informes/InformesEmpleados.aspx");
        }

        protected void GridP_SelectedIndexChanged(object sender, EventArgs e)
        {

        }


        //------Redireccionamiento a modulos de empleados por rut
        protected void Button1_Click(object sender, EventArgs e)
        {
            //if (String.IsNullOrEmpty(txtRut.Text) & String.IsNullOrEmpty(txtApellido.Text))
            //{
            //    PanelMsje.Visible = true;
            //    txtRut.Focus();
            //    return;
            //}
            //else if (String.IsNullOrEmpty(txtRut.Text))
            //{
            //    Rut = 0;
            //    Apellido = txtApellido.Text;
            //    txtApellido.Focus();


            //}
            //else if (String.IsNullOrEmpty(txtApellido.Text))
            //{

            //    Rut = Convert.ToInt32(txtRut.Text);
            //    Apellido = "null";

            //}

            //else if (txtRut.Text != null & txtApellido.Text != null)
           // {

                //Apellido = txtApellido.Text;
                //Rut = Convert.ToInt32(txtRut.Text);

                //        DataTable BuscarEmpleado = new DataTable();
                //        BuscarEmpleado = AccesoLogica.LLenarGrillaAdmin(Rut, Apellido, Codsuc, Codemp, CadenaConexion);
                //        GridP.DataSource = BuscarEmpleado;
                //        GridP.DataBind();
                //    }

                //    DataTable Buscar = new DataTable();
                //    Buscar = AccesoLogica.LLenarGrillaAdmin(Rut, Apellido, Codsuc, Codemp, CadenaConexion);
                //    GridP.DataSource = Buscar;
                //    GridP.DataBind();

                //    BuscarListaPersonal();
                //}
              //  {

                    string Rut = this.txtRut.Text.Trim();
                    string Apellido = this.txtApellido.Text.Trim();
                    string Codsuc = this.DropSucursal.Text.Trim();
                    string Codemp = this.DropEmpresa.Text.Trim();

                    if (Rut.Length == 0)
                        if (Apellido.Length == 0)
                            if (Codsuc.Length == 0)
                                if (Codemp.Length == 0)
                                {


                                    Rut = "";
                                    Apellido = "";
                                    Codsuc = "";
                                    Codemp = "";
                                    GridP.DataSource = AccesoLogica.LLenarGrillaAdmin(Convert.ToInt16(Session["Rut"].ToString()),
                                        (Session["Apellidos"].ToString()), Convert.ToInt16(Session["Codsuc"].ToString()),
                                        Convert.ToInt16(Session["Codemp"].ToString()), CadenaConexion);
                                    GridP.DataBind();
                                   // BuscarListaPersonal();

                                }
                }




            }
        }

my procedure in that field has an inner join to a parent table that contains all the tables and has a code of 999, that is to say that the daughter table of it has as code 999-1 that contains for example the branches, the 999 -2 has the companies and so on. so my sp when he goes to look for something from that table is like this:

Alter PROCEDURE [dbo].[IN_OBTENER_LISTADO_PERSONAL1]
AS
BEGIN
SELECT  per_rut_per, per_nom_per, 
        (per_ape_pat+' '+per_ape_mat) AS Apellidos,
        per_anx_per, 
        per_cor_ele ,
        par1.par_des_par as per_emp_per,
        par2.par_des_par as per_suc_per 
        FROM PER 
        INNER JOIN PAR as par1 ON PAR1.PAR_COD_TAB = 18  AND par1.PAR_COD_PAR = per_emp_per
        INNER JOIN PAR AS PAR2 ON PAR2.PAR_COD_TAB = 13  AND PAR2.PAR_COD_PAR = per_suc_per

If you can see the field that I get the error is being shown by the par_des_par field that brings the text of any field placed at the end of the inner and is defined with the correct name as you can see as per_emp_per // ********************************************** ********* BUSINESS LAYER   public static DataTable Search_Employeesadmin (string CadenaConexion)         {             return Data Access.Lookup_EmployeeAdmin (ChainConnection);         }         public static DataTable LLenarGrillaAdmin (int Rut, string Last, int codsuc, int codemp, string CadenaConexion)         {             SqlCommand _comando = DataAccess.ListadoPersonalEmpresa (Rut, Surname, codsuc, codemp, CadenaConexion);             _command.CommandType = CommandType.StoredProcedure;             return Data Access. Run Command (_ command);         }     } // ********************************************** *********** DATA LAYER   public static DataTable SearchList_EmployeesAdmin (string CadenaConexion)         {

        SqlConnection Conexion = new SqlConnection(CadenaConexion);
        SqlCommand Comando = new SqlCommand("IN_OBTENER_LISTADO_PERSONAL1", Conexion);
        Comando.CommandType = CommandType.StoredProcedure;
        try
        {
            SqlDataAdapter da = new SqlDataAdapter(Comando);
            DataTable Set = new DataTable();
            da.Fill(Set);
            return Set;
        }
        catch (Exception ex)
        {
            throw new Exception(ex.Message);
        }
    }

// ****************************************** *** DATA LAYER, WHERE THE PREVIOUS IS DONE (FUNCTION THAT ALL OCCUPIED)

    public static DataTable EjecutarComando(SqlCommand _comando)
    {
        DataTable _tabla = new DataTable();
        try
        {
            _comando.Connection.Open();
            SqlDataAdapter _adaptador = new SqlDataAdapter();
            _adaptador.SelectCommand = _comando;
            _adaptador.Fill(_tabla);
        }
        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            _comando.Connection.Close();
        }
        return _tabla;
    }
    
asked by Ricconter Salazar 14.12.2016 в 21:49
source

2 answers

1

Code for your Button Event

  

Code for Mysql

        DataTable dt = new DataTable();
        MySqlCommand comando = new MySqlCommand("mi_procedimiento", conexion);
        comando.CommandType = CommandType.StoredProcedure;
        MySqlDataAdapter sda = new MySqlDataAdapter(comando);
        sda.Fill(dt);
        dataGridView1.DataSource = dt;
  

Code for SqlServer

 DataTable dt = new DataTable();
 SqlCommand comando = new SqlCommand("mi_procedimiento", conexion);
 comando.CommandType = CommandType.StoredProcedure;
 SqlDataAdapter sda = new SqlDataAdapter(comando);
 sda.Fill(dt);
 dataGridView1.DataSource = dt;
  

Select Example of a Procedure for Mysql

DELIMITER ;;
CREATE PROCEDURE 'mi_procedimiento'()
BEGIN
select * from mitabla;
END ;;
DELIMITER ;
  

Example of an Insert for Mysql

DELIMITER ;;
CREATE  PROCEDURE 'proc_add_profesor'(
p_nuemp int,
p_nombre varchar(45),
p_apepat varchar(45),
p_apemat varchar(45),
p_direccion varchar(100),
p_telefono varchar(45),
p_correo varchar(45),
p_usuario varchar(45)
)
BEGIN
insert into mitabla values(p_nuemp,p_nombre,p_apepat,p_apemat,p_direccion,p_telefono,p_correo,p_usuario);
END ;;
DELIMITER ;
    
answered by 14.12.2016 в 22:04
0

It is clear that the error message indicates that you want to obtain the value of the field per_emp_per which you do not define in the SELECT that the procedure returns.

In the code that shows the execution of the procedure is not displayed, therefore it is not useful to analyze the problem.

What I would recommend is that you evaluate the SELECT that you execute and if the fields that it defines match those you access from code

    
answered by 14.12.2016 в 23:15