error in control from static method c # [duplicated]

0

I have a problem in bindear a gridview, since I need that when finishing the operations of a static method recharge the data of a gridview, place a breakpoint to know where the error was and it marks me when I assign the datatable to the datasource of the gridview

        public static string GetDataAjax(int proyecto, int concepto,string descripcion, int cantidad, double precio, double subtotal, string fechaComprobante, string operacion, string sesion)
    {
        string mensajeOperacion="";

        clCajaChica oProyectoGto = new clCajaChica();

        EntidadProyectoGasto EntProyectoGto = new EntidadProyectoGasto();

        EntProyectoGto.IDProyecto = Convert.ToInt32(proyecto);
        EntProyectoGto.IDConceptoGasto = Convert.ToInt32(concepto);
        EntProyectoGto.Descripcion = descripcion;
        EntProyectoGto.Cantidad = Convert.ToInt32(cantidad);
        EntProyectoGto.PrecioUnitario = Convert.ToDouble(precio);
        EntProyectoGto.SubTotal = Convert.ToDouble(subtotal);
        EntProyectoGto.FechaComprobante = Convert.ToDateTime(fechaComprobante);
        EntProyectoGto.UrlArchivo = "x";
        EntProyectoGto.IDUsuarioAlta = Convert.ToInt32(sesion);
        EntProyectoGto.FechaAlta = Convert.ToDateTime(DateTime.Now);
        EntProyectoGto.IDUsuarioModifica = Convert.ToInt32(sesion);
        EntProyectoGto.FechaModifica = Convert.ToDateTime(DateTime.Now);

        if (operacion == "Agregar")
        {
            if (oProyectoGto.InsertarProyectoGasto(EntProyectoGto) > 0)
            {
                ControlEscolar_CajaChica_DatosProyectosEfectivo instance = new ControlEscolar_CajaChica_DatosProyectosEfectivo();

                using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString))
                {
                    SqlCommand cmd = new SqlCommand("SELECT  pg.ID_ProyectoGasto, pg.ID_Proyecto, p.tituloproyecto, pg.ID_ConceptoGasto,        cg.conceptogasto, pg.Descripcion,pg.Cantidad,pg.PrecioUnitario,pg.SubTotal,pg.FechaComprobante, pg.UrlArchivo,pg.Validado,pg.Eliminado  FROM ProyectosGastos pg INNER JOIN Proyectos p ON p.id_proyecto = pg.id_proyecto INNER JOIN ConceptosGastos cg ON cg.id_conceptogasto = pg.id_conceptogasto WHERE pg.id_proyecto = '" + proyecto + "' AND pg.Eliminado <> 1 ORDER BY pg.ID_ProyectoGasto DESC", con);
                    SqlDataAdapter adapter = new SqlDataAdapter(cmd);
                    DataTable dt = new DataTable();
                    adapter.Fill(dt);
                    instance.GridView1.DataSource = dt;
                    instance.GridView1.DataBind();
                }                
                mensajeOperacion = "ok";
            }
            else
            {
                mensajeOperacion = ("No se pudo agregar el registro.");
            }
        }
        return mensajeOperacion;

    }

This is the error

    
asked by Ivxn 05.01.2018 в 01:06
source

2 answers

0

The method you are accessing is static and therefore you can not access the elements of the page, you must pass the GridView element to the method as a parameter of the function. You can make an intermediate function of the codebehind that calls the static method since from codebehind you have access to GridView .

You can also reference the page in the following way:

if (HttpContext.Current != null)
{
        Page page = (Page)HttpContext.Current.Handler;
        TextBox TextBox1 = (TextBox)page.FindControl("TextBox1");
}

Although it is not advisable to use static methods in the codebehind of a page, it would be better if you used it as a normal method when doing postback on the page.

    
answered by 05.01.2018 в 11:59
0

Assigning from a static method to a GridView in a Web environment is a bad practice since a single instance of your method serves to serve multiple calls. If you want to set the Grid from Ajax, you need to send the data back from your method as shown here .

    
answered by 05.01.2018 в 10:35