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