handle exception error in listview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • RomeoX
    New Member
    • Jan 2010
    • 25

    handle exception error in listview

    Hi everybody,

    Actually I'm trying to do small form that has a textbox for BARCODE which when I scan an item it will add on my listview but I recieved this error that I don't know what went wrong in my code

    "System.NullRef erenceException : Object reference not set to an instance of an object."


    Code:
    private void text1_down(object sender, KeyEventArgs e)
            {
                if (e.KeyCode == Keys.Enter)
                {
    
    
                    try
                    {
    
                        OracleDataAdapter xx = new OracleDataAdapter("SELECT * FROM POS WHERE barcode = " + this.textBox1.Text, conn);
                        ww = cmnd.ExecuteReader();
                        DataSet DS = new DataSet();
                        xx.Fill(DS, "pos");
                        DataTable dtable = DS.Tables["pos"];
    
                        // Display items in the ListView control
                        for (int i = 0; i < dtable.Rows.Count; i++)
                        {
                            DataRow drow = dtable.Rows[i];
    
                            // Only row that have not been deleted
                            if (drow.RowState != DataRowState.Deleted)
                            {
                                // Define the list items
                                ListViewItem lvi = new ListViewItem(drow["product_name"].ToString());
                                lvi.SubItems.Add(drow["price"].ToString());
    
                                // Add the list items to the ListView
                                listView2.Items.Add(lvi);
                            }
                        }
    
                    }
    
                    catch (Exception error)
                    {
                        MessageBox.Show(error.ToString());
                    }
                }
            }


    Could anyone help me to fix this code
  • PsychoCoder
    Recognized Expert Contributor
    • Jul 2010
    • 465

    #2
    What line does the error occur on. Set a breakpoint on line 3 and use F11 to step through your code to find out where the error is occurring.

    Comment

    • RomeoX
      New Member
      • Jan 2010
      • 25

      #3
      First of all, thank you very much for replying to my topic. Actually I changed the code to use the button instead of barcode, it mentioned the error in line 74 which is FOR loop line seems doesn't catch the any thing from the database,

      for (int i = 0; i < dtable.Rows.Cou nt; i++)

      Code:
      private void button1_Click(object sender, EventArgs e)
              {
                  try
                  {
      
                      OracleDataAdapter xx = new OracleDataAdapter("Select * From localpos where brcod = 6281014472210", conn);
                      DataSet DS = new DataSet();
                      xx.Fill(DS);
                      DataTable dtable = DS.Tables["localpos"];
      
                      // Display items in the ListView control
                      for (int i = 0; i < dtable.Rows.Count; i++)
                          
                      {
                          DataRow drow = dtable.Rows[i];
      
                          // Only row that have not been deleted
                          if (drow.RowState != DataRowState.Deleted)
                          {
                              // Define the list items
                              ListViewItem lvi = new ListViewItem(drow["p_name"].ToString());
                              lvi.SubItems.Add(drow["p_price"].ToString());
      
                              // Add the list items to the ListView
                              listView2.Items.Add(lvi);
                          }
                      }
      
                  }
      
                  catch (Exception er)
                  {
                      MessageBox.Show(er.ToString());
                  }
              }

      Comment

      Working...