-2

I'm filling datatables using queries to a database. I'm storing all my queries in a static class, then in a different class I'm sorting/filtering these tables. I ran into an issue pulling the generated datatable into that other class where I'm doing all the sorting, any calculations, etc. involving the data. How do I pull the generated tables into a button_click method in C#?

One of my queries:

string HeaderString =
            @" SELECT
            ID,
            model,
            series,
            short_name,
            published,
            VALID_DTTM
            FROM
            HEAD_TABLE;            
            
DataTable Headerdtbl = null;

if(IsConnected()) {
    try {
        string HeaderQuery = string.Format(HeaderString, Model);
        OracleDataAdapter HeaderdataAdapter = new OracleDataAdapter(HeaderQuery, A.ServerConnection.oracleConnection);
        Headerdtbl = new DataTable("HEAD");
        HeaderdataAdapter.Fill(Headerdtbl);
        }
        catch(Exception ex) {
            using ExceptionDialog exDialog = new(ex);
            exDialog.ShowDialog();
            Headerdtbl = null;
            }
        }
 return Headerdtbl;

The code I get errors with:

(This is in a different class.)

private void PopulateButton_Click(object sender, EventArgs e)
    {

        // Sort data

        DataView HeaderView = new(Headerdtbl);
        HeaderView.Sort = "PUBLISHED ASC";
    
        HeaderView = new DataView(Headerdtbl);
        HeaderView.Sort = "SHORT_NAME ASC";
    
        HeaderView = new DataView(Headerdtbl);
        HeaderView.Sort = "MODEL ASC";
        Headerdtbl = HeaderView.ToTable();

Error I get: CS0103 The name 'identifier' does not exist in the current context.

I know the most obvious solution is to just put the query in with the button click code but unfortunately I can't move the query out of the class its in as thats the designated location for querying from the database. I need a way to pull the established table into my button click method. Any good ways to do this?

Zay
  • 11
  • 4
  • Similar tasks are solved using the mvvm template – Maxim Feb 25 '23 at 05:39
  • Create a repository class that will implement all the necessary work with data and you just have to create its instance (use a singleton) or get a dependency. – Maxim Feb 25 '23 at 05:41
  • I'm pretty sure that the error you get I'd not `"CS0103 The name 'identifier' does not exist in the current context."`. I'm pretty sure that in the error you actually get, the placeholder `'identifier'` is replaced by a variable name in your code. The `'identifier'` does exist in the documentation of that error though – Flydog57 Feb 25 '23 at 05:59
  • @Flydog57 yes you are correct. The error has Headerdtbl in place of identifier. Apologies, I copied the error message straight from the help page explaining the error and didn't switch out identifier for the datatable having the error. – Zay Feb 25 '23 at 15:04
  • @Maxim do you have a good link I can use as reference for what you're describing? I'm just starting out so I don't have a personal frame of reference to understand what you're describing. I tried just googling it but I don't think I'm using the right search words as I'm not getting a good hit. – Zay Feb 25 '23 at 15:31
  • https://stackoverflow.com/questions/1405739/mvvm-tutorial-from-start-to-finish https://stackoverflow.com/questions/19444431/what-is-difference-between-mvc-mvp-mvvm-design-pattern-in-terms-of-coding-c-s – Maxim Feb 26 '23 at 07:29
  • https://stackoverflow.com/questions/3848375/looking-for-simple-mvvm-light-example?noredirect=1&lq=1 – Maxim Feb 26 '23 at 07:29

1 Answers1

0

here is the guideline from Microsoft

https://learn.microsoft.com/en-us/dotnet/api/system.data.dataview.sort?view=net-7.0

you don't need this line to execute every time

Headerdtbl = HeaderView.ToTable();

instead, you can use it at the end of the method

gridview1.DataSource= HeaderView.ToTable();
  • Thanks, that simplifies the block of code! Any input on how to pull the datatable into the button_click? – Zay Feb 25 '23 at 15:09