Hello everyone,

Please someone help me out from this problem. I have an datagridview which is bound to an datasource which contains two tables CompDetails and Orders. Now when i click on button and if texbox.text=1 data will be displayed fine, if i click the button for second time the data will be displayed again. I mean i have 4 rows in table, when i click button second time the extra 4 rows will be displayed, which is duplicate copy of first. Regardless of clicks only one copy shld be displayed on the grid.

the code is:

namespace CompTable
{
    public partial class Form1 : Form
    {
        SqlConnection con = null;
        string connection = "";
        DataSet ds = new DataSet("Company");
        SqlDataAdapter da = null;
        SqlCommandBuilder cb = null;
        public string str ;
        public Form1()
        {
            InitializeComponent();
            connection = ConfigurationManager.ConnectionStrings["Mycon"].ConnectionString;

        }

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

        private void InitializeConnection()
        {
            con = new SqlConnection(connection);
            con.Open();
            if (textBox1.Text == "1")
            {
                str = "CompDetails";
                da = new SqlDataAdapter("select * from CompDetails", con);
                cb = new SqlCommandBuilder(da);
                da.InsertCommand = cb.GetInsertCommand();
                da.DeleteCommand = cb.GetDeleteCommand();
                da.UpdateCommand = cb.GetUpdateCommand();
                
            }
            if (textBox1.Text == "2")
            {
                str = "Orders";
                da = new SqlDataAdapter("select * from Orders", con);
                cb = new SqlCommandBuilder(da);
                da.InsertCommand = cb.GetInsertCommand();
                da.DeleteCommand = cb.GetDeleteCommand();
                da.UpdateCommand = cb.GetUpdateCommand();
                //da.Fill(ds, str);
            }
            da.Fill(ds, str);
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = str;
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //MessageBox.Show(str);
            InitializeConnection();
            da.Update(ds,str);
        }
    }
}

Thank you

Recommended Answers

All 6 Replies

1. set dataSource to null
2. bind it again.

Yes i found out. Make Dataset instance local. i.e create a new instance in initializeconnection() method.

Thanks for your reply

Agian facing with other problem.If i make ds=new Dataset() local da.updates is not working. its not updating the database

Instantiate newDataSet with different name variable. This way like you do, you override it, and all data from before are gone.

now the condition set by my mentor is to use only one dataset and do all the operations using only tat dataset. So is the problem. How can i do it

Then you can instantiate a DataView.
A datatable (or DataSet, which is combinded with ore or multiple datatables) is an in-memory representation of a single database table. You can think of it as having columns and rows in the same way.
A dataview is a view on a datatable, a bit like a sql view. It allows you to filter and sort the rows - often for binding to a windows form control.

-----
Check here how to use it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.