Question Value cannot be null. Parameter name: source

Omer Butt

Active member
Joined
Sep 6, 2021
Messages
29
Programming Experience
Beginner
Hello, Please Help in this regard as I was very happy complete one phase of my work there was no error and everything worked just fine so I saved and closed the all the open instances of forms and user controls to start working on a new window form and went to have a little rest after a while when I came back I saw this error how it happens automatically by it self when I left with zero errors and now it showing me this error:
Value cannot be null. Parameter name: source

Here's a screenshot of this error:



Below in the Error List when I double Click on it it take me to the Administrator_Panel.Designer.cs Line 1092, Here's the screenshot of the code it leads to:


Administrator_Panel_Designer.cs Code that the Error List Message directs to:
       // Administrator_Panel
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 13F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.Color.White;
            this.ClientSize = new System.Drawing.Size(1366, 768);
            this.Controls.Add(this.panel4);
            this.Controls.Add(this.Panel_Admin_NavUCs);
            this.Controls.Add(this.Panel_Admin_DigClock);
            this.Controls.Add(this.Panel_Admin_NavBtns);
            this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.Name = "Administrator_Panel";
            this.StartPosition = System.Windows.Forms.FormStartPosition.CenterScreen;
            this.Text = "Administrator_Panel";
            this.Load += new System.EventHandler(this.Administrator_Panel_Load);
            this.Panel_Admin_NavBtns.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.PicBox_Main_Admin_UserPic)).EndInit();
            this.Panel_Admin_DigClock.ResumeLayout(false);
            this.Panel_DigitalClock.ResumeLayout(false);
            ((System.ComponentModel.ISupportInitialize)(this.bunifuPictureBox1)).EndInit();
            this.Panel_Admin_NavUCs.ResumeLayout(false);
            this.ResumeLayout(false);

        }

and if you see the instance of the error in call stack in first screenshot you can see it leads to the UC_Profile.cs: Line 29. Here's the screenshot of that Part of the code:

UC_Profile.cs Code:
private void UC_Profile_Load(object sender, EventArgs e)
        {
            var msUp = Login_Form.Upic;
            var bytes = msUp.ToArray();
            var ImageMemoryStream = new MemoryStream(bytes);
            Image ImgFromStream = Image.FromStream(ImageMemoryStream);
            PicBox_Profile_Picture.Image = ImgFromStream;

            Lbl_ProfileName.Text = Login_Form.UName;
            Query = "select * from Tb_Users where User_Name='" + Lbl_ProfileName.Text + "'";
            SqlDataAdapter ProfileData = new SqlDataAdapter(Query, Conn.Connect);
            DataTable dt = new DataTable();
            ProfileData.Fill(dt);
            DropDown_Profile_UserRole.Text = dt.Rows[0][1].ToString();
            Txt_Profile_Name.Text = dt.Rows[0][2].ToString();
            Txt_Profile_DOB.Text = dt.Rows[0][3].ToString();
            Txt_Profile_Mobile.Text = dt.Rows[0][4].ToString();
            Txt_Profile_Email.Text = dt.Rows[0][5].ToString();
            Txt_Profile_Password.Text = dt.Rows[0][7].ToString();

            Lbl_Profile_UserRole.Text = dt.Rows[0][1].ToString();
            Lbl_Profile_DOB.Text = dt.Rows[0][3].ToString();
            Lbl_Profile_Mobile.Text = dt.Rows[0][4].ToString();
            Lbl_Profile_Email.Text = dt.Rows[0][5].ToString();
            Lbl_Profile_UserID.Text = dt.Rows[0][0].ToString();
        }

Due to this error I am unable to open the Administrator Panel as you can see above in first screenshot Please guide how to resolve this error.....
 
Last edited:
Firstly, please don't post pictures of code. Code is text so post it as text, formatted as code. The code tags on this site allow you to specify lines to be highlighted. Code in pictures can be hard to read on mobile devices, which a number of us regulars use to view questions. Most importantly though, we can't copy code from a picture in order to run it for ourselves. It's very frustrating to have to retype code in order to test it and generally no one will do it for large chunks of code, so you make it less likely that we'll help you. We won't always need to run the code ourselves but we often will, so make sure that we can do so easily. Not being able to copy code also means that, if we want to draw your attention to a specific line in a reply, we can't copy that line from your post into our own, which is what I want to do in my next post that will address the issue.
 
Firstly, please don't post pictures of code. Code is text so post it as text, formatted as code. The code tags on this site allow you to specify lines to be highlighted. Code in pictures can be hard to read on mobile devices, which a number of us regulars use to view questions. Most importantly though, we can't copy code from a picture in order to run it for ourselves. It's very frustrating to have to retype code in order to test it and generally no one will do it for large chunks of code, so you make it less likely that we'll help you. We won't always need to run the code ourselves but we often will, so make sure that we can do so easily. Not being able to copy code also means that, if we want to draw your attention to a specific line in a reply, we can't copy that line from your post into our own, which is what I want to do in my next post that will address the issue.
Ok I am editing the Question itself and with picture I will mention the code inside the picture
 
Based on the error message, it's fairly obvious that Login_Form.Upic is null. You have shown us how that property/field is declared or populated so we have to guess what might be the issue. The fact that you are accessing it on a type seems to suggest that you have declared it static. I'm guessing that you did that just because you couldn't access it that way otherwise. In that case, you went the wrong way. The correct solution was not to change the declaration so you could access it that way. You're seeing the result of that here. The correct solution was to access the data correctly, via an instance of that type rather than via the type itself. Exactly how to do that is unclear from what you've provided.

