Click here to Skip to main content
15,881,380 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have two tables : VehicleMaintenance_log and safety_log. i want to display only one table data at a time. i have one combobox with two options:maintenance log and safety_report, When i select on one item oncombobox ex: for maintenance log, the gridview should display only that maintenance log values according to registration number. How can i acheive this? I know only to select from a table at a time. I am doing this for the first time. I am using visual studio 2013 and for the database mysql.My data resides in two tables(maintenance_log and safety_report) in the same schema.This is what i tried i used " if" loops.please help.
C#
private void button1_Click(object sender, EventArgs e)
        {
            string constring = "datasource=localhost;port=3306;username=root;password=Ammoos123";
            MySqlConnection conDatabase = new MySqlConnection(constring);
            if((this.cmb2.SelectedItem.ToString())=="SafetyInspection_Report")
            {
                MySqlCommand cmdDatabase = new MySqlCommand("select * from vehicle_automation.safetyinspection_report where vehRegNo=@regno and inspection_date between @date1 and @date2", conDatabase);
                cmdDatabase.Parameters.AddWithValue("@regno", this.cmb1.SelectedItem);
                cmdDatabase.Parameters.AddWithValue("@date1", this.dt1.Text);
                cmdDatabase.Parameters.AddWithValue("@date2", this.dt2.Text);
                try
                {
                    MySqlDataAdapter sda = new MySqlDataAdapter();
                    sda.SelectCommand = cmdDatabase;
                    DataTable dbdataset = new DataTable();
                    sda.Fill(dbdataset);
                    BindingSource bsource = new BindingSource();
                    bsource.DataSource = dbdataset;
                    dtgv1.DataSource = bsource;
                    sda.Update(dbdataset);

                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
            else if((this.cmb2.SelectedItem.ToString())=="Maintenance_log")
            {
                 MySqlCommand cmdDatabase = new MySqlCommand("select * from vehicle_automation.maintenance_log where vehRegNo=@regno and inspection_date between @date1 and @date2", conDatabase);
                cmdDatabase.Parameters.AddWithValue("@regno", this.cmb1.SelectedItem);
                cmdDatabase.Parameters.AddWithValue("@date1", this.dt1.Text);
                cmdDatabase.Parameters.AddWithValue("@date2", this.dt2.Text);
            try
            {
                MySqlDataAdapter sda = new MySqlDataAdapter();
                sda.SelectCommand = cmdDatabase;
                DataTable dbdataset = new DataTable();
                sda.Fill(dbdataset);
                BindingSource bsource = new BindingSource();
                bsource.DataSource = dbdataset;
                dtgv1.DataSource = bsource;
                sda.Update(dbdataset);

            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }        
         }


additional information copied from comment below
Hai actually i dont know if this is the proper way to do it.Its working for safety inspection.bt if there is no data in the table also the gridview is giving the empty table there I need to display "there is no datas found".
Posted
Updated 7-Dec-14 0:37am
v2
Comments
Kornfeld Eliyahu Peter 7-Dec-14 6:05am    
And what's wrong with your code?
Helen Jess 7-Dec-14 6:08am    
Hai actually i dont know if this is the proper way to do it.Its working for safety inspection.bt if there is no data in the table also the gridview is giving the empty table there I need to display "there is no datas found".
BillWoodruff 7-Dec-14 6:46am    
This may be quite an unusable idea, but: have you thought of having two DataGridViews, and simply switching which of them appears based on the ComboBox selection ?

Or, might it be possible to switch what the (one) DataGridView is bound to ?
Helen Jess 7-Dec-14 7:47am    
Hai many thanks both code is working now. i added one more gridview and i am getting both the table records.
DamithSL 7-Dec-14 10:12am    
if you solve your problem please post the solution and mark it as answer. ( it will save time of others who open this question for answering)
or otherwise delete it.

The is the solution.
private void button1_Click(object sender, EventArgs e)
{
string constring = "datasource=localhost;port=3306;username=root;password=Ammoos123";
MySqlConnection conDatabase = new MySqlConnection(constring);
if((this.cmb2.SelectedItem.ToString())=="SafetyInspection_Report")
{
dtgv4.Visible = false;
dtgv1.Visible = true;
MySqlCommand cmdDatabase = new MySqlCommand("select * from vehicle_automation.safetyinspection_report where vehRegNo=@regno and inspection_date between @date1 and @date2", conDatabase);
cmdDatabase.Parameters.AddWithValue("@regno", this.cmb1.SelectedItem);
cmdDatabase.Parameters.AddWithValue("@date1", this.dt1.Text);
cmdDatabase.Parameters.AddWithValue("@date2", this.dt2.Text);

try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDatabase;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bsource = new BindingSource();
bsource.DataSource = dbdataset;
dtgv1.DataSource = bsource;
sda.Update(dbdataset);

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
else if((this.cmb2.SelectedItem.ToString())=="Maintenance_Log")
{
dtgv1.Visible = false;
dtgv4.Visible = true;
MySqlCommand cmdDatabase = new MySqlCommand("select * from vehicle_automation.maintenance_log where vehRegNo=@regno and maintenance_date between @date1 and @date2", conDatabase);
cmdDatabase.Parameters.AddWithValue("@regno", this.cmb1.SelectedItem);
cmdDatabase.Parameters.AddWithValue("@date1", this.dt1.Text);
cmdDatabase.Parameters.AddWithValue("@date2", this.dt2.Text);
try
{
MySqlDataAdapter sda = new MySqlDataAdapter();
sda.SelectCommand = cmdDatabase;
DataTable dbdataset = new DataTable();
sda.Fill(dbdataset);
BindingSource bsource = new BindingSource();
bsource.DataSource = dbdataset;
dtgv4.DataSource = bsource;
sda.Update(dbdataset);

}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
 
Share this answer
 
Comments
Helen Jess 7-Dec-14 10:23am    
Hi How can i show a message if there is no data on the database for the search criteria mentioned in this program itself?
Quote:
Hi How can i show a message if there is no data on the database for the search criteria?


check this answer
[^] you can use EmptyDataRow [^]template style. there are some other alternatives, check
http://forums.asp.net/t/1222441.aspx?how+to+display+empty+gridview+with+a+message+No+data+Avaialable+[^]
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900