Update gridview asp.net c #

0

Good, I would like you to help me I have a GridView like this:

When I select in Ver it shows me a popup or a pop-up window like this:

The point is that when I click on Guardar if it is recorded correctly and a message is sent, it was registered correctly like this:

And when it closes it shows again the GridView but in the field where it says validated it has to say YES and the gridview is not updated.

This is the code I use to record:

   protected void BtnnoConforme_Click(object sender, EventArgs e)
        {


            DateTime d1 = DateTime.Parse(TextBox11.Text);
            string mes = d1.ToString("MM");
            string anio = d1.ToString("yyyy");


            DateTime d3 = DateTime.Parse(TextBox5.Text);
            string FechaPagoMes = d3.ToString("MM");
            string FechaPagoAnio = d3.ToString("yyyy");
            if (mes != FechaPagoMes)
            {
                Label1.Text = "El Mes y año de la Fecha de Vencimiento de Recibo tiene que ser igual al Mes y Año de Fecha de Cumplimiento   :     " + TextBox11.Text;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "showModal1();", true);

            }
            else if (anio != FechaPagoAnio)
            {
                Label1.Text = "El Mes y año de la Fecha de Vencimiento de Recibo tiene que ser igual al Mes y Año de Fecha de Cumplimiento   :     " + TextBox11.Text;
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "showModal1();", true);

            }
            else if (mes != FechaPagoMes)
            {
                Label1.Text = "El Mes de Pago tiene que ser del Mismo Mes Actual";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "showModal1();", true);


            }
            else if (anio != FechaPagoAnio)
            {
                Label1.Text = "El Año  Tiene que ser igual al Año de Actual";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "showModal1();", true);

            }
            else if (TextBox4.Text == "")
            {
                Label1.Text = "Ingresa Su Fecha de Emision";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "showModal1();", true);


            }
            else if (DateTime.Parse(TextBox4.Text) > DateTime.Parse(TextBox5.Text))
            {
                Label1.Text = "La Fecha de Emision tiene que ser Menor a la Fecha Vencimiento";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "showModal1();", true);

            }
            else if (TextBox4.Text == "")
            {
                Label1.Text = "Ingresa Su Fecha de Emision";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "showModal1();", true);

            }
            else if (TextBox6.Text == "")
            {
                Label1.Text = "Ingrese el Monto";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "showModal1();", true);

            }
            else if (TextBox6.Text == "0.0")
            {
                Label1.Text = "El monto debe ser mayor de 0";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "showModal1();", true);

            }
            else if (TextBox6.Text == "0")
            {
                Label1.Text = "El monto debe ser mayor de 0";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "showModal1();", true);


            }


            else if (TextBox4.Text == "")
            {
                Label1.Text = "Ingresa Su Fecha de Emision";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "showModal1();", true);

            }
            else if (TextBox6.Text == "..")
            {
                Label1.Text = "Ingresa Su Fecha de Emision";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "showModal1();", true);

            }



            else if (TextBox11.Text == "")
            {
                Label1.Text = "Ingresa Su Fecha de Emision";
                ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "showModal1();", true);

            }
            else
            {

                string fileName = FileUpload1.PostedFile.FileName;

                int fileLength = FileUpload1.PostedFile.ContentLength;



                byte[] imageBytes = new byte[fileLength];

                FileUpload1.PostedFile.InputStream.Read(imageBytes, 0, fileLength);


                SqlConnection cn = new SqlConnection(ObtenerCadenaConexion());
                SqlCommand cmd = new SqlCommand("sp_atendertarea", cn);

                cmd.CommandType = CommandType.StoredProcedure;

                cmd.Parameters.Add("@idregistrotarea", SqlDbType.Int);
                cmd.Parameters.Add("@usuario", SqlDbType.VarChar);
                cmd.Parameters.Add("@fechaemision", SqlDbType.Date);
                cmd.Parameters.Add("@fechacumplimiento", SqlDbType.Date);
                cmd.Parameters.Add("@fechaatendido", SqlDbType.Date);
                cmd.Parameters.Add("@montopagado", SqlDbType.Decimal, 18);
                cmd.Parameters.Add("@numerorecibo", SqlDbType.VarChar);
                cmd.Parameters.Add("@tarea", SqlDbType.VarChar);
                cmd.Parameters.Add("@oficina", SqlDbType.VarChar);
                cmd.Parameters.Add("@fechaemisions", SqlDbType.Date);
                cmd.Parameters.Add("@comentario", SqlDbType.VarChar);
                cmd.Parameters.Add("@fechacumplimientotarea", SqlDbType.Date);
                cmd.Parameters.Add("@fileName", SqlDbType.VarChar, 50);
                cmd.Parameters.Add("@fileContent", SqlDbType.Image);
                cmd.Parameters.Add("@active", SqlDbType.Bit);
                cmd.Parameters.Add("@ERROR", SqlDbType.Char, 500);
                cmd.Parameters.Add("@registro", SqlDbType.Int);



                cmd.Parameters["@idregistrotarea"].Value = lblidregistrotareas.Text;
                cmd.Parameters["@usuario"].Value = TextBox3.Text;
                cmd.Parameters["@fechaemision"].Value = TextBox4.Text;
                cmd.Parameters["@fechacumplimiento"].Value = TextBox5.Text;
                cmd.Parameters["@fechaatendido"].Value = DBNull.Value;
                cmd.Parameters["@montopagado"].Value = TextBox6.Text;
                cmd.Parameters["@numerorecibo"].Value = TextBox7.Text;
                cmd.Parameters["@tarea"].Value = TextBox2.Text;
                cmd.Parameters["@oficina"].Value = TextBox10.Text;
                cmd.Parameters["@fechaemisions"].Value = TextBox4.Text;
                cmd.Parameters["@comentario"].Value = "";
                cmd.Parameters["@fechacumplimientotarea"].Value = TextBox11.Text;
                cmd.Parameters["@fileName"].Value = fileName;
                cmd.Parameters["@fileContent"].Value = imageBytes;
                cmd.Parameters["@active"].Value = true;
                cmd.Parameters["@registro"].Value = true;


                cmd.Parameters["@ERROR"].Direction = ParameterDirection.Output;


                cn.Open();

                BtnnoConforme.Enabled = false;

                cmd.ExecuteNonQuery();

                message = (string)cmd.Parameters["@ERROR"].Value;

                Label1.Text = message;


                ScriptManager.RegisterStartupScript(this, this.GetType(), "Pop", "showModal1();", true);
                /*INICIO */

                if (!Page.IsPostBack)
                {
                    SqlConnection con = new SqlConnection(ObtenerCadenaConexion());
                    con.Open();

                    SqlCommand cmd1 = new SqlCommand("SP_SeguimientoTareaMaster", con);


                    cmd.CommandType = CommandType.StoredProcedure;
                    SqlParameter p1 = new SqlParameter("OPT", 1);


                    cmd.Parameters.Add(p1);



                    SqlDataAdapter da = new SqlDataAdapter(cmd1);
                    DataTable dt = new DataTable();
                    da.Fill(dt);
                    DtgSolicitudes.DataSource = dt;
                    DtgSolicitudes.Visible = true;


                    DtgSolicitudes.DataBind();

                    if (DtgSolicitudes.Rows.Count != 0)
                    {

                        DtgSolicitudes.HeaderRow.Cells[1].Attributes["data-hide"] = "phone";
                        DtgSolicitudes.HeaderRow.Cells[2].Attributes["data-hide"] = "phone";
                        DtgSolicitudes.HeaderRow.Cells[3].Attributes["data-hide"] = "phone";
                        DtgSolicitudes.HeaderRow.Cells[4].Attributes["data-hide"] = "phone";
                        DtgSolicitudes.HeaderRow.Cells[5].Attributes["data-hide"] = "phone";
                        DtgSolicitudes.HeaderRow.Cells[6].Attributes["data-hide"] = "phone";
                        DtgSolicitudes.HeaderRow.Cells[7].Attributes["data-hide"] = "phone";
                        DtgSolicitudes.HeaderRow.Cells[8].Attributes["data-hide"] = "phone";
                        DtgSolicitudes.HeaderRow.Cells[9].Attributes["data-hide"] = "phone";
                        DtgSolicitudes.HeaderRow.Cells[10].Attributes["data-hide"] = "phone";
                        DtgSolicitudes.HeaderRow.Cells[11].Attributes["data-hide"] = "phone";
                        DtgSolicitudes.HeaderRow.Cells[12].Attributes["data-hide"] = "phone";
                        DtgSolicitudes.HeaderRow.Cells[13].Attributes["data-hide"] = "phone";
                        DtgSolicitudes.HeaderRow.Cells[14].Attributes["data-hide"] = "phone";
                        DtgSolicitudes.HeaderRow.Cells[15].Attributes["data-hide"] = "phone";
                        DtgSolicitudes.HeaderRow.TableSection = TableRowSection.TableHeader;




                        con.Close();

                    }
                }

                /* FIN */

                cn.Close();

                TextBox6.Text = "";


            }

        }

