I have application which contain textbox, 2 button and datagridview
In text box user will enter query
button1 click to display query result in gridview and Button2 to export data to excel
My application is working fine
But problem is when i again click button1 it also show the previous data with new one
But i dont want when user click button1 2nd time the prevoius data to be present

Do mean that when you execute your query the datagridview should refresh the data!!
If So, Here it is.

If you use dataadapter to fill gridview use,

dataGridView1.DataSource = null;

If you manually fill the gridview Use,

dataGridView1.Columns.Clear();

Edited 5 Years Ago by samueal: n/a

namespace QueryWindow
{
public partial class Form1 : Form
{
    public static string fname = "";
    DataSet ds = new DataSet();
    public Form1()
    {
        InitializeComponent();
    }

    private void Form1_Load(object sender, EventArgs e)
    {
            
            
    }

    private void button1_Click(object sender, EventArgs e)
    {
        /*if (dataGridView1.Columns != null)
        {
            dataGridView1.DataSource = null;
        }
        */
        string value = "";
        string value1 = "";
        string keytitle = "Provide Key";
        string prompt = "Enter Key:";
        InputBox ib = new InputBox();
        if (ib.InputBox1(keytitle, prompt, ref value) == DialogResult.OK)
        {
            value1 = value;
        }


        string decrpt = System.Configuration.ConfigurationManager.AppSettings["Connect2"];
        CryptorDecrpt cod = new CryptorDecrpt();
        //string temp = cod.Decrypt("+UevxF7TyVs=", true, value1);
        string temp = cod.Decrypt(decrpt, true, value1);
        //string conn1 = System.Configuration.app
        string conn1 = System.Configuration.ConfigurationManager.AppSettings["Connect1"];
        if (temp == "0")
        {

            MessageBox.Show("Invalid Key / Case Sensitive");
            return;
        }

       

        string connstr = conn1 + temp + ";";
        OracleConnection conn = new OracleConnection(connstr);
        OracleCommand cmd = new OracleCommand();
        //DataSet ds = new DataSet();
        cmd = conn.CreateCommand();
        dataGridView1.DataSource = null;
        try
        {
            conn.Open();
            cmd.CommandText = textBox1.Text;
            OracleDataAdapter adapter = new OracleDataAdapter(cmd);
            adapter.Fill(ds);
            dataGridView1.DataSource = ds.Tables[0];
        }
        catch (Exception err)
        {
            MessageBox.Show("Error :" + err.Message);
        }
        finally
        {
            conn.Close();
        }
    }



}
}
This article has been dead for over six months. Start a new discussion instead.