This is a demo project.
What i am trying to achieve is, when a button is clicked it will check certain condition then will insert rows into Datagridview.
The thing is i need to insert rows again with that of existing rows present in the datagridview, with the radio button conditions applied (both new row and updating).
As so far i have achieved this much.
private void button5_Click(object sender, EventArgs e)
{
if (button5.Text == "")
{
MessageBox.Show("No Value Assigned");
}
else
{
MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db"].ConnectionString);
con.Open();
MySqlDataAdapter dt = new MySqlDataAdapter();
DataTable tt = new DataTable();
BindingSource bs = new BindingSource();
if (dataGridView1.RowCount > 0)
{
bs = (BindingSource)dataGridView1.DataSource;
}
else
{
if (radioButton1.Checked == true)
{
string query = "SELECT type,priceS FROM service WHERE type='" + button5.Text + "'";
MySqlCommand cmd = new MySqlCommand(query, con);
dt.SelectCommand = cmd;
}
else if (radioButton2.Checked == true)
{
string query = "SELECT type,priceM FROM service WHERE type='" + button5.Text + "'";
MySqlCommand cmd = new MySqlCommand(query, con);
dt.SelectCommand = cmd;
}
else if (radioButton3.Checked == true)
{
string query = "SELECT type,priceL FROM service WHERE type='" + button5.Text + "'";
MySqlCommand cmd = new MySqlCommand(query, con);
dt.SelectCommand = cmd;
}
}
dt.Fill(tt);
bs.DataSource = tt;
dataGridView1.DataSource = bs;
dt.Update(tt);
con.Close();
}
}
I can Insert first row, but when i try to insert another with the same button problem.
Any idea is appreciated.
private void button5_Click(object sender, EventArgs e)
{
if (button5.Text == "")
{
MessageBox.Show("No Value Assigned");
}
else
{
MySqlConnection con = new MySqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings["db"].ConnectionString);
con.Open();
MySqlDataAdapter dt = new MySqlDataAdapter();
DataTable tt = new DataTable();
BindingSource bs = new BindingSource();
if (radioButton1.Checked == true)
{
string query = "SELECT type,priceS FROM service WHERE type='" + button5.Text + "'";
MySqlCommand cmd = new MySqlCommand(query, con);
dt.SelectCommand = cmd;
}
else if (radioButton2.Checked == true)
{
string query = "SELECT type,priceM FROM service WHERE type='" + button5.Text + "'";
MySqlCommand cmd = new MySqlCommand(query, con);
dt.SelectCommand = cmd;
}
else if (radioButton3.Checked == true)
{
string query = "SELECT type,priceL FROM service WHERE type='" + button5.Text + "'";
MySqlCommand cmd = new MySqlCommand(query, con);
dt.SelectCommand = cmd;
}
if (dataGridView1.RowCount > 0)
{
tt.Rows.Add(dataGridView1);
dt.Fill(tt);
bs.DataSource = tt;
dataGridView1.DataSource = bs;
dt.Update(tt);
con.Close();
}
else
{
dt.Fill(tt);
bs.DataSource = tt;
dataGridView1.DataSource = bs;
dt.Update(tt);
con.Close();
}
}
}
this is my another method but i am getting Error: Input array is longer than the number of columns in this table. Where am I making the mistake?