Format Exception Error when writing text to a file in VB - vb.net

When I try to write text to a file, I get an error saying "FormatException was unhandled"
Here's the code:
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Dim ChampPath As String = FolderBrowserDialog1.SelectedPath & "\League of Legends\Config\Champions"
Dim NamePath As String = ChampPath & "\" & SelectChampion.Text
Dim FilePath As String = NamePath & "\" & SelectChampion.Text & ".json"
Dim Map As String
Dim Mode As String
System.IO.Directory.CreateDirectory(NamePath)
System.IO.File.Create(FilePath).Dispose()
If (SelectMap.Text = "Any") Then
Map = "any"
ElseIf (SelectMap.Text = "Summoners Rift") Then
Map = "1"
ElseIf (SelectMap.Text = "Twisted Treeline") Then
Map = "10"
ElseIf (SelectMap.Text = "Crystal Scar") Then
Map = "8"
ElseIf (SelectMap.Text = "Proving Grounds") Then
Map = "3"
End If
If (SelectMode.Text = "Any") Then
Mode = "any"
ElseIf (SelectMode.Text = "Classic") Then
Mode = "CLASSIC"
ElseIf (SelectMode.Text = "Dominon") Then
Mode = "ODIN"
ElseIf (SelectMode.Text = "Proving Grounds") Then
Mode = "ARAM"
End If
If (System.IO.File.Exists(FilePath)) Then
Using Writer As StreamWriter = New StreamWriter(FilePath)
Writer.Write("{" & vbNewLine &
" ""champion"":""" & SelectChampion.Text & """," & vbNewLine &
" ""title"":""" & TitleBox.Text & "", " " & vbNewLine &
" ""type"":""" & TypeBox.Text & "", " " & vbNewLine &
" ""map"":""" & Map & "", " " & vbNewLine &
" ""mode"":""" & Mode & "", " " & vbNewLine &
" ""priority""" & SelectPriority.Text & "", " " & vbNewLine &
" ""blocks"":[ " & vbNewLine &
"{" & vbNewLine &
" ""type"":""starting"", " & vbNewLine &
" ""items"":[ " & vbNewLine &
"{" & vbNewLine &
" ""id"":""1001"", " & vbNewLine &
" ""count"":1 " & vbNewLine &
"}," & vbNewLine &
"{" & vbNewLine &
" ""id"":""3010"", " & vbNewLine &
" ""count"":3 " & vbNewLine &
"}" & vbNewLine &
"]" & vbNewLine &
"}," & vbNewLine &
"{" & vbNewLine &
" ""type"":""essential"", " & vbNewLine &
" ""items"":[ " & vbNewLine &
"{" & vbNewLine &
" ""id"":""3001"", " & vbNewLine &
" ""count"":1 " & vbNewLine &
"}," & vbNewLine &
"{" & vbNewLine &
" ""id"":""3089"", " & vbNewLine &
" ""count"":1 " & vbNewLine &
"}" & vbNewLine &
"]" & vbNewLine &
"}," & vbNewLine &
"{" & vbNewLine &
" ""type"":""offensive"", " & vbNewLine &
" ""items"":[ " & vbNewLine &
"{" & vbNewLine &
" ""id"":""3100"", " & vbNewLine &
" ""count"":1 " & vbNewLine &
"}," & vbNewLine &
"{" & vbNewLine &
" ""id"":""3128"", " & vbNewLine &
" ""count"":1 " & vbNewLine &
"}," & vbNewLine &
"{" & vbNewLine &
" ""id"":""3135"", " & vbNewLine &
" ""count"":1 " & vbNewLine &
"}" & vbNewLine &
"]" & vbNewLine &
"}," & vbNewLine &
"{" & vbNewLine &
" ""type"":""defensive"", " & vbNewLine &
" ""items"":[ " & vbNewLine &
"{" & vbNewLine &
" ""id"":""3140"", " & vbNewLine &
" ""count"":1 " & vbNewLine &
"}," & vbNewLine &
"{" & vbNewLine &
" ""id"":""3157"", " & vbNewLine &
" ""count"":1 " & vbNewLine &
"}" & vbNewLine &
"]" & vbNewLine &
"}" & vbNewLine &
"]" & vbNewLine &
"}")
End Using
End If
End Sub
I can't find where the error is actually coming from...
The text also needs to be formatted like it is, with the quotes and what not. Thanks for any help.

