Results 1 to 16 of 16

Thread: [RESOLVED] solution Character is not valid in vb.net

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Resolved [RESOLVED] solution Character is not valid in vb.net

    Dear All master,

    please help to provide a solution.

    Code:
      Dim stream As FileStream = File.Open("C:\Sample-Sales-Data.xlsx", FileMode.Open, FileAccess.Read)
            Dim excelReader As IExcelDataReader = ExcelReaderFactory.CreateOpenXmlReader(stream)
            Dim result As DataSet = excelReader.AsDataSet(New ExcelDataSetConfiguration() With {
             .ConfigureDataTable = Function(underscore) New ExcelDataTableConfiguration() With {.UseHeaderRow = True}
            })
          For Each table As DataTable In result.Tables
                For Each row As DataRow In table.Rows
                    Dim salesRepName = CType(row("Sales_Rep_Name"), String)
                    If [String].Equals(salesRepName, "Janet") Then
                        Dim year = CType(CType(row("Year"), Double), Integer)
    			Console.WriteLine($"Janet's year is {year}") 'problem this line
                    End If
                Next
            Next
    
            excelReader.Close()
            Console.ReadLine()
    Thanks
    roy88

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: solution Character is not valid in vb.net

    A solution to what? Maybe make the effort to actually explain the issue. If you're using an older version of VB that doesn't support string interpolation then either update to a newer version, e.g. 2019, or just don't use string interpolation. There are other ways to construct a String from parts and we shouldn't have to explain then to you. If the problem is something else then I have no idea what it might be, which is why you need to explain it.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Re: solution Character is not valid in vb.net

    Quote Originally Posted by jmcilhinney View Post
    A solution to what? Maybe make the effort to actually explain the issue. If you're using an older version of VB that doesn't support string interpolation then either update to a newer version, e.g. 2019, or just don't use string interpolation. There are other ways to construct a String from parts and we shouldn't have to explain then to you. If the problem is something else then I have no idea what it might be, which is why you need to explain it.
    dear mr. jmcilhinney ,
    I'm using visual studio 2010 and the code solution so I can run in visual studio 2010.
    Thanks
    roy88

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Re: solution Character is not valid in vb.net

    Quote Originally Posted by roy88 View Post
    dear mr. jmcilhinney ,
    I'm using visual studio 2010 and the code solution so I can run in visual studio 2010.
    Thanks
    roy88
    How do I get a particular value of the column named Year whose say, Sales_Rep_Name value is Janet?
    Attached Images Attached Images  

  5. #5
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: solution Character is not valid in vb.net

    Quote Originally Posted by roy88 View Post
    How do I get a particular value of the column named Year whose say, Sales_Rep_Name value is Janet?
    That is unrelated to the question you asked in the first place.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: solution Character is not valid in vb.net

    Quote Originally Posted by roy88 View Post
    dear mr. jmcilhinney ,
    I'm using visual studio 2010 and the code solution so I can run in visual studio 2010.
    Thanks
    roy88
    The line you highlighted is using string interpolation, which is basically native language support for composite formatting. That was introduced in (I think) VB 2015, or it may have been 2017. Either way, it was well after 2010. There are plenty of other options from building Strings from parts though. Like I said, we shouldn't have to teach you the basics but it's probably not worth the pain to fight it, so here goes. You can use String.Format in any version of VB to perform composite formatting. String interpolation compiles to that anyway. Console.WriteLine also has overloads that perform composite formatting and you're already calling that method. There are other options too but, apart from anything else, you can just use the concatenation operator, as I'm sure you've done many times.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  7. #7
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    846

    Re: solution Character is not valid in vb.net

    As JMC said, for VB2010, replace your $"...." with string.format(" .... {0}.... {1}....", firstvariable, secondvariable, etc...) and the link to documentation https://docs.microsoft.com/fr-fr/dot...t?view=net-5.0
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  8. #8
    Angel of Code Niya's Avatar
    Join Date
    Nov 2011
    Posts
    9,017

    Re: solution Character is not valid in vb.net

    Quote Originally Posted by jmcilhinney View Post
    Like I said, we shouldn't have to teach you the basics but it's probably not worth the pain to fight it, so here goes.
    This is one of those rare occasions where I don't really fault the OP. Even I do sometimes forgot what features were introduced when. This is especially true for C# that seems to get a thousand new features every week! The different possible combinations of Framework and IDE versions don't exactly help either. For example, Tasks are a good example of something that has implementations in both the language(Async/Await) and the Framework(The Task class). I can see how something like this can confuse people. It's kind of hard sometimes to keep in your head what you can and cannot do based on what IDE/Framework/language version you're currently using. I make mistakes like this sometimes when I switch between .Net 5 and the classic Framework.
    Treeview with NodeAdded/NodesRemoved events | BlinkLabel control | Calculate Permutations | Object Enums | ComboBox with centered items | .Net Internals article(not mine) | Wizard Control | Understanding Multi-Threading | Simple file compression | Demon Arena

    Copy/move files using Windows Shell | I'm not wanted

    C++ programmers will dismiss you as a cretinous simpleton for your inability to keep track of pointers chained 6 levels deep and Java programmers will pillory you for buying into the evils of Microsoft. Meanwhile C# programmers will get paid just a little bit more than you for writing exactly the same code and VB6 programmers will continue to whitter on about "footprints". - FunkyDexter

    There's just no reason to use garbage like InputBox. - jmcilhinney

    The threads I start are Niya and Olaf free zones. No arguing about the benefits of VB6 over .NET here please. Happiness must reign. - yereverluvinuncleber

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: solution Character is not valid in vb.net

    Quote Originally Posted by Niya View Post
    This is one of those rare occasions where I don't really fault the OP.
    I do. This is almost certainly a case of copying code off the internet and pasting it without any understanding of what it actually does. If they had made the effort to think about what that line was actually doing, they could have worked out how to do it without string interpolation. It shouldn't take much effort at all to work out what a call to Console.WriteLine is doing.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Re: solution Character is not valid in vb.net

    please help there is an error running the code. The error " Input string was not in a correct format."
    Code:
    Dim stream As FileStream = File.Open("C:\Sample-Sales-Data.xlsx", FileMode.Open, FileAccess.Read)
            Dim excelReader As IExcelDataReader = ExcelReaderFactory.CreateOpenXmlReader(stream)
            Dim result As DataSet = excelReader.AsDataSet(New ExcelDataSetConfiguration() With {
             .ConfigureDataTable = Function(underscore) New ExcelDataTableConfiguration() With {.UseHeaderRow = True}
            })
    For Each table As DataTable In result.Tables
                For Each row As DataRow In table.Rows
                    Dim salesRepName = CType(row("Sales_Rep_Name"), String)
                    If [String].Equals(salesRepName, "Janet") Then
                        Dim year = CType(CType(row("Year"), Double), Integer)
                        Console.WriteLine(String.Format("Janet's year is {0} {year}", {year}))
                    End If
                Next
            Next
    
            excelReader.Close()
            Console.ReadLine()

  11. #11
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: solution Character is not valid in vb.net

    Perhaps you should read the documentation for the String.Format method to learn how it works. It's certainly not like that. Information already exists out there. You can go and find it, rather than waiting for it to come to you. The documentation is always in the same place so it's not like you have to search for it. Just click the method name and press F1 and you're there.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  12. #12
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    846

    Re: solution Character is not valid in vb.net

    Quote Originally Posted by roy88 View Post
    please help there is an error running the code. The error " Input string was not in a correct format."
    Code:
    Dim stream As FileStream = File.Open("C:\Sample-Sales-Data.xlsx", FileMode.Open, FileAccess.Read)
            Dim excelReader As IExcelDataReader = ExcelReaderFactory.CreateOpenXmlReader(stream)
            Dim result As DataSet = excelReader.AsDataSet(New ExcelDataSetConfiguration() With {
             .ConfigureDataTable = Function(underscore) New ExcelDataTableConfiguration() With {.UseHeaderRow = True}
            })
    For Each table As DataTable In result.Tables
                For Each row As DataRow In table.Rows
                    Dim salesRepName = CType(row("Sales_Rep_Name"), String)
                    If [String].Equals(salesRepName, "Janet") Then
                        Dim year = CType(CType(row("Year"), Double), Integer)
                        Console.WriteLine(String.Format("Janet's year is {0} {year}", {year}))
                    End If
                Next
            Next
    
            excelReader.Close()
            Console.ReadLine()

    of course it isn't , your variable is year not {year} and with string.format you don't put the variable inside the string, just the position reference of the variable as explained in the documentation (see the link I gave)

    Code:
     Console.WriteLine(String.Format("Janet's year is {0} {year}", {year}))
    should be
    Code:
     Console.WriteLine(String.Format("Janet's year is {0}", year))
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  13. #13
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: solution Character is not valid in vb.net

    Quote Originally Posted by Delaney View Post
    of course it isn't , your variable is year not {year} and with string.format you don't put the variable inside the string, just the position reference of the variable as explained in the documentation (see the link I gave)

    Code:
     Console.WriteLine(String.Format("Janet's year is {0} {year}", {year}))
    should be
    Code:
     Console.WriteLine(String.Format("Janet's year is {0}", year))
    You don't even need to call String.Format. As I already said, Console.WriteLine already has composite formatting built in.
    vb.net Code:
    1. Console.WriteLine("Janet's year is {0}", year)
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  14. #14
    Fanatic Member Delaney's Avatar
    Join Date
    Nov 2019
    Location
    Paris, France
    Posts
    846

    Re: solution Character is not valid in vb.net

    yes I know but I imagine the OP, at some point, may use it with something else than Console.WriteLine so we might as well give the full syntax.
    The best friend of any programmer is a search engine
    "Don't wish it was easier, wish you were better. Don't wish for less problems, wish for more skills. Don't wish for less challenges, wish for more wisdom" (J. Rohn)
    “They did not know it was impossible so they did it” (Mark Twain)

  15. #15

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Re: solution Character is not valid in vb.net

    Quote Originally Posted by Delaney View Post
    of course it isn't , your variable is year not {year} and with string.format you don't put the variable inside the string, just the position reference of the variable as explained in the documentation (see the link I gave)

    Code:
     Console.WriteLine(String.Format("Janet's year is {0} {year}", {year}))
    should be
    Code:
     Console.WriteLine(String.Format("Janet's year is {0}", year))
    Dear Delaney,

    sorry for late reply, your code solution works perfectly. Thank you.

    Thanks
    roy88

  16. #16

    Thread Starter
    Lively Member
    Join Date
    Aug 2021
    Posts
    91

    Re: solution Character is not valid in vb.net

    Quote Originally Posted by jmcilhinney View Post
    You don't even need to call String.Format. As I already said, Console.WriteLine already has composite formatting built in.
    vb.net Code:
    1. Console.WriteLine("Janet's year is {0}", year)
    dear jmcilhinney
    sorry for late reply, your code solution works perfectly. Thank you
    thanks
    roy88

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width