Pagina 1 di 2 1 2 ultimoultimo
Visualizzazione dei risultati da 1 a 10 su 13
  1. #1
    Utente di HTML.it L'avatar di orcim
    Registrato dal
    May 2003
    Messaggi
    1,692

    [C#] The name 'VarPerc' does not exist in the current context

    Un saluto a tutti e grazie del vostro tempo.

    Dovrei valorizzare l'output di questa query select in mysql, ma mi dà errore, cosa sbaglio?

    Compiler Error Message: CS0103: The name 'VarPerc' does not exist in the current context
    Version Information: Microsoft .NET Framework Version:4.0.30319; ASP.NET Version:4.0.30319.1
    codice:
           String myQuery = " SELECT " +
                             " CONCAT(VariazionePercentuale,'%') As VarPerc " +                         
                             " FROM tbl_m " +
                             " WHERE " +
                             " tNome LIKE '%Pippo%'; ";
    
    
           OdbcConnection myConnectionString = 
           new OdbcConnection(
           ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString);
           myConnectionString.Open();
           
           OdbcCommand objCmd = new OdbcCommand(myQuery, myConnectionString);
           objCmd.Prepare();
           objCmd.CommandType = CommandType.Text;
           objCmd.CommandText = myQuery;
    
           objCmd.Parameters.Add("?VarPerc", OdbcType.Text).Value = VarPerc.ToString();
           
           Chart1.DataSource = objCmd.ExecuteReader();
    "Ubi Maior, Minor Cessat"
    Domandare è lecito, rispondere è cortesia...
    A tutti è dovuta una risposta, comunque...

    “Dio gradisce molto di più le bestemmie dell’uomo disperato che non le lodi del benpensante la domenica mattina durante il culto“ Martin Lutero

  2. #2
    Moderatore di ASP.net L'avatar di djciko
    Registrato dal
    Nov 2002
    Messaggi
    6,886
    ma VarPerc dove è dichiarata ?
    io la vedo soltanto come pezzo di una stringa composta...

  3. #3
    Utente di HTML.it L'avatar di orcim
    Registrato dal
    May 2003
    Messaggi
    1,692
    Originariamente inviato da djciko
    ma VarPerc dove è dichiarata ?
    io la vedo soltanto come pezzo di una stringa composta...
    Grazie per la risposta, come sempre hai centrato il problema.

    La variabile `VarPerc` dovrebbe assumere il valore di questa stringa sql compresa nella query:

    codice:
    ... CONCAT(VariazionePercentuale,'%') As VarPerc ...
    E' questo che non riesco a capire: come assegno ad una variabile il valore conseguente all'esecuzione di una query, variabile da riutilizzare più avanti nel codebehind ?

    "Ubi Maior, Minor Cessat"
    Domandare è lecito, rispondere è cortesia...
    A tutti è dovuta una risposta, comunque...

    “Dio gradisce molto di più le bestemmie dell’uomo disperato che non le lodi del benpensante la domenica mattina durante il culto“ Martin Lutero

  4. #4
    Moderatore di ASP.net L'avatar di djciko
    Registrato dal
    Nov 2002
    Messaggi
    6,886
    Ricavandone il valore dal risultato dell'istruzione > objCmd.ExecuteReader();
    (il risultato di questo metodo deve andare in un DataReader).

    codice:
           string myQuery = "SELECT etc";
    
           .........
    
           OdbcDataReader reader = cmd.ExecuteReader();
          
           while (reader.Read())
               Response.Write(reader["VarPerc"].ToString());
    
           reader.Close();

  5. #5
    Utente di HTML.it L'avatar di orcim
    Registrato dal
    May 2003
    Messaggi
    1,692
    Grazie, ma probabilmente non ho capito...
    Exception Details: System.IndexOutOfRangeException: VarPerc
    codice:
    String myQuery = " SELECT " +
                             " CONCAT(VariazionePercentuale,'%') As VarPerc " +                         
                             " FROM tbl_m " +
                             " WHERE " +
                             " tNome LIKE '%Pippo%'; ";
    
           OdbcConnection myConnectionString = 
           new OdbcConnection(
           ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString);
           myConnectionString.Open();
           
           OdbcCommand objCmd = new OdbcCommand(myQuery, myConnectionString);
           objCmd.Prepare();
           objCmd.CommandType = CommandType.Text;
           objCmd.CommandText = myQuery;
    
           OdbcDataReader reader = objCmd.ExecuteReader();
           
           while (reader.Read())
               Response.Write(reader["VarPerc"].ToString());
           reader.Close();
           
           Chart1.DataSource = objCmd.ExecuteReader();
    "Ubi Maior, Minor Cessat"
    Domandare è lecito, rispondere è cortesia...
    A tutti è dovuta una risposta, comunque...

    “Dio gradisce molto di più le bestemmie dell’uomo disperato che non le lodi del benpensante la domenica mattina durante il culto“ Martin Lutero

  6. #6
    Moderatore di ASP.net L'avatar di djciko
    Registrato dal
    Nov 2002
    Messaggi
    6,886
    per ora elimina l'ultima riga, il bind al Chart1

    (e segui il codice passo passo con il debug, per vedere DOVE ti da' quell'errore).

    altra cosa: hai provato la query in SQL Server ?



    In ultima spiaggia, prova:

    Response.Write(reader[0].ToString());

  7. #7
    Utente di HTML.it L'avatar di orcim
    Registrato dal
    May 2003
    Messaggi
    1,692
    Ok, grazie dell'aiuto.

    1) La query provata in mysql funziona correttamente;
    2) Ho aggiunto nella query un altro campo della tabella (tNome);
    3) Ho eliminato dalla query il CONCAT(VariazionePercentuale,'%') As VarPerc;
    3) Eliminata l'ultima riga ossia il bind al Chart1;
    4) Provato con Response.Write(reader[1].ToString());
    5) Ripristinata l'ultima riga ossia il bind al Chart1;

    Mi stampa il valore del campo `VarPerc` -59 (corretto).

    Quindi per esclusione posso dire che la sintassi CONCAT(VariazionePercentuale,'%') di mysql non è compatibile con net?

    Adesso però devo utilizzare il valore del campo `VarPerc` più avanti nel codebehind e ho scritto così:

    codice:
    String myQuery = " SELECT tNome, " +
                             " VariazionePercentuale As VarPerc " +                         
                             " FROM tbl_m " +
                             " WHERE " +
                             " tNome LIKE '%Pippo%'; ";
    
           OdbcConnection myConnectionString = 
           new OdbcConnection(
           ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString);
           myConnectionString.Open();
           
           OdbcCommand objCmd = new OdbcCommand(myQuery, myConnectionString);
           objCmd.Prepare();
           objCmd.CommandType = CommandType.Text;
           objCmd.CommandText = myQuery;
    
           OdbcDataReader reader = objCmd.ExecuteReader();
           
           while (reader.Read())
               Response.Write(reader["VarPerc"].ToString());
           reader.Close();
    
           String top = reader["VarPerc"].ToString();
           LegendItem newItem = new LegendItem();
           newItem.ImageStyle = LegendImageStyle.Marker;
           newItem.MarkerStyle = MarkerStyle.Diamond;
           newItem.Cells.Add(LegendCellType.SeriesSymbol, "", ContentAlignment.MiddleLeft);
           newItem.Cells.Add(LegendCellType.Text, "top", ContentAlignment.MiddleLeft);
           newItem.Cells[1].CellSpan = 2;
           newItem.Cells.Add(LegendCellType.Text, "", ContentAlignment.MiddleLeft);
           newItem.Cells.Add(LegendCellType.Text, top, ContentAlignment.MiddleLeft);
           Chart1.Legends[0].CustomItems.Add(newItem);
    Errore:
    No data exists for the row/column.
    Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

    Exception Details: System.InvalidOperationException: No data exists for the row/column.

    Source Error:

    Line 692: String top = reader["VarPerc"].ToString();
    Line 693: LegendItem newItem = new LegendItem();
    Line 694: newItem.ImageStyle = LegendImageStyle.Marker;
    "Ubi Maior, Minor Cessat"
    Domandare è lecito, rispondere è cortesia...
    A tutti è dovuta una risposta, comunque...

    “Dio gradisce molto di più le bestemmie dell’uomo disperato che non le lodi del benpensante la domenica mattina durante il culto“ Martin Lutero

  8. #8
    hai messo un

    String top = reader["VarPerc"].ToString();

    dopo un

    reader.Close();

    per forza che ti da errore, il reader è chiuso!!!!!

  9. #9
    Utente di HTML.it L'avatar di orcim
    Registrato dal
    May 2003
    Messaggi
    1,692
    Originariamente inviato da Gluck74
    hai messo un

    String top = reader["VarPerc"].ToString();

    dopo un

    reader.Close();

    per forza che ti da errore, il reader è chiuso!!!!!
    E' vero, grazie !!!

    Ma cosa devo mettere allora per portarmi dietro la variabile reader["VarPerc"].ToString(); in tutto il codebehind?

    Perchè provo così ma dà errore:

    Exception Details: System.InvalidOperationException:
    There is already an open DataReader associated with this Command which must be closed first.

    Source Error:

    Line 599: Chart1.DataSource = objCmd.ExecuteReader();
    codice:
    String myQuery = " SELECT tNome, " +
                             " VariazionePercentuale As VarPerc " +                         
                             " FROM tbl_m " +
                             " WHERE " +
                             " tNome LIKE '%Pippo%'; ";
    
           OdbcConnection myConnectionString = 
           new OdbcConnection(
           ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString);
           myConnectionString.Open();
           
           OdbcCommand objCmd = new OdbcCommand(myQuery, myConnectionString);
           objCmd.Prepare();
           objCmd.CommandType = CommandType.Text;
           objCmd.CommandText = myQuery;
    
           OdbcDataReader reader = objCmd.ExecuteReader();
           
           while (reader.Read())
               Response.Write(reader["VarPerc"].ToString());
      
           String top = reader["VarPerc"].ToString();
           LegendItem newItem = new LegendItem();
           newItem.ImageStyle = LegendImageStyle.Marker;
           newItem.MarkerStyle = MarkerStyle.Diamond;
           newItem.Cells.Add(LegendCellType.SeriesSymbol, "", ContentAlignment.MiddleLeft);
           newItem.Cells.Add(LegendCellType.Text, "top", ContentAlignment.MiddleLeft);
           newItem.Cells[1].CellSpan = 2;
           newItem.Cells.Add(LegendCellType.Text, "", ContentAlignment.MiddleLeft);
           newItem.Cells.Add(LegendCellType.Text, top, ContentAlignment.MiddleLeft);
           Chart1.Legends[0].CustomItems.Add(newItem);  
    
    
           objCmd.Dispose();
           objCmd.Cancel();
                  
           reader.Dispose();
           reader.Close();
                               
           myConnectionString.Dispose(); 
           myConnectionString.Close();
    "Ubi Maior, Minor Cessat"
    Domandare è lecito, rispondere è cortesia...
    A tutti è dovuta una risposta, comunque...

    “Dio gradisce molto di più le bestemmie dell’uomo disperato che non le lodi del benpensante la domenica mattina durante il culto“ Martin Lutero

  10. #10
    Moderatore di ASP.net L'avatar di djciko
    Registrato dal
    Nov 2002
    Messaggi
    6,886
    dopo la execute e prima di fare tutte le chiusure:

    codice:
    Chart1.DataSource = reader;
    Chart1.DataBind();


    ---

    Rendi pubblica (prima di tutti i metodi) la variabile 'top' per averla a disposizione in tutto il code-behind (dopo averla valorizzata ovviamente). Ma non ne vedo la necessità...

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2024 vBulletin Solutions, Inc. All rights reserved.