It appears that what is happening is that you are assuming that a login form has been displayed and an image loaded before this particular form is displayed. That might turn out to be the case at run time but it's not the case at design time. When you load the form in the designer, an instance of your user control is created and loaded, so the code in the Load event handler will be executed. If you assume that some run time functionality has been invoked in code that's run at design time then you have a problem.

The real issue here is your design. There's no way that that user control should be referring to a form and getting data from it. The user control should not know what's going on outside itself. You should design it such that that data gets passed in to it, so the control itself has no need to know where it comes from. What you're doing is akin to a TextBox getting a string from a form to set its Text property instead of the for that contains it passing a string in. You need to rethink your design. Basically, object awareness should work in one direction only if at all possible. That means that if object A creates object B then a knows about B but B doesn't know about A. A control on a form should have any data it needs passed into it by that form.
 
Based on the error message, it's fairly obvious that Login_Form.Upic is null. You have shown us how that property/field is declared or populated so we have to guess what might be the issue. The fact that you are accessing it on a type seems to suggest that you have declared it static. I'm guessing that you did that just because you couldn't access it that way otherwise. In that case, you went the wrong way. The correct solution was not to change the declaration so you could access it that way. You're seeing the result of that here. The correct solution was to access the data correctly, via an instance of that type rather than via the type itself. Exactly how to do that is unclear from what you've provided.

It appears that what is happening is that you are assuming that a login form has been displayed and an image loaded before this particular form is displayed. That might turn out to be the case at run time but it's not the case at design time. When you load the form in the designer, an instance of your user control is created and loaded, so the code in the Load event handler will be executed. If you assume that some run time functionality has been invoked in code that's run at design time then you have a problem.

The real issue here is your design. There's no way that that user control should be referring to a form and getting data from it. The user control should not know what's going on outside itself. You should design it such that that data gets passed in to it, so the control itself has no need to know where it comes from. What you're doing is akin to a TextBox getting a string from a form to set its Text property instead of the for that contains it passing a string in. You need to rethink your design. Basically, object awareness should work in one direction only if at all possible. That means that if object A creates object B then a knows about B but B doesn't know about A. A control on a form should have any data it needs passed into it by that form.
After Changing the line in UC_Profile.cs from var msUp = Login_Form.Upic; to var msUp = new MemoryStream(Login_Form.Upic); and start the program and close, and this time on Administrator_Panel.cs show this Error Buffer Cannot be null. Parameter name: buffer
Buffer cannot be null. Parameter name: buffer

Instances of this error (1)
1.
Hide Call Stack​
at System.IO.MemoryStream..ctor(Byte[] buffer, Boolean writable)
at System.IO.MemoryStream..ctor(Byte[] buffer)
at PMS.UC_Profile.UC_Profile_Load(Object sender, EventArgs e) in C:\Users\faroo\OneDrive\Desktop\Graphin8 - Pharmacy Management System - v1\Graphin8_PMS\PMS\PMS\UC_Profile.cs:line 28
at System.Windows.Forms.UserControl.OnLoad(EventArgs e)
at System.Windows.Forms.UserControl.OnCreateControl()
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl(Boolean fIgnoreVisible)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.ControlCollection.Add(Control value)
at System.Windows.Forms.Form.ControlCollection.Add(Control value)
at System.Windows.Forms.Design.ControlDesigner.DesignerControlCollection.Add(Control c)
 
It's still the same problem. If Login_Form.Upic is null, which it is at design time, then you can't do anything with it that requires it to provide actual data. You need to change your design. What you did is not changing the design but simply adding another object in the layers that will still fail if there's nothing at the bottom.
 
It's still the same problem. If Login_Form.Upic is null, which it is at design time, then you can't do anything with it that requires it to provide actual data. You need to change your design. What you did is not changing the design but simply adding another object in the layers that will still fail if there's nothing at the bottom.
Just to see if the Error lies here or not, I removed the Picture Box from the design and the whole of that part of code including the part you emphasized: but the error is with something else
var msUp = new MemoryStream(Login_Form.Upic); var bytes = msUp.ToArray(); var ImageMemoryStream = new MemoryStream(bytes); Image ImgFromStream = Image.FromStream(ImageMemoryStream); PicBox_Profile_Picture.Image = ImgFromStream;
 
The issue is nothing to do with a PictureBox. As I have already stated, the issue is right here:
C#:
var msUp = new MemoryStream(Login_Form.Upic);
You appear to be assuming that Login_Form.Upic has a value but it doesn't. It is null and you can't pass null to the MemoryStream constructor. You appear to be assuming that, when that code is executed, the user has already successfully logged in and that field/property will have a value. That may be the case when you run your app for real but this is happening in the designer, so no login has occurred. For the last time, you need to change your design. Stop having controls actively retrieving data from forms like that. Do it properly and have have the form containing the control pass the data in. If your user control is going to display an image then it should have a property to receive that image and it can do any processing required when that property is set. That property will be set by the form containing the user control. It won't happen at design time but it will happen at run time. Problem solved!

You also almost certainly need to change the relationship between your login form and the other forms. Nothing should be pulling data from the login form unless it created the login form in the first place. If you have a startup form that displays the login form then the login form can expose data via properties and that startup form can get the data from them. Otherwise, the login form should put its data in a common location and other forms should get it from there. You might declare a static class or perhaps a singleton for that.

BTW, please take note of how I used the CODE and ICODE tags in my post and do the same in future. Only use the ICODE tag for inline code. For blocks of code, use the CODE tag.
 
Back
Top Bottom