AllExperts > Experts 
Search      

VB.NET

Volunteer
Answers to thousands of questions
 Home · More Questions · Answer Library  · Encyclopedia ·
More VB.NET Answers
Question Library

Ask a question about VB.NET
Volunteer
Experts of the Month
Expert Login

Awards

About Us
Tell friends
Link to Us
Disclaimer

 
 
 
 
About Brandon Drake
Expertise
I can answer questions regarding the Graphics object, file access, general optimization, printing (on paper), user controls, program mathematics, the registry, creating macros, processes, and fundamental or basic questions. I have made a few asp.net pages but I do not know the language extensively. I have not done much work with databases but I have created my own database systems.

Experience
I have been programming with visual basic for 6 years. In all I have written programs in about 8 programming languages, although, I only know about 3 well. I have written a few applications but mainly I like to write programs for fun.

 
   

You are here:  Experts > Computing/Technology > Basic > VB.NET > Error in VB.net

Topic: VB.NET



Expert: Brandon Drake
Date: 7/10/2006
Subject: Error in VB.net

Question
Hi,
I'm working with code at the minute which I keep getting the following error, but dont know how to fix ...

Variable 'x' hides a variable in an enclosing block.

... the error is on line 416

If you could help I'd be very greatful, thank you

Public Shared Sub exportToExcel(ByVal source As DataSet, ByVal fileName As String)
Dim excelDoc As System.IO.StreamWriter
excelDoc = New System.IO.StreamWriter(fileName)
Const startExcelXML As String = "<xml version>" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "<Workbook " + "xmlns=""urn:schemas-microsoft-com:office:spreadsheet""" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & "" + " xmlns:o=""urn:schemas-microsoft-com:office:office""" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " " + "xmlns:x=""urn:schemas- microsoft-com:office:" + "excel""" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " xmlns:ss=""urn:schemas-microsoft-com:" + "office:spreadsheet"">" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " <Styles>" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " " + "<Style ss:ID=""Default"" ss:Name=""Normal"">" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " " + "<Alignment ss:Vertical=""Bottom""/>" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " <Borders/>" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " <Font/>" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " <Interior/>" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " <NumberFormat/>" + "" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " <Protection/>" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " </Style>" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " " + "<Style ss:ID=""BoldColumn"">" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " <Font " + "x:Family=""Swiss"" ss:Bold=""1""/>" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " </Style>" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " " + "<Style ss:ID=""StringLiteral"">" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " <NumberFormat" + " ss:Format=""@""/>" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " </Style>" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " <Style " + "ss:ID=""Decimal"">" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " <NumberFormat " + "ss:Format=""0.0000""/>" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " </Style>" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " " + "<Style ss:ID=""Integer"">" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " <NumberFormat " + "ss:Format=""0""/>" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " </Style>" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " <Style " + "ss:ID=""DateLiteral"">" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " <NumberFormat " + "ss:Format=""mm/dd/yyyy;@""/>" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " </Style>" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " " + "</Styles>" & Microsoft.VisualBasic.Chr(13) & "" & Microsoft.VisualBasic.Chr(10) & " "
Const endExcelXML As String = "</Workbook>"
Dim rowCount As Integer = 0
Dim sheetCount As Integer = 1
excelDoc.Write(startExcelXML)
excelDoc.Write("<Worksheet ss:Name=""Sheet" + sheetCount + """>")
excelDoc.Write("<Table>")
excelDoc.Write("<Row>")
Dim x As Integer = 0
While x < source.Tables(0).Columns.Count
  excelDoc.Write("<Cell ss:StyleID=""BoldColumn""><Data ss:Type=""String"">")
  excelDoc.Write(source.Tables(0).Columns(x).ColumnName)
  excelDoc.Write("</Data></Cell>")
  System.Math.Min(System.Threading.Interlocked.Increment(x),x-1)
End While
excelDoc.Write("</Row>")
For Each x As DataRow In source.Tables(0).Rows
  System.Math.Min(System.Threading.Interlocked.Increment(rowCount),rowCount-1)
  If rowCount = 64000 Then
    rowCount = 0
    System.Math.Min(System.Threading.Interlocked.Increment(sheetCount),sheetCount-1)
    excelDoc.Write("</Table>")
    excelDoc.Write(" </Worksheet>")
    excelDoc.Write("<Worksheet ss:Name=""Sheet" + sheetCount + """>")
    excelDoc.Write("<Table>")
  End If
  excelDoc.Wri ... etc

Answer
Amanda,

I think what has happened is that you declared the variable x twice.  Something like:

Dim x As Integer
While
  Dim x As Integer
  ...
End While

Go to Edit->Find and search for "dim x" (no quotes) and see if it can find two of the statements.



Good Luck,

Brandon Drake

Add to this Answer    Ask a Question



  Rate this Answer
   Was this answer helpful?
Not at allDefinitely              
   12345  

     
About Us | Advertise on This Site | User Agreement | Privacy Policy | Help
Copyright  © 2008 About, Inc. About and About.com are registered trademarks of About, Inc. The About logo is a trademark of About, Inc. All rights reserved.