Assumed VB Net .. Better you change like this
Writer.Write("{" & vbNewLine & _
" champion : " & SelectChampion.Text & "," & vbNewLine & _
" title : " & TitleBox.Text & "," & vbNewLine & _
" type : " ........
...... etc

Related

Reading Code From a Text File Efficiently And How to Distinguish Blocks of Code In The Text File

I'm creating a utility for a game that creates spreadsheets of data by reading the game's code.
One of the most basic aspects of this is being able to read the game's code efficiently. The game is written in C#. I'm using VB.Net for my program because my mind can't grasp the abstraction of more complex languages. Historically the way I had my program recognise code blocks in the code text file is by counting the brackets that encapsulated them and working off of that. This seems a bit clunky, though. Is there any simpler way to help the program figure out code blocks?
For Example, code in the text file might look like this:
6673={
key="d_example_duchy_1"
de_facto_liege=12
variables={
data={ {
flag="traditional_de_jure_kingdom"
tick=0
data={
type=lt
identity=6632
}
}
}
}
de_jure_liege=6632
de_jure_vassals={ 6674 6678 6682 6686 }
holder=558
name="Example Duchy 1"
date=1253.1.1
heir={ 880 }
history={ 1253.1.1=558 }
capital=6674
history_government="tribal_government"
coat_of_arms_id=2320
}
6674={
key="c_example_county_1"
variables={
data={ {
flag="influx_culture"
tick=0
data={
type=culture
identity=11
}
}
{
flag="influx_faith"
tick=0
data={
type=faith
identity=57
}
}
{
flag="fog_resistance"
tick=0
data={
type=value
}
}
}
}
de_jure_liege=6673
de_jure_vassals={ 6675 6676 6677 }
holder=558
name="Example County 1"
date=1243.6.26
heir={ 880 }
capital=6674
coat_of_arms_id=2323
}
6675={
key="b_example_barony_1"
de_facto_liege=6674
de_jure_liege=6674
holder=558
name="Example Barony 1"
date=1254.1.1
capital_barony=yes
duchy_capital_barony=yes
capital=6674
coat_of_arms_id=2328
}
6676={
key="b_example_barony_2"
de_facto_liege=6674
de_jure_liege=6674
holder=11609
name="Example Barony 2"
date=1253.7.28
capital=6674
coat_of_arms_id=2332
}
6677={
key="b_example_barony_3"
de_facto_liege=6674
de_jure_liege=6674
holder=558
name="Example Barony 3"
date=1232.2.8
heir={ 880 }
capital=6674
theocratic_lease=yes
coat_of_arms_id=2334
}
Thanks in advance. :)
The solution I eventually settled on, for discerning the overall code-blocks (e.g. all the data nested under 6674={ ... }) was basically:
Dim Code As String = File.ReadAllText("PATH/TO/CODE/FILE.txt")
Dim Output As List(Of String) = Code.Split(vbCrLf & "}" & vbCrLf).ToList
So in plain English, reading all the code into a single string, then splitting the string according to how the original code's language distinguished the initial nest of code (i.e. a carriage return followed by a curly bracket followed by another carriage return).
The list at the end would look something like this:
Output(0)
6673={" & vbCrLf & " key=""d_example_duchy_1""" & vbCrLf & " de_facto_liege=12" & vbCrLf & " variables={" & vbCrLf & " data={ {" & vbCrLf & " flag=""traditional_de_jure_kingdom""" & vbCrLf & " tick=0" & vbCrLf & " data={" & vbCrLf & " type=lt" & vbCrLf & " identity=6632" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " de_jure_liege=6632" & vbCrLf & " de_jure_vassals={ 6674 6678 6682 6686 }" & vbCrLf & " holder=558" & vbCrLf & " name=""Example Duchy 1""" & vbCrLf & " date=1253.1.1" & vbCrLf & " heir={ 880 }" & vbCrLf & " history={ 1253.1.1=558 }" & vbCrLf & " capital=6674" & vbCrLf & " history_government=""tribal_government""" & vbCrLf & " coat_of_arms_id=2320
Output(1)
6674={" & vbCrLf & " key=""c_example_county_1""" & vbCrLf & " variables={" & vbCrLf & " data={ {" & vbCrLf & " flag=""influx_culture""" & vbCrLf & " tick=0" & vbCrLf & " data={" & vbCrLf & " type=culture" & vbCrLf & " identity=11" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " {" & vbCrLf & " flag=""influx_faith""" & vbCrLf & " tick=0" & vbCrLf & " data={" & vbCrLf & " type=faith" & vbCrLf & " identity=57" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " {" & vbCrLf & " flag=""fog_resistance""" & vbCrLf & " tick=0" & vbCrLf & " data={" & vbCrLf & " type=value" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " }" & vbCrLf & " de_jure_liege=6673" & vbCrLf & " de_jure_vassals={ 6675 6676 6677 }" & vbCrLf & " holder=558" & vbCrLf & " name=""Example County 1""" & vbCrLf & " date=1243.6.26" & vbCrLf & " heir={ 880 }" & vbCrLf & " capital=6674" & vbCrLf & " coat_of_arms_id=2323
And so on with the remaining three.

How to get the value without encoding in vb.net watch?

In vb.net in debugging you can watch a variable and right click.
There are options between copy and copy value.
There is little differences. I basically got this
"[" & vbCrLf & " {" & vbCrLf & " ""id"": 22812485751," & vbCrLf & " ""currencyPair"": ""ORME/BTC""," & vbCrLf & " ""goodUntilTime"": 0," & vbCrLf & " ""type"": ""LIMIT_SELL""," & vbCrLf & " ""orderStatus"": ""EXECUTED""," & vbCrLf & " ""issueTime"": 1539721209920," & vbCrLf & " ""price"": 2.726E-05," & vbCrLf & " ""quantity"": 1733.83246147," & vbCrLf & " ""remainingQuantity"": 0.0," & vbCrLf & " ""commissionByTrade"": 8.508E-05," & vbCrLf & " ""bonusByTrade"": 0," & vbCrLf & " ""bonusRate"": 0," & vbCrLf & " ""commissionRate"": 0.0018," & vbCrLf & " ""lastModificationTime"": 1539727280096" & vbCrLf & " }," & vbCrLf & " {" & vbCrLf & " ""id"": 19661583751," & vbCrLf & " ""currencyPair"": ""ORME/ETH""," & vbCrLf & " ""goodUntilTime"": 0," & vbCrLf & " ""type"": ""LIMIT_BUY""," & vbCrLf & " ""orderStatus"": ""EXECUTED""," & vbCrLf & " ""issueTime"": 1537113087231," & vbCrLf & " ""price"": 0.00110001," & vbCrLf & " ""quantity"": 1733.83246562," & vbCrLf & " ""remainingQuantity"": 0.0," & vbCrLf & " ""commissionByTrade"": 0.003433," & vbCrLf & " ""bonusByTrade"": 0," & vbCrLf & " ""bonusRate"": 0," & vbCrLf & " ""commissionRate"": 0.0018," & vbCrLf & " ""lastModificationTime"": 1538092923124" & vbCrLf & " }," & vbCrLf & " {" & vbCrLf & " ""id"": 19669018551," & vbCrLf & " ""currencyPair"": ""ORME/BTC""," & vbCrLf & " ""goodUntilTime"": 0," & vbCrLf & " ""type"": ""LIMIT_SELL""," & vbCrLf & " ""orderStatus"": ""EXECUTED""," & vbCrLf & " ""issueTime"": 1537118640003," & vbCrLf & " ""price"": 4.302E-05," & vbCrLf & " ""quantity"": 1140.08057366," & vbCrLf & " ""remainingQuantity"": 0.0," & vbCrLf & " ""commissionByTrade"": 8.829E-05," & vbCrLf & " ""bonusByTrade"": 0," & vbCrLf & " ""bonusRate"": 0," & vbCrLf & " ""commissionRate"": 0.0018," & vbCrLf & " ""lastModificationTime"": 1537119048771" & vbCrLf & " }," & vbCrLf & " {" & vbCrLf & " ""id"": 19666104251," & vbCrLf & " ""currencyPair"": ""ORME/BTC""," & vbCrLf & " ""goodUntilTime"": 0," & vbCrLf & " ""type"": ""LIMIT_SELL""," & vbCrLf & " ""orderStatus"": ""PARTIALLY_FILLED_AND_CANCELLED""," & vbCrLf & " ""issueTime"": 1537116476248," & vbCrLf & " ""price"": 4.25E-05," & vbCrLf & " ""quantity"": 7487.1," & vbCrLf & " ""remainingQuantity"": 1140.08057366," & vbCrLf & " ""commissionByTrade"": 0.00048563," & vbCrLf & " ""bonusByTrade"": 0," & vbCrLf & " ""bonusRate"": 0," & vbCrLf & " ""commissionRate"": 0.0018," & vbCrLf & " ""lastModificationTime"": 1537116507585" & vbCrLf & " }," & vbCrLf & " {" & vbCrLf & " ""id"": 19661490851," & vbCrLf & " ""currencyPair"": ""ETH/BTC""," & vbCrLf & " ""goodUntilTime"": 0," & vbCrLf & " ""type"": ""LIMIT_BUY""," & vbCrLf & " ""orderStatus"": ""EXECUTED""," & vbCrLf & " ""issueTime"": 1537113025233," & vbCrLf & " ""price"": 0.03357901," & vbCrLf & " ""quantity"": 1.91066607," & vbCrLf & " ""remainingQuantity"": 0.0," & vbCrLf & " ""commissionByTrade"": 0.00011548," & vbCrLf & " ""bonusByTrade"": 0," & vbCrLf & " ""bonusRate"": 0," & vbCrLf & " ""commissionRate"": 0.0018," & vbCrLf & " ""lastModificationTime"": 1537113028429" & vbCrLf & " }," & vbCrLf & " {" & vbCrLf & " ""id"": 17637942051," & vbCrLf & " ""currencyPair"": ""ORME/BTC""," & vbCrLf & " ""goodUntilTime"": 0," & vbCrLf & " ""type"": ""LIMIT_SELL""," & vbCrLf & " ""orderStatus"": ""EXECUTED""," & vbCrLf & " ""issueTime"": 1535545801765," & vbCrLf & " ""price"": 4.5E-05," & vbCrLf & " ""quantity"": 2289.92000001," & vbCrLf & " ""remainingQuantity"": 0.0," & vbCrLf & " ""commissionByTrade"": 0.00018916," & vbCrLf & " ""bonusByTrade"": 0," & vbCrLf & " ""bonusRate"": 0," & vbCrLf & " ""commissionRate"": 0.0018," & vbCrLf & " ""lastModificationTime"": 1535545801765" & vbCrLf & " }," & vbCrLf & " {" & vbCrLf & " ""id"": 17637532551," & vbCrLf & " ""currencyPair"": ""ORME/BTC""," & vbCrLf & " ""goodUntilTime"": 0," & vbCrLf & " ""type"": ""LIMIT_SELL""," & vbCrLf & " ""orderStatus"": ""PARTIALLY_FILLED_AND_CANCELLED""," & vbCrLf & " ""issueTime"": 1535545474694," & vbCrLf & " ""price"": 4.78E-05," & vbCrLf & " ""quantity"": 2487.1," & vbCrLf & " ""remainingQuantity"": 2289.92000001," & vbCrLf & " ""commissionByTrade"": 1.697E-05," & vbCrLf & " ""bonusByTrade"": 0," & vbCrLf & " ""bonusRate"": 0," & vbCrLf & " ""commissionRate"": 0.0018," & vbCrLf & " ""lastModificationTime"": 1535545515636" & vbCrLf & " }" & vbCrLf & "]"
And that sucks and hard to read. If a string contains a json, I want to see the json as it actually is. I want to be able to copy that to notepad.
How do I do so?
Here is what you can do when debugging.
put mouse over your variable. Popup menu will show up and there you will see magnifying lens
click that
Right-click on your variable, select "Quick Watch" in the menu a screen will show
click magnifying lens

Using variables in fieldnames - SQL Update statement

I am new to using Access 2010. I wish to execute the following sql update statement, but I have problems with the syntax. The table is called "Forecasts", and users will edit & update the qty forecasted.
Problem - The table fieldnames are 2014_1, 2014_2, 2014_3 ... to represent the different months, stored in an array. I have done abit of research and I believe the way to dynamically do this is:
Dim sqlString As String
sqlString = "UPDATE Forecasts " & _
" SET Branch_Plant=" & Me.txtBranch_Plant & _
", Item_Number_Short='" & Me.txtItem_Number_Short & "'" & _
", Description='" & Me.txtDescription & "'" & _
", UOM='" & Me.txtUOM & "'" & _
", Estimated_Cost=" & Me.txtEstimated_Cost & _
", Requesting_Business_Unit='" & Me.txtRequesting_Business_Unit & "'" & _
", End_Customer='" & Me.txtEnd_Customer & "'" & _
", Project='" & Me.txtProject & "'" & _
", Forecasts." & "[" & arrMonthToDisplay(0) & "]" = " & Me.txtProjectedJanVolume " & _
" WHERE ID =" & Me.txtID.Tag
MsgBox ("This is the output: " & sqlString)
CurrentDb.Execute sqlString
It was working fine until this line was added
Forecasts." & "[" & arrMonthToDisplay(0) & "]" = " & Me.txtProjectedJanVolume
The msgbx output now shows: "False". Whats wrong with sqlString?
Please help! Thank you very much.
", Forecasts.[" & arrMonthToDisplay(0) & "] = " & Me.txtProjectedJanVolume & _
" WHERE ID =" & Me.txtID.Tag
You compare string to string.
Change
", Forecasts." & "[" & arrMonthToDisplay(0) & "]" = " & Me.txtProjectedJanVolume " &
to
", Forecasts." & "[" & arrMonthToDisplay(0) & "] = " & " Me.txtProjectedJanVolume " &

Index out of range exception unhandled vb.net

I just faced a problem with my code.It raises an exception.This is the code.
conn = oSubPayItemDescription.GetDbConnection()
md = New OleDbCommand("SELECT [sub_pay_item_quantity].[quantity],[sub_pay_item_unit_rate].[rate] FROM " &
"[sub_pay_item_quantity],[sub_pay_item_unit_rate] WHERE [sub_pay_item_quantity].[sub item]=[sub_pay_item_unit_rate].[sub item] AND " &
"[sub_pay_item_quantity].[sub item]='" & subItem & "' AND [sub_pay_item_quantity].[bridge type]='" & bridgeType & "' " &
"AND [sub_pay_item_quantity].[span]='" & span & "'", conn)
data_reader = cmd.ExecuteReader()
If data_reader.HasRows = True Then
Do While data_reader.Read()
payItem = CDbl(data_reader.Item("pay item"))
subpayItem = CDbl(data_reader.Item("sub pay item"))
subItem = data_reader.Item("sub item")
unit = data_reader.Item("unit")
bridgeType = data_reader.Item("bridge type")
span = (data_reader.Item("span").ToString())
quantity = CDbl(data_reader.Item("quantity"))
rate = CDbl(data_reader.Item("rate"))
RichTextBox1.AppendText(payItem & vbTab & vbTab & " " & " " & subpayItem & vbTab & vbTab & subItem & vbTab & vbTab & " " & _
unit & vbTab & vbTab & " " & " " & " " & " " & bridgeType & vbTab & vbTab & " " & span & vbTab & _
"" & vbTab & " " & quantity & vbTab & rate & vbNewLine)
Loop
Else
MsgBox("Unit rate does not exist", vbCritical, "Bridge Construction Cost Estimate")
End If
It raises the exception when it reaches the do while loop indicating at pay item.
So the specified name does not exist in the DataReader as the documentation suggests:
IndexOutOfRangeException: No column with the specified name was found
and i cannot see this column in your query. You are selecting only quantity and rate.

Checking if destination exists when using CreateTextFile

I am making a form that the user fills out and when complete, they click a command button and the text they inputted is saved as a text file in a folder in the C: drive called Survey Results.
Currently, the user has to create the file "Survey Results" manually, and if it does not exist in the C: drive, there is an error in Word. I want some way to check if the destination exists before this error occurs and either prompt the user to create the folder or automatically create the folder.
What would I include in my while loop to check for this?
do while ()
messagebox1.open
loop
Private Sub CommandButton1_Click()
Dim FS As FileSystemObject
Set FS = New FileSystemObject
Dim MyFile As TextStream
Dim i As Integer
Dim FilePrefix As String, FileName As String, Extension As String
If CommandButton1.Enabled = True Then
FilePrefix = "C:\SurveyResults\"
Extension = ".txt"
i = 1
FileName = FilePrefix & comop & Trim(Str(i)) & Extension
Do While (FS.FileExists(FileName))
i = i + 1
FileName = FilePrefix & comop & Trim(Str(i)) & Extension
Loop
Set MyFile = FS.CreateTextFile(FileName)
MyFile.Write num1 & vbNewLine & num2 & vbNewLine & _
num3 & vbNewLine & num4 & vbNewLine & _
num5 & vbNewLine & num6 & vbNewLine & _
num7 & vbNewLine & num8 & vbNewLine & num9 & vbNewLine & num10 & vbNewLine & num11 _
& vbNewLine & num12 & vbNewLine & num13 & vbNewLine & num14 & vbNewLine _
& num15 & vbNewLine & num16 & vbNewLine & num17 & vbNewLine & num18 & vbNewLine & num19 & vbNewLine _
& num20 & vbNewLine & num21 & vbNewLine & num22 & vbNewLine & num23 & vbNewLine _
& num24 & vbNewLine & num25 & vbNewLine & num26 & vbNewLine & num27 & vbNewLine & num28 & vbNewLine _
& num29 & vbNewLine & num30 & vbNewLine & num31 & vbNewLine & num32 & vbNewLine & num33 & vbNewLine _
& com1a & vbNewLine & com1b & vbNewLine & com1c & vbNewLine & com1d & vbNewLine & com1e & vbNewLine _
& com1f & vbNewLine & com1g & vbNewLine & com1i & vbNewLine & com1j & vbNewLine & com1k & vbNewLine _
& com2a & vbNewLine & com2b & vbNewLine & com2c & vbNewLine & com2d & vbNewLine & com2e & vbNewLine _
& com2f & vbNewLine & com2g & vbNewLine _
& com3a & vbNewLine & com3b & vbNewLine & com3c & vbNewLine & com3d & vbNewLine & com3e & vbNewLine _
& com3f & vbNewLine _
& com4a & vbNewLine & com4b & vbNewLine & com4c & vbNewLine & com4d & vbNewLine & com4e & vbNewLine _
& com4f & vbNewLine & com4g & vbNewLine & com4h & vbNewLine & com4i & vbNewLine & com4j & vbNewLine & com4k & vbNewLine & com4l & vbNewLine _
& com5a & vbNewLine & com5b & vbNewLine & com5c & vbNewLine & com5d & vbNewLine & com5e & vbNewLine _
& com5f & vbNewLine & com5g & vbNewLine & com5h & vbNewLine & com5i & vbNewLine & com5j & vbNewLine _
& com6a & vbNewLine & com6b & vbNewLine & com6c & vbNewLine & com6d & vbNewLine _
& com1hb & vbNewLine & com1hd & vbNewLine _
& com1hf & vbNewLine & com1hh & vbNewLine & com1hj & vbNewLine & comop
End If
End Sub
Do something like the below snippet:
Dim FilePrefix$, fs As New Scripting.FileSystemObject
FilePrefix = "C:\SurveyResults\"
If Dir(FilePrefix, vbDirectory) <> "" Then
'directory exists
Else
'directory does not exist
fs.CreateFolder FilePrefix ' create the dir
End If