runtime error object reference not set to an instance of an object c# winform

时间:2019-04-06 10:20:34

标签: c#

i have write a function but when the code hits the function it throw the exception of that object reference not set to an instance of an object i am unable to resolve it. i have also tried the file system watcher but i am not aware of it how to use it it is the error when i select the item

         '''


private void barcode_TextChanged(object sender, EventArgs e)
             {
            if (barcode.Text == "")
            {

                  }
            else
            {
                try
                {
                    dgrvSalesItemList.Visible = true;


                    conn.Open();
                    //- new in 8.1 version // Default Product QTY is 1
                    string sql = "SELECT name ,price , 1.00  as Qty, (price * 1.00)  as 'Total' FROM  Item  where (Item_id="+ Convert.ToInt32(barcode.Text)+")";

                    dataexe.ExecuteSQL(sql, conn);
                    DataTable dt = dataexe.GetDataTable(sql, conn);

                    string ItemsName = dt.Rows[0].ItemArray[0].ToString();
                    double Rprice = Convert.ToDouble(dt.Rows[0].ItemArray[1].ToString());
                    double Qty = Convert.ToDouble(dt.Rows[0].ItemArray[2].ToString());
                    double Total = Convert.ToDouble(dt.Rows[0].ItemArray[3].ToString()) * Qty;
                    label1.Text = "starting";
                    int n = Finditem(ItemsName);
                    if (n == -1)  //If new item
                    {
                        label1.Text = "-1";
                        dgrvSalesItemList.Rows.Add(ItemsName, Rprice, Qty);
                    }
                    else   {

                        label1.Text = "0";
                        int QtyInc = Convert.ToInt32(dgrvSalesItemList.Rows[n].Cells[2].Value);
                        dgrvSalesItemList.Rows[n].Cells[3].Value = (QtyInc + 1);  //Qty Increase
                        dgrvSalesItemList.Rows[n].Cells[2].Value = Rprice * (QtyInc + 1);   // Total price
                                                                                            //  dgrvSalesItemList.Rows[n].Cells[4].Value = Itemid;                     

                        double qty = Convert.ToDouble(dgrvSalesItemList.Rows[n].Cells[3].Value);

                    }
                }

                catch(Exception ex)
                {
                    MessageBox.Show("Error " + ex);
                }
                conn.Close();
            }
             }
             public int Finditem(string item)
             {
                 int k = -1;
                 if (dgrvSalesItemList.Rows.Count > 0)
                 {
                     foreach (DataGridViewRow row in dgrvSalesItemList.Rows)
                     {
                         if (row.Cells[0].Value.ToString()==item)
                         {
                             k = row.Index;
                            // break;
                         }
                     }
                 }
                 return k;
             }

   '''

0 个答案:

没有答案