Connecting to Database in Visual Studio 2010

jcs

Member
Joined
Jun 23, 2012
Messages
7
Programming Experience
Beginner
I am fairly new to C# and just as new to Visual Studio and am working on a project for personal development. I am following the instructions at the following page to set up a connection from a windows form to the database:
How to: Create and Execute an SQL Statement that Returns Rows

I have a DB called Taxdb, table called Federal, and I created a table adapter called Federal1.TableAdapter. (Original, I know.) I went through the wizard to create a query, and called it GetFedSingle. So when I got to the part where I put code in the form, I thought it would be a slam dunk by putting this line of code in:

TaxdbDataSetTableAdapters.Federal1TableAdapter tableAdapter = new TaxdbDataSetTableAdapters.Federal1TableAdapter();
TaxdbDataSetTableAdapters.Federal1TableAdapter.GetFedSingle();

However, the two errors I am getting are these:

Invalid token '(' in class, struct, or interface member declaration
'Tax3.TaxdbDataSetTableAdapters.Federal1TableAdapter.GetFedSingle(Tax3.TaxdbDataSet.Federal1DataTable)' is a 'method' but is used like a 'type'


I put this code as the first line of the meat of the form code, above the variable definitions:

namespace Tax3
{
public partial class TaxResults : Form
{
TaxdbDataSetTableAdapters.Federal1TableAdapter tableAdapter = new TaxdbDataSetTableAdapters.Federal1TableAdapter();
TaxdbDataSetTableAdapters.Federal1TableAdapter.GetFedSingle();



I'm sure I am forgetting something really stupidly easy. Does anyone have any suggestions?
 
You're trying to execute a query that returns presumably one row but, even if the compiler accepted that code, where exactly are you expecting that row to go? You're not putting it anywhere so the code wouldn't do you any good anyway.

When you create a TableAdapter for a database table, it will create two methods by default: Fill and GetData. They both execute a query that retrieves all rows from the table with the difference being that Fill populates an existing DataTable that you provide and GetData creates and returns a populated DataTable. When you add new queries you get to decide whether to a method that populates an existing DataTable, a method that returns a new DataTable or both. While it's up to you what you name them, I strongly recommend following the convention for clarity and consistency. For instance, if your query gets the data with a specific ID value then you would name the methods FillById and GetDataById.

It appears that the GetFedSingle method that you've created is a Fill-style method, as indicated by the method signature that the error message shows you:
'Tax3.TaxdbDataSetTableAdapters.Federal1TableAdapter.GetFedSingle(Tax3.TaxdbDataSet.Federal1DataTable)' is a 'method' but is used like a 'type'
It's telling you that you need to pass a DataTable to the method as an argument. That DataTable will be filled with the result set of the query and you then display its contents to the user in whatever way is appropriate.
 
I plan to eventually use the row returned to create an array. (Currently I just have a ton of hard-coded individual variables.)
For now, once I get rid of the above errors, I will put in code to display the results.


I have the table adapter set up to do both Get and Fill since I didn't know which one I was going to need at the time.


So if I understand you right, I should put in the parentheses the name of the table the data will fill? I don't think I am understanding you correctly because this is what I came up with:




TaxdbDataSetTableAdapters.Federal1TableAdapter tableAdapter = new TaxdbDataSetTableAdapters.Federal1TableAdapter();
TaxdbDataSetTableAdapters.Federal1TableAdapter.GetFedSingleByYearAndStatus(TaxdbDataSet.Federal1DataTable);


Result: Same result as before.




So then I tried the same thing but slightly different after having noticed the MS example used "tableAdapter" instead of "TaxdbDataSetTableAdapters.Federal1TableAdapter". This got a slightly different result.


TaxdbDataSetTableAdapters.Federal1TableAdapter tableAdapter = new TaxdbDataSetTableAdapters.Federal1TableAdapter();
tableAdapter.GetFedSingleByYearAndStatus(TaxdbDataSet.Federal1DataTable);


Result:
Tax.AnnualTaxResults.tableAdapter' is a 'field' but is used like a 'type'
 

Latest posts

Back
Top Bottom