none
Moving a windows form (with dataset) from one project to another

    Question

  • This must be a common problem, so perhaps someone can point me to a Knowledgebase article.  I've got a form that works fine in a completed project.  It's a windows form that uses a dataset and tableadapter (which show in the tray below).   Now I'm developing a different project that requires a very similar form.  I want to "copy" that form into my new project, make some minor changes, and then save it under a new name.   So I "added" the form.  No real problem, except that the data stuff from the "tray" didn't come with it.  So I "added" the DataSet.xsd file from the first project.   It looked good on it's page, but wouldn't "Preview Data" -  no connection string.  So I manually typed in the connection string into the MyProject.Settings.Settings.   Now when I try to "Preview Data" I get an error message that it is missing "type info" for the dataset.
         How do I copy the "type info" to my new project, and is this going to be the end of it?  Does there exist to step-by-step guide to copy this kind of data intensive form from one project to another?
         Thanks for any help
    - Steve F.   
    Monday, March 15, 2010 8:26 PM

Answers

  • Steve,

    You have a couple of options.

    1) If you think you may be reusing this form and the various components associated with it in other situations as well (the new project and/or other projects), then you might consider creating a whole new project that includes this form (and possibly some other code as well).  This project would not be executable, but instead serve as a repository for your reusable classes.  You might call this MyBaseClasses or somesuch.

    In your new project, just include this master project and you are good to go.  Then add a form based on the form from the MyBaseClasses form and just modify those values that are unique to the new situation.  Over time, you will be able to fine tune the settings in that base form so it is flexible enough for a variety of needs and/or create other subclasses suited to specific needs.

    2) If you don't want to do that and just need a copy of the file, do it the easy way.  Use Windows Explorer, go to the original project, copy all 3 files that make up the form (both .vb and the .resx files) and copy them to the new project location.  Then add them to the project.  You should have everything you need, no need to reenter anything (unless you want to do so.)

    Fletcher
    Tuesday, March 16, 2010 6:09 PM
  • Hi Steve.  You need to keep in mind that a dataset is a project object in itself and is not an integral part of the form.  So there is no reason that the dataset would be available to the imported form in your new project...  unless you copied the dataset into your new project.  As you mentioned above -- you had to do this before and it required that teh connection string be added too.  And that is what prompted your original question here.

    You are essentially bumping into some of the limitations of Visual Studio in regard to project management and creation.  There is no real import wizard for creating a project based on another, nor is there an intuitive import wizard that can bring in all dependency objects (unless you import the entire other project).  I think this is by design, but I agree with you -- it probably should be a feature.  Consider that VS will intuitively bring in dependency libraries when you import a library or assembly.  So what you're asking for is not unreasonable in my opinion.

    For what its worth, I don't choose to store my connection string in the app settings during the Add Data Source wizard.  I can't stand having that dependency and it works just as fine without choosing that option (at least for me so far).  I suppose it could be useful if you were to potentially switch DB's though.
    Wednesday, March 17, 2010 2:08 AM

