How Does Visual Studio/VB Create and Initialize a Table Adapter? - vb.net

I have a very simple Windows form application that uses a data set. I am having trouble figuring out how Visual Studio creates a new Table Adapter object and then initializes that object through its insertion of initialization code into the .Designer.vb files. My problem is I have inadvertently caused this to happen through some series of commands I do not remember, and am trying to figure out how to re-create what I did.
For example, if I want the data set to be used by a form, I know this goes in the form's Designer.vb module
Friend WithEvents StreetsTableAdapter1 As WindowsApplication1.DataSet1TableAdapters.streetsTableAdapter
However, this line alone won't instantiate the table adapter object.
As to the data set, I've created a one; configured one of its table's table adapters to use SQL queries; tested those queries; and they work.
I'm more interested in what Visual Studio writes to create the new object rather than the keystrokes to do it, because I'd like to know what's going on underneath the covers, rather than relying on keystroke magic.
I have a workaround
Me.StreetsTableAdapter1 = New DataSet1TableAdapters.streetsTableAdapter
I've added this to the form's .Designer.vb file. I'm still trying to figure out what I did in Visual Studio to get all this initialized through running some specific commands.
A pointer to an example would help, as well as the steps to make this happen. Then, I'll examine what Visual Studio does, so I do not get stuck again.

The easiest way to create the table adapter on your form is to let Visual Studio do it for you. In the toolbox "DataSources", you should see your current data. You can expand the nodes and drag the fields/tables to your form. This will create the table adapter and everything needed behind the scenes.

Related

Open a VFP Database Container with OpenData event coded

I am migrating data from a Visual FoxPro Data Base Container to Excel using VBA.
After trying and failing various settings of the Connection String in order to open the Container, I discovered that at Container was placed code at OpenData event; this code asks a password to open the Container. This prevents other programs open the Container for access to data, including the Password parameter of the Connection String.
Are there a way to open the Data Base Container with OpenData coded event? If the answer can be implemented in VBA, please let me know how I can do it.
Thanks for your attention.
PD: Forgive my english. Is not so good.
Solution details would probably depend on what the (custom) "Stored Procedure" code in the Vfp database is doing exactly, where one way could be simply removing it. If you got a Visual FoxPro IDE, you could for example do so by using the IDE's "Command Window":
MODIFY DATABASE ?
and then right-click the Database Designer window, choose the desired "Event" in the Properties dialog -> "Edit Code".
If then for example the code would simplified look like
PROCEDURE dbc_BeforeOpenTable(cTableName)
*Just before a table or view is opened. Return .F. to prevent table or view being opened.
RETURN INPUTBOX("Password") == "Test"
ENDPROC
... you could simply
RETURN .T.
instead

How to refresh a dataset to reflect external changes to the database?

In a WinForms application that uses a dataset from a data source created using the Data Source Configuration Wizard and connected to an Access database. How can I refresh the dataset at run time to reflect changes (data changes only, no structural changes) to the database occurred outside without closing and reopening my application?
Thank you ahead of time for your help.
Call the .fill command on the tableadapter that the wizard should have created.
By default the code is placed in the load event of the form that you added the tableadapter and dataset to, so either copy paste or creat a new sub and call that.
Edit:
There is a clearBeforeFill property of any tableadapter listed under the TableadapterManager in the Form designer if you added it via the designer, if not then you'll have to set it in your code behind.
in the code behind there are several events that can occur on your binding source, I would suggest putting the needed code on the currentItemChanged event in order to commit changes in real time.

Issue with OpenFileDialog and threading

