6 Months Free & No Setup Fees ASP.NET Hosting!
Skip Navigation Links
C# Corner Home
Forum Home
Latest 50
Unanswered
Win Prizes
All Time Leaders
Jump to CategoryExpand Jump to Category
Login 
    Welcome Guest!
 Search Forum For :  
X
 Login
Please login to submit a new post, reply and edit exiting posts, see user profiles, and access more features. If you are not a registered member, Register here.
User Id / Email:
Password:  
Forgot Password | Forgot UserName
   Home » C# Language » Object reference not set to an instance of an object.
       
Author Reply
Nel
posted 212 posts
since Feb 22, 2011 
from

Object reference not set to an instance of an object.

  Posted on: 22 Feb 2012       
Hi,

I have a picturebox in one form. When inserting data, I am filling the picturebox as well, together with other types objects, and on clikc on the picturebox I can open another window aith a whole picture of the document.
But when the data is inserted in the database, and I click on one row of the datagridview, all the objects are filled, and I want again when clicking on the picturebox to open the picture in a new window, but I get error.
"Object reference not set to an instance of an object." 
in the bold row below.

The code for the form where I open the whole picture is:

 public partial class Slik : Form
    {
        public Slik()
        {
            InitializeComponent();
            Dogovor dog = (Dogovor)Application.OpenForms["Dogovor"];
            DataGridView dgv = (DataGridView)dog.Controls["dataGridView2"];
                      
                byte[] data = (byte[])dgv.SelectedRows[0].Cells[3].Value;
                MemoryStream ms = new MemoryStream(data);
                pictureBox1.Image = Image.FromStream(ms);
           
        }

        private void Slik_Load(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Maximized;
        }     

       
    }



and the code for the first form which calls this second form is:

private void pictureBox2_Click(object sender, EventArgs e)
        {
            if (pateka != "")
            {
                Slika slika = new Slika(izbranfajl);
                slika.Show();
            }
            else
            {
               
                Slik slika1 = new Slik();
                slika1.Show();
              
            }
        }

Can anybody help me please?Thank you in advance.
Vulpes
posted  5419 posts
since  Feb 28, 2011 
from 

 Re: Object reference not set to an instance of an object.
  Posted on: 22 Feb 2012        0  
My suspicion is that no row is actually selected when that line is executed.

Try placing this line immediately above the one which is giving the error to see whether that's true or not:

MessageBox.Show(dgv.SelectedRows.Count.ToString());
Nel
posted  212 posts
since  Feb 22, 2011 
from 

 Re: Object reference not set to an instance of an object.
  Posted on: 22 Feb 2012        0  
Now the same error occures on that row, Object reference not set to an instance of an object.

But maybe it doesn't consider that any row is selected. When clicking on one row in the datagridview the values are retrieved and the combobox, datetimepicker, picturebox...are filled. And I want immediately to check the picture , so how can I open the picture when clicking on the picturebox in other way, or how to "repair" this one? Please tell me if you have any idea. Thanks
Vulpes
posted  5419 posts
since  Feb 28, 2011 
from 

 Re: Object reference not set to an instance of an object.
  Posted on: 22 Feb 2012        0  
Ah, if you're getting the same error on the MessageBox.Show, that's telling us that 'dgv' itself is null.

Is the DataGridView placed directly on your Dogovor form or is it on some other container such as a panel, groupbox or tabpage?

Nel
posted  212 posts
since  Feb 22, 2011 
from 

 Re: Object reference not set to an instance of an object.
  Posted on: 22 Feb 2012        0  
Yes, The DataGridView is placed on my Dogovor form in a tabpage/tabcontrol.
Vulpes
posted  5419 posts
since  Feb 28, 2011 
from 

 Re: Object reference not set to an instance of an object.
  Posted on: 22 Feb 2012   Accepted Answer     0  
OK, whilst it's still possible to do it by drilling down to the tab page which the DGV is on, a much easier way is to simply change the DGV's Modifiers property from Private to Internal. 

This can be done from its Properties Box and will make it directly visible to other forms or classes in the application.

You can then change your code to:

Dogovor dog = (Dogovor)Application.OpenForms["Dogovor"];
DataGridView dgv = dog.dataGridView2; // now accessing DGV directly
// etc.
Nel
posted  212 posts
since  Feb 22, 2011 
from 

 Re: Object reference not set to an instance of an object.
  Posted on: 23 Feb 2012        0  
Thank you very very much Vulpes:)
       
Dynamic PDF
ceTE software specializes in components for dynamic PDF generation and manipulation. The DynamicPDF™ product line allows you to dynamically generate PDF documents, merge PDF documents and new content to existing PDF documents from within your applications. Visit DynamicPDF here
Introducing MaxV - one click. infinite control. Hyper-V Hosting from MaximumASP.
Finally – a virtual platform that delivers next-generation Windows Server 2008 Hyper-V virtualization technology from a managed hosting partner you can truly depend on. Visit www.maximumasp.com/max for a FREE 30 day trial. Hurry offer ends soon. Climb aboard the MaxV platform and take advantage of High Availability, Intelligent Monitoring, Recurrent Backups, and Scalability – with no hassle or hidden fees. As a managed hosting partner focused solely on Microsoft technologies since 2000, MaximumASP is uniquely qualified to provide the superior support that our business is built on. Unparalleled expertise with Microsoft technologies lead to working directly with Microsoft as first to offer IIS 7 and SQL 2008 betas in a hosted environment; partnering in the Go Live Program for Hyper-V; and product co-launches built on WS 2008 with Hyper-V technology.
Nevron Chart for .NET 2010.1 Now Available
The leading .NET charting control now features PDF, Flash and Silverlight export, visualization of large datasets and more. Deliver true charting functionality to your BI, Scorecard, Presentation or Scientific apps. Download evaluation now.
ASP.NET 4 Hosting
Get 2 Months Free of ASP.NET Hosting for Only $4.95/month! Receive FREE MS SQL and MySQL Databases Including ASP.NET 4/3.5, MVC 3.0, Silverlight 4, Windows 2008/IIS 7.0 Plus FREE IIS 7 Modules. Host UNLIMITED ASP.NET Web Sites - Click Here!

 Hosted by MaximumASP  |  Found a broken link?  |  Contact Us  |  Terms & conditions  |  Privacy Policy  |  Site Map  |  Advertise with us
Current Version: 5.2011.3.12
 © 1999 - 2012  Mindcracker LLC. All Rights Reserved