All replies

  • When you added the form to your new project, did you do it by right clicking on the application name in Solution Explorer, selecting Add, then selecting Existing Item...?
    Doug

    SEARCH ... then ask
    Tuesday, March 16, 2010 5:28 PM
  • Steve,

    You have a couple of options.

    1) If you think you may be reusing this form and the various components associated with it in other situations as well (the new project and/or other projects), then you might consider creating a whole new project that includes this form (and possibly some other code as well).  This project would not be executable, but instead serve as a repository for your reusable classes.  You might call this MyBaseClasses or somesuch.

    In your new project, just include this master project and you are good to go.  Then add a form based on the form from the MyBaseClasses form and just modify those values that are unique to the new situation.  Over time, you will be able to fine tune the settings in that base form so it is flexible enough for a variety of needs and/or create other subclasses suited to specific needs.

    2) If you don't want to do that and just need a copy of the file, do it the easy way.  Use Windows Explorer, go to the original project, copy all 3 files that make up the form (both .vb and the .resx files) and copy them to the new project location.  Then add them to the project.  You should have everything you need, no need to reenter anything (unless you want to do so.)

    Fletcher
    Tuesday, March 16, 2010 6:09 PM
  • I was already in the new project, so I clicked on Project and then clicked on Add Existing Item.  I just tried it again your way, but it doesn't make any difference.
    - Steve
    Tuesday, March 16, 2010 11:42 PM
  • I don't think I'll need this form again, so I would opt for your number 2.   I deleted all references to the files and started over again using your directions (and using "Add Existing Item").   Still the same, I'm afraid.  The problem seems to be in the form.designer.vb file (which I'm not supposed to change, but I've carefully tried some changes).   There's a line (near the bottom):

    Friend WithEvents InvDataSet As Inventory.InvDataSet

    InvDataSet is the dataset that is used by the form.   Inventory is the name of the first project (completed).  So I tried replacing the word Inventory with Update (the name of the project Im working on), but then VB.NET complains that it has no type information about Update.InvDataSet

    Any ideas

    - Steve

    Wednesday, March 17, 2010 12:03 AM
  • Hi Steve.  You need to keep in mind that a dataset is a project object in itself and is not an integral part of the form.  So there is no reason that the dataset would be available to the imported form in your new project...  unless you copied the dataset into your new project.  As you mentioned above -- you had to do this before and it required that teh connection string be added too.  And that is what prompted your original question here.

    You are essentially bumping into some of the limitations of Visual Studio in regard to project management and creation.  There is no real import wizard for creating a project based on another, nor is there an intuitive import wizard that can bring in all dependency objects (unless you import the entire other project).  I think this is by design, but I agree with you -- it probably should be a feature.  Consider that VS will intuitively bring in dependency libraries when you import a library or assembly.  So what you're asking for is not unreasonable in my opinion.

    For what its worth, I don't choose to store my connection string in the app settings during the Add Data Source wizard.  I can't stand having that dependency and it works just as fine without choosing that option (at least for me so far).  I suppose it could be useful if you were to potentially switch DB's though.
    Wednesday, March 17, 2010 2:08 AM
  • Steve,

    Ok, if I understand correctly, this is more related to being able to use the same basic form with a different dataset.  If so, you may want to revisit the way you are managing your data.

    DeborahK is an MVP on this forum and has written an excellent book on the subject that includes a good basic framework for doing what you want to do.  And you should find that using an approach similar to that will make your life much easier.

    As Dig-Boy pointed out, the form wizards are really a one use only approach - great for getting started, but not very flexible. 

    You might post a question related to how to create a form that can be used with multiple datasets (or something similar) and I am sure she will help you out.

    Take care,

    Fletcher
    Thursday, March 18, 2010 5:14 PM

  • See this link, particulary the response by kaymaf:  http://social.msdn.microsoft.com/Forums/en-US/Vsexpressvb/thread/9a431782-8dd7-4111-9c1d-bd346e6a36e5
    Only performance counts!
    Thursday, March 18, 2010 6:34 PM
  • Thanks Sylva, but kaymaf's suggestion doesn't work, as the original questioner (rtmos) pointed out.  In the  form.designer.vb file the name of the original project appears, associated with the dataset name.  In the new project, this (project) name is wrong, and leads to an error.  Simply substituting the name of the new project doesnt work.  It is frustrating.
    - Steve
    Thursday, March 18, 2010 9:45 PM
  • Steve,

    If you are still trying to do this and still frustrated, one thing to note.  There are 3 files that make up a VB form.  If you copy a form currently in your project to make a new one, you will find that just renaming the 3 files isn't enough.  The reason is that, even though you only edit one of them in the designer, all 3 often have references that need to be updated.

    form.vb - this holds all the code you might add by double clicking on the form.

    form.designer.vb - this is used by VS.NET to allow you to visually modify the form.  If you edit this, you run a big risk of making the form uneditable in the IDE.  On the other hand, there are settings in here that you will need to change in order to be able to use the copy of the form.  It's this file that causes people the most trouble when they make copies of forms as they are often not aware of it nor how to edit it.

    form.resx - this is an xml file that also has some values used by the IDE.  I haven't had to manually edit this when copying forms within the same project or between projects.  But if editing the other two do not help, you may find that there is a reference in here.

    There is no reason why you can't copy the forms as you wish, provided you open the 1st 2 .vb files and make sure there are no references to things that don't exist.  Either you may need to rename them (the class is the most common example) or comment them out until you can figure out what else you need to add to the project.

    Finally, if your form has references to any other classes in your original project, those classes will also need to be brought over.

    So try this.  In the new project, search for the old form name to see if there are any references to that left in the forms in the new project.  Then search on the old path name to see if you brought over anything that still has that reference.  Note, the path may be relative.

    Let me know if you are still struggling with this.

    Fletcher

    Tuesday, March 23, 2010 6:41 PM
  • form.designer.vb - this is used by VS.NET to allow you to visually modify the form.  If you edit this, you run a big risk of making the form uneditable in the IDE.  On the other hand, there are settings in here that you will need to change in order to be able to use the copy of the form.  It's this file that causes people the most trouble when they make copies of forms as they are often not aware of it nor how to edit it.

    There is no reason why you can't copy the forms as you wish, provided you open the 1st 2 .vb files and make sure there are no references to things that don't exist.  Either you may need to rename them (the class is the most common example) or comment them out until you can figure out what else you need to add to the project.

    Finally, if your form has references to any other classes in your original project, those classes will also need to be brought over.

     

    Just to add to Fletcher's good advice (and a good summary to the question's answer) -- the strongly-typed datasets are the "references" that have caused you the most headache, and there are no silver bullets for bringing these over (especially when the connection string is another dependency that gets left behind once you bring the datasets over!)

    My thought on this:  maybe you copy the datasets over before the forms and there won't be so many errors to wade through.  If you store all your datasets in a single project folder then you could copy them all together and just use the "Add to Project" menu item when you right click on the dataset class in colution explorer.

     

    Tuesday, March 23, 2010 7:05 PM
  • Thank you Fletcher and Dig-Boy for staying with me on this.  When I first copied the form (when you choose to include a .vb file into your project, VS brings all 3 files in) the form wouldn't even display.  The error message did indicate the problem line in the Designer.vb form, so I eventually commented out those lines (all to do with the dataset and tableadapter) - then the form displayed.   I copied over the dataset (.xml file) and it would display properly in the Dataset Designer, but wouldn't 'preview data', even after I had manually entered the connection string (some sort of "type" error).

    At the bottom of the Designer.vb form there is a section of statements in the form of:  "Friend WithEvents InvDataSet As Proj1.InvDataSet"  Since I now had the InvDataSet .xml file in the new project (Proj2), I tried changing Proj1 in that statement to Proj2, but it wouldn't accept it ("type" problems again) - I commented it out.  So eventually I gave up on the .xml file - it was easy enough to re-create the DataSet from scratch with the wizard.  Then I looked in the Designer.vb code.  At the bottom VS had inserted the line "Friend WithEvents InvDataSet As Proj2.InvDataSet" -  letter for letter identical to my (edited) attempt.  So it wouldn't accept that statement from me, but was fine when VS inserted it.  Obviously VS had made some other internal connections (references, etc.) that are beyond my capabilities. 

    So everything seems to be OK now / so far/  I think.   I know I'll be doing some more or this sort of thing in this Solution, and I wish VB.NET made it easier.

    - Steve

     

     

    Tuesday, March 23, 2010 11:35 PM
  • Steve,

    Keep in mind that you are using the entry level tools.  They are great for new users to be able to get up and going.  But once you get a little more advanced, you start to run into some problems (just as you did.)  That is why I tried to point you to a different approach, where you can do what you want without all the hassles you are having now. 

    If you want to continue with your approach, then I would recommend that you back up the source project, open the form you want to copy, delete the dataset references from the form, copy the form to the new project, and then build the dataset in that project and add it to the form.  (You may also want to restore the back up so the other project now works again.)

    Anyway, play with it and pick the approach that works best for you. 

    Take care,

    Fletcher

    Thursday, March 25, 2010 4:15 PM