Jump to content

Recommended Posts

Posted

olá,

quero preencher uma gridview com dados de uma base de dados.

O problema é que não está a preencher e eu não estou a perceber pq.

    protected void Page_Load(object sender, EventArgs e)
    {
          
        userTB.Text = Session["nome"].ToString();
        //Session.Remove("nome");
  
        if (!IsPostBack) -----------------------------------------------------------------------> não entra neste if
        {
            if (userTB.Text.Contains("*?@?*.?*"))
            {
                carregardados();
            }
            else
            {
                carregardados2();
            }
        }
    }

    public void carregardados()
    {
         GridView1.Visible = false;
        //Label1.Text = "";
        
         
        string query = ("Select mes,tiposervico,horasserv,preçohora,totalserv from mensalidades Where email '" + userTB.Text + "'order by mes");
       

        String SERVIDOR = "localhost";
        String BANCO = "";
        String USUARIO = "";
        String SENHA = "";
        
         
        String stringConexao = "Database=" + BANCO + ";Data Source=" + SERVIDOR + ";User Id=" + USUARIO + ";Password=" + SENHA + "; pooling=false";

        
        MySqlConnection conexao = new MySqlConnection(stringConexao);

        
        MySqlDataAdapter myAdapter = new MySqlDataAdapter();

        
        DataSet dataset = new DataSet();
        try
        {
           
            myAdapter.SelectCommand = new MySqlCommand(query, conexao);

           
            myAdapter.Fill(dataset);

           
            GridView1.DataSource = dataset;

           GridView1.DataBind();

            GridView1.Visible = true;


        }
        catch (MySqlException erro)
        {
            MessageBox.Show("erro " + erro);
        }
        catch (Exception ex)
        {
            MessageBox.Show("erro inesperado " + ex);
        }
        finally
        {
            dataset.Dispose();
            myAdapter.Dispose();
            conexao.Close();
            conexao.Dispose();
        }
    }

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.