9 Replies - 4781 Views - Last Post: 24 January 2012 - 05:13 PM Rate Topic: -----

#1 Hebco  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 5
  • Joined: 20-January 12

I can't insert item from barcode to listview

Posted 20 January 2012 - 06:21 AM

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

This post has been edited by eclipsed4utoo: 20 January 2012 - 06:31 AM
Reason for edit:: Please learn how to use the code tags


Is This A Good Question/Topic? 0
  • +

Replies To: I can't insert item from barcode to listview

#2 eclipsed4utoo  Icon User is offline

  • Not Your Ordinary Programmer
  • member icon

Reputation: 1524
  • View blog
  • Posts: 5,960
  • Joined: 21-March 08

Re: I can't insert item from barcode to listview

Posted 20 January 2012 - 06:32 AM

It would be nice if you told us WHERE you get that exception at.
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: I can't insert item from barcode to listview

Posted 20 January 2012 - 06:54 AM

Covered in the first link


TOP most asked:
What does this error message mean?
FAQ 2: How do I debug
FAQ 3: How do I make Form1 talk to Form2



FAQ (Frequently Asked Questions - Updated Jan 2012

Spoiler

This post has been edited by tlhIn`toq: 20 January 2012 - 06:55 AM

Was This Post Helpful? 0
  • +
  • -

#4 Hebco  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 5
  • Joined: 20-January 12

Re: I can't insert item from barcode to listview

Posted 20 January 2012 - 08:15 AM

When I debug it mentioned this line has an error,
 for (int i = 0; i < dtable.Rows.Count; i++)




Even though I tried to trace the problem by applying the code using Button as you can see below but still I can't figure out the error

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

            }

Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: I can't insert item from barcode to listview

Posted 20 January 2012 - 08:25 AM

View PostHebco, on 20 January 2012 - 09:15 AM, said:

When I debug it mentioned this line has an error,
 for (int i = 0; i < dtable.Rows.Count; i++)



Did you read the article I linked to you or read the debugging tutorials that were provided?
I seriously doubt that you did. You only have two things that could throw this error:
  • dtable
  • dtable.rows


Hover your mouse over them like it shows in the the first link I gave you and see which one is null.
Was This Post Helpful? 0
  • +
  • -

#6 Hebco  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 5
  • Joined: 20-January 12

Re: I can't insert item from barcode to listview

Posted 20 January 2012 - 11:58 AM

I'm sorry I haven't read your debug link page, but I already said that the FOR loop does raised the error, you can try the code that I mentioned in my second reply to trace it, thank you very much for your reply
Was This Post Helpful? -1
  • +
  • -

#7 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: I can't insert item from barcode to listview

Posted 20 January 2012 - 02:43 PM

Excuse me?! *I* can run your code to debug your problem... When you admit that you can't be bothered with learning how to debug your own programs???

I don't think so. If you're too disinterested in coding to actually learn how to debug then stop now and take up cooking or something. Nobody here is going to be your debug-monkey because you're too lazy to learn your craft.
Was This Post Helpful? 0
  • +
  • -

#8 Hebco  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 5
  • Joined: 20-January 12

Re: I can't insert item from barcode to listview

Posted 21 January 2012 - 12:59 PM

Sorry I didn't mean anything, but I was saying the truth that I haven't read because I haven't noticed it. Any I'm trying to lock at it and I'll try to figure it out and I'll return back with feedback, thanks.
Was This Post Helpful? 0
  • +
  • -

#9 Hebco  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 5
  • Joined: 20-January 12

Re: I can't insert item from barcode to listview

Posted 24 January 2012 - 12:53 PM

hi there,
The problem still there, I tried to debug but I couldn't find out where is the problem exactly, any help
Was This Post Helpful? 0
  • +
  • -

#10 tlhIn`toq  Icon User is online

  • Please show what you have already tried when asking a question.
  • member icon

Reputation: 5535
  • View blog
  • Posts: 11,857
  • Joined: 02-June 10

Re: I can't insert item from barcode to listview

Posted 24 January 2012 - 05:13 PM

The line highlighted by Visual Studio when execution stops:
That's where the problem is

Did you not look at the link I gave you?
http://www.dreaminco...-common-errors/
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1