Converting to C#

cPubis

Member
Joined
Dec 26, 2021
Messages
7
Programming Experience
10+
Hi all,

I’m not sure if this is the right forum to post this in, but here it is anyway. Please move it to the proper forum if it’s not.

Well, I have a huge vb.net project (about 120,000 lines) I wanted to convert to C#. Among many conversion tools on the web, one of them was the best since it converted it with no errors (or that’s what it told me). Now my project is fully converted with no compile errors at design time. It even runs correctly and it loaded the login form which connects to the database and asks for the username/password normally. But once I click the login button (which takes the user to the main form) it tries to load the main form and stops at:

InitializeComponent();

Error:

Value cannot be null.
Parameter name: dataSource

I must mention that the main form could not be loaded even at design time. It just shows the warning message:

To prevent possible data loss before loading the designer, the following errors must be resolved

And hers’s the exception details:

Exception Details:
System.ArgumentNullException was unhandled
Message=Value cannot be null.
Parameter name: dataSource
Source=System.Windows.Forms
ParamName=dataSource
StackTrace:
at System.Windows.Forms.BindingContext.HashKey..ctor(Object dataSource, String dataMember)
at System.Windows.Forms.BindingContext.EnsureListManager(Object dataSource, String dataMember)
at System.Windows.Forms.BindingContext.UpdateBinding(BindingContext newBindingContext, Binding binding)
at System.Windows.Forms.Control.UpdateBindings()
at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
at System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
at System.Windows.Forms.Control.OnParentBindingContextChanged(EventArgs e)
at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
at System.Windows.Forms.Control.set_BindingContextInternal(BindingContext value)
at System.Windows.Forms.ContainerControl.get_BindingContext()
at System.Windows.Forms.Control.get_BindingContextInternal()
at System.Windows.Forms.Control.get_BindingContextInternal()
at System.Windows.Forms.Control.get_BindingContextInternal()
at System.Windows.Forms.Control.get_BindingContextInternal()
at System.Windows.Forms.Control.UpdateBindings()
at System.Windows.Forms.Control.OnBindingContextChanged(EventArgs e)
at System.Windows.Forms.ListControl.OnBindingContextChanged(EventArgs e)
at System.Windows.Forms.Control.CreateControl()
at System.Windows.Forms.Control.WmShowWindow(Message& m)
at System.Windows.Forms.Control.WndProc(Message& m)
at System.Windows.Forms.ComboBox.WndProc(Message& m)
at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
InnerException:

To see what’s happening I clicked on “Ignore and continue” and the form loaded but the main TabControl was empty with just few lines showing the same exception details mentioned above.

The main TabControl has many TabPages and DGVs and lots of bindingsources all done through the designer using one connection to a MySql database.

I spent the last two weeks learning the basics of C# but I got bored since it was all just basics (you know loops if conditions etc...) But now I decided to take a huge step and I was having a great hope that my project will work so I could learn C# in action to see how it does things comparing to vb.net but here I am :(

Thank you[/B]
 
Last edited by a moderator:
Without seeing the code, and just seeing the error, we would just be speculating. My speculation is that there is some "global" variable or set of "global" variables that the login form and the main form shares, but the conversion tool didn't realize this and thus the main form doesn't have what it needs to initialize properly.
 
My speculation is that there is some "global" variable or set of "global" variables that the login form and the main form shares, but the conversion tool didn't realize this and thus the main form doesn't have what it needs to initialize properly.
There are some. In the vb project I maintain some global variables in a module. But the conversion tool was smart enough to convert that module to a public static class and add its name before the variable's names when they're used in the forms. No errors of undefined variables neither in design time or run time.

I think the source of the problem is the bindingsources, since all the controls that are contained in the TabControl are bound to the database through bindingsources. It looks like the designer can't find those as after ignoring the warning and the form loads, only the unbound controls are shown. The problem is that the designer does not give me the TabControl so I could add/correct the bindings manually through the designer the way I did in the vb project in the beginning.
 
Here:
 

Attachments

  • Capture.JPG
    Capture.JPG
    576.7 KB · Views: 75
That's an error message, not a warning message.
 
Anyway, you should still be able open the generated code. I don't recall the hotkey to basically "View Code" for a form instead of defaulting to opening the WinForms Designer. Try right clicking on the form file in the Solution explorer. It will probably have the "View Code" option there as well.
 
There doesn't seem much point in ignoring a message about parameter named dataSource being null. Surely something will go very awry, such as no data being available or a slew of subsequent exceptions or even a crash. I don't understand why VS even suggests this as an option, and then proceeds to warn you it may be lethal 😵 Reminds me of a packet of cigarettes with its big black "Tobacco will kill you" warning. By that time it's already too late for debugging anyway.
 
There doesn't seem much point in ignoring a message about parameter named dataSource being null. Surely something will go very awry, such as no data being available or a slew of subsequent exceptions or even a crash. I don't understand why VS even suggests this as an option, and then proceeds to warn you it may be lethal 😵 Reminds me of a packet of cigarettes with its big black "Tobacco will kill you" warning. By that time it's already too late for debugging anyway.
I know and I agree. I wouldn't even think of continuing with the project after ignoring the designer error message. I ignored them just to have a look at what was left of the main form that did not get corrupted. Well, it was all fine except all the unbound controls. So I knew where to look. So after that sneak peek I restored a backup copy and continued debugging (without ignoring the error message this time).

I think now I should work on the main_form.designer.cs code to see what's wrong with all binding during the controls initializing although I have bad experience with editing these files as the designer doesn't like editing them manually at all. Thank you.
 
Indeed editing the xxx_designer.cs is discouraged. But just as with editing the registry, small changes are mostly fine. I do it sometimes, e.g. to change control size and dimensions (sometimes easier this way to get them all the same size) or rename/delete an event handler. But yeah, if you really stir things up VS may well get in a strop. Be careful out there.
 
Hooray! I did it. It was a bloody debugging night!

It turned out to be the properties bindings in the settings file. The conversion tool didn’t put the file in the correct folder and didn’t set the correct namespace in it. The designer didn't give me any hint, but once I altered the code in the settings.designer.cs file the errors appeared.

I resolved the problem by deleting the settings file altogether then create a new one through the designer and entered all my settings there.

Now the form is showing and everything is fine. :)

Thank you guys.
 

Latest posts

Back
Top Bottom