I just upgraded from VS 2005 to VS 2012. This is a new issue that I do not understand. I am using the default "Form1" class the VS automatically creates. I added a button to open a file open dialog and when I click the button I get this error:
Current thread must be set to single thread apartment (STA) mode before OLE calls can be >made. Ensure that your Main function has STAThreadAttribute marked on it. This exception >is only raised if a debugger is attached to the process.
I have added " to Public Class Form1:
<STAThread()> Public Class Form1
But I get this...
Attribute 'STAThreadAttribute' cannot be applied to 'Form1' because the attribute is not >valid on this declaration type.
I have searched but get some info telling me that I need to set the entry point (Form1 I believe) to be Single Thread Attribute but the above code does not work.
How?
The <STAThread()> attribute cannot be added to classes like your form. It only works when it is applied to the Main function, which is the entry point of your application.
But VB.NET hides this function from you because it is rare that one needs to mess with Main in a WinForms application. It is just needed to get the plumbing set up for your app, which the compiler can manage for you. This is controlled by the "Application Framework" checkbox in the project options. If this is checked, the compiler automatically generates the Main function and the required plumbing. You can disable this option, but it makes life quite a bit harder for the average WinForms developer because you'll have to write and maintain your own Main function.
The real question here is why this is a problem at all. The compiler-generated Main function for a WinForms application is always going to have the STAThread attribute applied to it. That is just how the WinForms framework is designed to run. If that is not happening, then there is something badly wrong with your project. I would recommend scrapping it and starting over letting Visual Studio create a new WinForms project from one of the built-in templates. Everything should Just Work™.
The other option, of course, is that you're trying to display the OpenFileDialog on a separate thread (other than your main UI thread). But from your description in the question (adding a button to the form to display the dialog), it doesn't sound like this is the case. Regardless, the solution is not to do that. For example, if you're using a BackgroundWorker to do work on a non-UI thread in order to keep the UI responsive, that's great, but you'll want to do all of the UI stuff like showing an OpenFileDialog on the main UI thread before invoking the BackgroundWorker. There is a way to set a particular thread's apartment state using the SetApartmentState function, but I really don't recommend showing an OpenFileDialog on a background thread.

VB.NET UserControl

I am trying to create a UserControl in VB.net, under VS2010. I have the code for the UserControl and I would like to add it to a form. My problem is that, according to every book and forum I have seen, after I build the UserControl, it should show up in the Toolbox. It doesn't. I even downloaded code from a book, the code executes perfectly, but their TrafficLight control doesn't go in the Toolbox (even though the book says it should - and that the only way to set its properties and to add it to the form is through the Control properties). I have tried to add the control to the form manually, by declaring it
Dim myObj As New SomeClass.SomeControl
and in the Designer.vb, identical with the buttons on the form:
Friend WithEvents myObj As SomeClass.SomeControl
With both, I get an error saying
'myObj' is already declared as 'Friend WithEvents myObj As SomeControl' in this class.
And either way, I get an error when I try to look at the design:
Could not find type 'SomeClass.SomeControl'. Please make sure that the assembly that contains this type is referenced. If this type is part of your development project, make sure the project has been successfully built using settings for your current platform or AnyCPU.
The control by itself builds and shows up in design view (not in the Toolbox though, even though it Imports System.ComponentModel and Inherits System.Windows.Forms.UserControl and... what else ? I tried to build it in a separate project, to see if I creating a separate dll will make a difference, though I really want it in the same project.
Please help ! (BTW I have reinstalled VS2010 and it did no good)
Thank you.
Look in Tools / Options / Windows Forms Designer and set "AutoToolboxPopulate" to True (but note that this can take a noticeable amount of time if you haver many (i.e. dozens) of user controls.

Referencing user controls that are contained within the same VB.NET project

I do apologize if this post is a duplicate, but I haven't found anything similar when I searched.
I'm fairly new to VB.NET and I'm currently playing around with user controls, figuring out good programming practices. As far as I understand, to create and use a UserControl, I need to create a project with the UserControl in it, then build the project and use that DLL (add it to Toolbox or otherwise).
My question is this: Is there a way a have a project (a Form with a bunch of things on it) that contains a UserControl written in a *.vb file inside that same Project? If you do that, the DLL (in my case) never gets produced, possibly because the UserControl is never used and building it is simply omitted. Is it perhaps a bad practice to do that altogether? It simply makes sense to me to keep a UserControl as a part of the Project that uniquely uses it. Is there a reason not to do that?
Thanks in advance! = )
SOLUTION:
Visual Studio does not automatically include your own controls to the toolbox by default! In order to change that, go to Tools>Options>Windows Form Designer>General and set AutoToolboxPopulate to True. When you build your project next time, your new Control will appear in your Toolbox.
It's a perfectly valid design decision to include a UserControl in a WinForm or WPF project that uses it. If you do this then VS will not create a DLL for the UserControl; instead the UserControl will be built into the assembly your project is producing.
If you did want to reuse a UserControl in multiple projects then you would want to create separate project that generates a DLL that can be reused.