display a message instead of displaying the result of the search in a gridview

1

I'm new to this in C # and I have this project which asked me that they no longer want to show the data in a table, they just want a message that says "that person is found" and if your rfc is not in that one Base says 'person is not in this area'

protected void btnBuscador_Click(object sender, EventArgs e)
        {
            string rfcTxt = txtBuscador.Text.Trim();
            rfcTxt.Length.ToString();
            string nameTxt = txtBuscador2.Text;
            CV_ver.Visible = true;

            if (rbRFC.Checked == false)
            {

            }
            else if (rfcTxt.Length < 11)
            {
                LblBus.Visible = true;
                LblBus.Text = "El campo RFC debe contener 12 ó 13 caracteres.";
                CV_ver.Visible = false;
            }
            else
            {
             SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionDate"].ConnectionString);
                SqlCommand cmd = new SqlCommand("consultarRFC_2", con);
                cmd.CommandType = CommandType.StoredProcedure;
                SqlParameter p1 = new SqlParameter("rfc", rfcTxt);

                cmd.Parameters.Add(p1); 


                SqlDataAdapter da = new SqlDataAdapter(cmd);
                DataTable dt = new DataTable();
                da.Fill(dt);
                CV_ver.DataSource = dt;
                CV_ver.DataBind();

            }
    
asked by Mario 04.05.2018 в 17:00
source

2 answers

0

After the line: da.Fill(dt); is as simple as adding this:

string mensaje;

// Obtenga una referencia ClientScriptManager de la clase Página.
ClientScriptManager cs = Page.ClientScript;

if (dt.Rows.Count > 0)
{
    mensaje = "alert(\"La Persona se encuentra.\");";
    cs.RegisterStartupScript(this, GetType(),
                                  "MENSAJE", mensaje, true);
}
else
{
    mensaje = "alert(\"La Persona no se encuentra en esta área.\");";
    cs.RegisterStartupScript(this, GetType(),
                                  "MENSAJE", mensaje, true);
} 

You must take into account that in all the parts of your code where you have CV_ver.Visible = true; you must return false to not show it or delete the code.

Additionally, if you do not use your GridView , you do not need to assign the data source :

CV_ver.DataSource = dt;
CV_ver.DataBind();

As stated by @gbianchi if you only get a single result rfc because you do not use ExecuteScalar() , this avoids having to use SqlDataAdapter and DataTable .

Then it would be something like this:

string mensaje;

// Obtenga una referencia ClientScriptManager de la clase Página.
ClientScriptManager cs = Page.ClientScript;

if (!string.IsNullOrEmpty(Convert.ToString(cmd.ExecuteScalar())))
{
    mensaje = "alert(\"La Persona se encuentra.\");";
    cs.RegisterStartupScript(this, GetType(),
                                  "MENSAJE", mensaje, true);
}
else
{
    mensaje = "alert(\"La Persona no se encuentra en esta área.\");";
    cs.RegisterStartupScript(this, GetType(),
                                  "MENSAJE", mensaje, true);
} 
  
    
answered by 04.05.2018 / 17:22
source
0

@mario what you can do is the following.

in your datatable valid if there is information if it comes out that your datatable has no data you send a string or lbl or texbox that has no information if you have to show that there is information

protected void btnBuscador_Click(object sender, EventArgs e)
{
     string rfcTxt = txtBuscador.Text.Trim();
     rfcTxt.Length.ToString();
     string nameTxt = txtBuscador2.Text;
     CV_ver.Visible = true;

    if (rbRFC.Checked == false)
    {

    }
    else if (rfcTxt.Length < 11)
    {
         LblBus.Visible = true;
         LblBus.Text = "El campo RFC debe contener 12 ó 13 caracteres.";
         CV_ver.Visible = false;
    }
    else
    {
        SqlConnection con = new SqlConnection(WebConfigurationManager.ConnectionStrings["ConnectionDate"].ConnectionString);
        SqlCommand cmd = new SqlCommand("consultarRFC_2", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter p1 = new SqlParameter("rfc", rfcTxt);

        cmd.Parameters.Add(p1); 

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        // CV_ver.DataSource = dt;
        //CV_ver.DataBind();

        if (dt .Rows.Count == 0)
        {
           string mensaje ;
           mensaje='persona no se encuentra en esta área';
        }
        else
        {
           string mensaje ;
           mensaje='se encuentra esa persona';
        }
  }
    
answered by 04.05.2018 в 17:08