If you could help me.

    
asked by PieroDev 18.04.2017 в 18:40
source

4 answers

1

You almost reach the answer, it is correct to want to reload the data again and fill in the grid , there are only several points that must be corrected:

  • It is not necessary to put if(!Page.IsPostBack)
  • You declare cmd1 with SqlCommand cmd1 = new SqlCommand("SP_SeguimientoTareaMaster", con); but when you add the parameter you do it to the variable cmd which you had declared previously
  • When you declare the SqlDataAdapter da = new SqlDataAdapter(cmd1); you relate it to cmd1 , which does not have parameters for the execution of the stored procedure

Your code should look something like this:

//Aquí va todo el código anterior, no considero necesario ponerlo
SqlConnection conexion = new SqlConnection(ObtenerCadenaConexion());
conexion.Open();

SqlCommand sqlCmd = new SqlCommand("SP_SeguimientoTareaMaster", conexion);

sqlCmd.CommandType = CommandType.StoredProcedure;
SqlParameter sqlParam = new SqlParameter("OPT", 1);

sqlCmd.Parameters.Add(sqlParam);

SqlDataAdapter da = new SqlDataAdapter(sqlCmd);
DataTable dt = new DataTable();
da.Fill(dt);
DtgSolicitudes.DataSource = dt;
DtgSolicitudes.Visible = true;
//Aquí va el código con el que finaliza tu método
    
