【问题标题】:fetch data from two different tables on same textbox changed event从同一个文本框更改事件的两个不同表中获取数据
【发布时间】:2014-12-04 09:55:20
【问题描述】:

我想从两个不同的表 PROOPN_STK 获取一些数据,在一些控件中,比如 textboxs 和 datagridview 。当我从一个表(即PRO)中选择数据时,代码运行良好,但是当我在它旁边应用相同的代码时,在同一事件中从另一个表(即OPN_STK)获取数据时,它会抛出异常“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();
        }

我会给予任何帮助,在此先感谢。

【问题讨论】:

  • 好问题,我正在寻找它

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


【解决方案1】:

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

【讨论】:

  • 当它中断时,comboBthNo.Text 为空?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-06-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-07-11
  • 2019-05-27
相关资源
最近更新 更多