从同一文本框更改事件的两个不同表中获取数据

时间:2014-10-09 12:52:26

标签: c# mysql visual-studio-2010 datagridview

我想从一些控件(例如textboxs和datagridview)中从两个不同的表PROOPN_STK中获取一些数据。当我从一个表中选择数据,即PRO时,代码工作正常但是当我在它旁边应用相同的代码时,在同一事件中从另一个表中获取数据,即OPN_STK它会引发异常& #34; object refrence not set to an instance of object" 。我试图了解这个问题,但现在我一片空白,这就是我所做的,

private void comboProname_TextChanged(object sender, EventArgs e)
       {
             dataGridView3.Rows.Clear();
             dataGridView2.Rows.Clear();
             //if (get == false)     //in case when i need to apply condition which I dont prefer
             {
                 string _sql = "select DISTINCT P_batchno,P_sh from PRO where P_name='" + comboProname.Text + "'";
                 if (comboBthNo.Text != "")
                     _sql += " AND P_batchno='" + comboBthNo.Text + "' ";
                 if (SampleToggle)
                     _sql += " AND IsSample='true' ";
                 else
                     _sql += " AND IsSample='false' ";

                 DataTable dt = DataBase.getDataTable(_sql);

                 foreach (DataRow dr in dt.Rows)
                 {
                     if (comboBthNo.Text == "")
                     {
                         dataGridView3.Visible = true;
                         int i = 0;
                         dataGridView3.Rows.Insert(i);
                         dataGridView3.Rows[i].Cells[0].Value = dr["P_batchno"].ToString();
                         dataGridView3.Focus();
                     }
                     sh = dr["P_sh"].ToString();
                 }
             }
             //else if (get == true)   // opnstk
             {
                 string _sql = "select DISTINCT P_batchno,P_sh from OPN_STK where P_name='" + comboProname.Text + "'";
                 if (comboBthNo.Text != "")
                     _sql += " AND P_batchno='" + comboBthNo.Text + "' ";
                 if (SampleToggle)
                     _sql += " AND IsSample='true' ";
                 else
                     _sql += " AND IsSample='false' ";

                 DataTable dt = DataBase.getDataTable(_sql);

                 foreach (DataRow dr in dt.Rows)
                 {
                     if (comboBthNo.Text == "")
                     {
                         dataGridView3.Visible = true;
                         int i = 0;
                         dataGridView3.Rows.Insert(i);
                         dataGridView3.Rows[i].Cells[0].Value = dr["P_batchno"].ToString();
                         dataGridView3.Focus();
                     }
                     sh = dr["P_sh"].ToString();
                 }
             }                
             getdata();
        }

        private void comboBthNo_TextChanged(object sender, EventArgs e)
        {
            dataGridView3.Rows.Clear();
            dataGridView2.Rows.Clear();
           // if (get == false)
            {
                string _sql = "SELECT DISTINCT P_name,P_pack,P_comp,P_expdate,P_rate,P_mrp from PRO where P_batchno='" + comboBthNo.Text + "'";
                if (comboProname.Text != "")
                    _sql += " AND P_name='" + comboProname.Text + "'";
                if (SampleToggle)
                    _sql += " AND IsSample='true' ";
                else
                    _sql += " AND IsSample='false' ";

                DataTable dt = DataBase.getDataTable(_sql);

                foreach (DataRow dr in dt.Rows)
                {
                    if (comboProname.Text == "")
                    {
                        dataGridView2.Visible = true;
                        int i = 0;
                        dataGridView2.Rows.Insert(i);
                        dataGridView2.Rows[i].Cells[0].Value = dr["P_name"].ToString();
                        dataGridView2.Focus();
                    }
                    tbMrp.Text = (dr["P_mrp"].ToString());
                    dateTimePicker2.Text = (dr["P_expdate"].ToString());
                }
            }
           // else if (get == true)   ///// opn stk ///////
            {
                string _sql = "SELECT DISTINCT P_name,P_pack,P_comp,P_expdate,P_rate,P_mrp from OPN_STK where P_batchno='" + comboBthNo.Text + "'";
                if (comboProname.Text != "")
                    _sql += " AND P_name='" + comboProname.Text + "'";
                if (SampleToggle)
                    _sql += " AND IsSample='true' ";
                else
                    _sql += " AND IsSample='false' ";

                DataTable dt = DataBase.getDataTable(_sql);

                foreach (DataRow dr in dt.Rows)  // I get exception here only on dt
                {
                    if (comboProname.Text == "")
                    {
                        dataGridView2.Visible = true;
                        int i = 0;
                        dataGridView2.Rows.Insert(i);
                        dataGridView2.Rows[i].Cells[0].Value = dr["P_name"].ToString();
                        dataGridView2.Focus();
                    }
                    tbMrp.Text = (dr["P_mrp"].ToString());
                    dateTimePicker2.Text = (dr["P_expdate"].ToString());
                }
            }
            getdata();
        }

我会提前感谢任何帮助。

1 个答案:

答案 0 :(得分:0)

在代码中放置一个断点,调试并检查OPN_STK是否为空。如果它是null那将是你的问题。