answered by 19.04.2017 / 07:23
source
0

You have all the logic in the same event. As a good practice sepera validations, access to data in different methods or classes. I suppose that the BtnnoConforme_Click event is being called by ajax from the client, and I also suppose that the person in charge of showing the message "It was updated correctly" is in the Javascript function showModal1 (). In that javascript function you should reload the page or the table or the record that you just updated.

    
answered by 18.04.2017 в 21:21
0

I recommend you separate the logic in methods, place all the code "in the PostBack condition", for example, it will not facilitate your work.

From what I saw in your question, I could not detect where you are refreshing the data of your GridView, therefore, the table does not show the changes made to the data ...

That said, I suggest you first create a method that stores the logic you use to query your database information and assign this data to your GridView.

Example:

private void ConsultarDatos()
{
   try
   {
      // Coloca el código que uses para consultar la información
      // y bindear dicha información en tu GridView.
   }
   catch (ex)
   {
       label1.Text = "Error en el método ConsultarDatos();. Error: " + ex.Message;
   }       
}

Then, in your method Page_Load you can modify your logic like this:

private void Page_load()
{
   if (!Page.IsPostBack)
   {
      ConsultarDatos();
   }       
}

On your button after you have actually done the insert / update data , you can invoke the method that draws the data in the GridView:

Example:

private void btn_guardarInformacion()
{
   // Toda tu lógica...
   label1.text = "información guardada.";

   // Con este paso, se actualizará los datos 
   // y serán visibles en el GridView.
   ConsultarDatos();       
}
    
answered by 19.04.2017 в 00:24
0

Ok the problem that does not show Validated field = YES,

I assume that the @active field that you send to the stored is the Validated that you show in the grid. If this is correct, I recommend the following:

  • Check store outside the system, direct in BD sp_atendertarea

    Validate that the insert is being made in the field:

     md.Parameters["@active"].Value = true;
    

    I guess this is where you are saving that grid column.

  • Check that after saving the information you are consulting the grid data again.

  • Check the mapping of data you make to the grid, the mapping you do here directly:

                da.Fill(dt);
                DtgSolicitudes.DataSource = dt;
                DtgSolicitudes.Visible = true;
    
  • But if the grid has some configuration in design mode or in another part where you are mapping the data, check that the columns are called the same as in the dt.

    I think with those reviews you could solve your problem.

    NOTE : If possible, can you publish what your stored contains?

        
    answered by 19.04.2017 в 03:12