12
Answers

The name 'VarPerc' does not exist in the current context

Hello there, thanks for your time.

First of all I must say that I am a newbie when it comes to ASP NET. 
Here is my problem in attachment file.

Your help would be very appreciated.

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

Attachment: codes.zip

Answers (12)
0
Vulpes

Vulpes

NA 98.3k 1.5m 13y
OK, let's try replacing these lines:
      
       while (reader.Read())
           Response.Write(reader["VarPerc"].ToString());
  
       String top = reader["VarPerc"].ToString();

with these:

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

       while (reader.Read())
           Response.Write(reader["VarPerc"].ToString());
Accepted
0
Chevy Mark Sunderland

Chevy Mark Sunderland

NA 160 110.6k 13y
Hi Vulpes and many thanks for your interest.

The problem now is solved your code works properly with the latest changes, but I have problem with the remote hosting dbms MySql not updated by hosting webmaster. I'm sorry.

Thanks a lot !!!
Cheers
0
Vulpes

Vulpes

NA 98.3k 1.5m 13y
I'm not familiar with the Charting API so I don't really understand the code you have there.

However, I notice that 'top' appears in this line:

  newItem.Cells.Add(LegendCellType.Text, top, ContentAlignment.MiddleLeft);

Is that in fact the line which controls what's displayed at the lower right hand corner of the chart?

If it is and nothing is being displayed, then it would suggest that 'top' is either null or an empty string.
0
Chevy Mark Sunderland

Chevy Mark Sunderland

NA 160 110.6k 13y
Thank you very much for your help. 

With your last suggestion, I don't have error in the page asp net.

But I don't print the value of the `String top = reader["VarPerc"].ToString();`

I attachment new output image: 

1) upper left side (top to left) I have the correct output of  `reader["VarPerc"].ToString();` "-59";
2) lower right side (bottom to right) I don't have the same value.

The new code:
       OdbcConnection myConnectionString = 
       new OdbcConnection(
       ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString);
       myConnectionString.Open();
       
       OdbcCommand objCmd = new OdbcCommand(strQuery, myConnectionString);
       objCmd.Prepare();
       objCmd.CommandType = CommandType.Text;
       objCmd.CommandText = strQuery;

       OdbcDataReader reader = objCmd.ExecuteReader(); // create first reader
       reader.Read();
       String top = reader["VarPerc"].ToString();
       Response.Write(top);

       while (reader.Read())
           Response.Write(reader["VarPerc"].ToString());
       reader.Close();
       reader.Dispose(); // make sure the first reader has been released

       LegendItem newItem = new LegendItem();
       newItem.ImageStyle = LegendImageStyle.Marker;
       newItem.MarkerStyle = MarkerStyle.Diamond;
       newItem.Cells.Add(LegendCellType.SeriesSymbol, "", ContentAlignment.MiddleLeft);
       newItem.Cells.Add(LegendCellType.Text, "?% DTR", ContentAlignment.MiddleLeft);
       newItem.Cells[1].CellSpan = 1;
       newItem.Cells.Add(LegendCellType.Text, "", ContentAlignment.MiddleLeft);
       newItem.Cells.Add(LegendCellType.Text, top, ContentAlignment.MiddleLeft);
       Chart1.Legends[0].CustomItems.Add(newItem);
                                         
       Chart1.DataSource = objCmd.ExecuteReader(); // create another reader

       objCmd.Dispose();

       myConnectionString.Close();
       myConnectionString.Dispose();
0
Chevy Mark Sunderland

Chevy Mark Sunderland

NA 160 110.6k 13y
Hi Vulpes. Thanks for your time.

I tried your code, but I have new error: 

       OdbcConnection myConnectionString = 
       new OdbcConnection(
       ConfigurationManager.ConnectionStrings["ConnMySQL"].ConnectionString);
       myConnectionString.Open();
       
       OdbcCommand objCmd = new OdbcCommand(strQuery, myConnectionString);
       objCmd.Prepare();
       objCmd.CommandType = CommandType.Text;
       objCmd.CommandText = strQuery;

       OdbcDataReader reader = objCmd.ExecuteReader(); // create first reader

       while (reader.Read())
           Response.Write(reader["VarPerc"].ToString());

       String top = reader["VarPerc"].ToString(); // LINE OF ERROR: No data exists for the row/column.
       reader.Close();
       reader.Dispose(); // make sure the first reader has been released

       LegendItem newItem = new LegendItem();
       newItem.ImageStyle = LegendImageStyle.Marker;
       newItem.MarkerStyle = MarkerStyle.Diamond;
       newItem.Cells.Add(LegendCellType.SeriesSymbol, "", ContentAlignment.MiddleLeft);
       newItem.Cells.Add(LegendCellType.Text, "?% DTR", ContentAlignment.MiddleLeft);
       newItem.Cells[1].CellSpan = 1;
       newItem.Cells.Add(LegendCellType.Text, "", ContentAlignment.MiddleLeft);
       newItem.Cells.Add(LegendCellType.Text, top, ContentAlignment.MiddleLeft);
       Chart1.Legends[0].CustomItems.Add(newItem);
                                         
       Chart1.DataSource = objCmd.ExecuteReader(); // create another reader

       objCmd.Dispose();

       myConnectionString.Close();
       myConnectionString.Dispose();
