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.NullReferenceException: Object reference not set to an instance of an object."

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

You haven't highlighted which line is causing the error but it will be one of your assignment lines - where you are setting something with the = symbol.
It normally happens because the object you are trying to assign is null soI'm guessing it is either:

OracleDataAdapter xx = new OracleDataAdapter("SELECT * FROM POS WHERE barcode = " + this.textBox1.Text, conn);

or

ListViewItem lvi = new ListViewItem(drow["product_name"].ToString());

But it may be something else. Debug your code, see what line is throwing the error and make sure you are trying to assign an something (not null).

Thank you very much for your reply and care, I tried to debug the code. But I changed the code to figure out where is the problem from the below code and it tells me in this code that the FOR loop doesn't retrieve any data from the database even the table and contents are really exist there,

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());
            }
        }
    }

OK, because you aren't getting any database errors your syntax is correct so it could be that the dataset is empty based on the where clause (where brcod = 6281014472210). Check the dtable.Rows.count value is more than zero.
If not either the returned dataset is empty or setting dtable = DS.Tables["localpos"]
is the problem.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.