0
Vulpes

Vulpes

NA 98.3k 1.5m 13y
You've changed the order of the code so that you're now calling ExecuteReader() again without closing the first reader:

       OdbcDataReader reader = objCmd.ExecuteReader();

       Chart1.DataSource = objCmd.ExecuteReader(); // calling ExecuteReader again
       
Try this instead:

       String myQuery = " SELECT tNome, " +
                         " VarPerc " +                         
                         " FROM tbl_m " +
                         " WHERE " +
                         " tNome LIKE '%PPP%'; ";

       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(); // create first reader
      
       while (reader.Read())
           Response.Write(reader["VarPerc"].ToString());
  
       String top = reader["VarPerc"].ToString();
       reader.Close();
       reader.Dispose(); // make sure the first reader has been released

       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);  

       Chart1.DataSource = objCmd.ExecuteReader(); // create another reader

       objCmd.Dispose();
                           
       myConnectionString.Close(); 
       myConnectionString.Dispose();
 
0
Chevy Mark Sunderland

Chevy Mark Sunderland

NA 160 110.6k 13y
I'm sorry Sir but not working 

There is an already open DataReader associated with this command 

String myQuery = " SELECT tNome, " +
                         " VarPerc " +                         
                         " FROM tbl_m " +
                         " WHERE " +
                         " tNome LIKE '%PPP%'; ";

       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();

       Chart1.DataSource = objCmd.ExecuteReader(); 
       
       while (reader.Read())
           Response.Write(reader["VarPerc"].ToString());
  
       String top = reader["VarPerc"].ToString();
       reader.Close();

       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);  

       reader.Dispose();

       objCmd.Dispose();
       objCmd.Cancel();              
                           
       myConnectionString.Dispose(); 
       myConnectionString.Close();
0
Vulpes

Vulpes

NA 98.3k 1.5m 13y
As the error message says, you need to close the first OdbcDataReader object before you open a second.

So, I'd insert the line I've highlighted below:

String top = reader["VarPerc"].ToString();
reader.Close();
LegendItem newItem = new LegendItem();


0
Chevy Mark Sunderland

Chevy Mark Sunderland

NA 160 110.6k 13y
hello, thanks for your time.

I try debug:

1) The myQuery in mysql working correctly;
2) I have add new field in the myQuery (tName);
3) I have canceled the syntax CONCAT(VarPerc,'%') As VarPerc in the myQuery;
3) I rem the last line of bind to Chart1;
4) I try with Response.Write(reader[1].ToString());
5) I have add the last line of bind to Chart1;

The output of alias `VarPerc` is correct = -59

Now I need use the value of alias `VarPerc` in codebehind and try this but I have error.

Can you help me?

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(); 

String myQuery = " SELECT tNome, " +
                         " VarPerc As VarPerc " +                        
                         " FROM tbl_m " +
                         " WHERE " +
                         " tNome LIKE '%PPP%'; ";

       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);  

       Chart1.DataSource = objCmd.ExecuteReader(); 

       objCmd.Dispose();
       objCmd.Cancel();
             
       reader.Dispose();
       reader.Close();
                           
       myConnectionString.Dispose(); 
       myConnectionString.Close();
0
Vulpes

Vulpes

NA 98.3k 1.5m 13y
You could try: reader[0].ToString() to make sure but I suspect that the error is caused by the query not returning any rows.

Are you sure that your LIKE clause is working OK?
0
Chevy Mark Sunderland

Chevy Mark Sunderland

NA 160 110.6k 13y
thanks a lot.

I have changed method, but I have this other error, why?

Exception Details: System.IndexOutOfRangeException: VarPerc 
String myQuery = " SELECT " +
" CONCAT( VarPerc,'%') As VarPerc " +
" FROM tbl_m " +
" WHERE " +
" tNome LIKE '%PPP%'; ";

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();
0
Vulpes

Vulpes

NA 98.3k 1.5m 13y
It appears that the problem is occurring on this line:

  objCmd.Parameters.Add("?VarPerc", OdbcType.VarChar, 15).Value = VarPerc;    

I can't tell from the code you've uploaded where VarPerc is defined but, for you to get that particular error message, it's not currently within scope. 

If you've defined it in a different method then you could store it in Session state and then retrieve it in the current method.
Next Recommended Forum