vb.net webclient class throw an out of memory exception - vb.net

I use VB.Net with VisualStudio 2015 and .net 4.5.2.
I use the System.Net.WebClient class to UPLOAD ASYNC FILES into a webserver with the methode UploadFileAsync(address as URI, fileName as string) but a System.OutOfMemoryException accured when the fileSize is too large...
Any idea?
Thank you!

Resolved.
There was an error here :
postStreamHeaders = boundary & vbCrLf & "Content-Disposition: form-data; name=""var1""" & vbCrLf & vbCrLf & "val1" & vbCrLf
postStreamHeaders = boundary & vbCrLf & "Content-Disposition: form-data; name=""var2""" & vbCrLf & vbCrLf & "val2" & vbCrLf
postStreamFooters = boundary & "--"
The good syntax is :
postStreamHeaders = "--" & boundary & vbCrLf & "Content-Disposition: form-data; name=""var1""" & vbCrLf & vbCrLf & "val1" & vbCrLf
postStreamHeaders &= "--" & boundary & vbCrLf & "Content-Disposition: form-data; name=""var2""" & vbCrLf & vbCrLf & "val2" & vbCrLf
postStreamFooters = "--" & boundary & "--" & vbCrLf
Hope this may help someone else ...

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.

Deserialize non xml or json response from API

Been trying to figure out if there is a way to deserialize this object below. the problem is the api returns the data in this format. There seems to be an object here called Info that is listof()..
"found=23" & vbCrLf & "info[0].Channel=0" & vbCrLf & "info[0].EndTime=2020-05-11 00:59:59" & vbCrLf & "info[0].EnteredSubtotal=0" & vbCrLf & "info[0].ExitedSubtotal=0" & vbCrLf & "info[0].RuleName=NumberStat" & vbCrLf & "info[0].StartTime=2020-05-11 00:00:00" & vbCrLf & "info[1].Channel=0" & vbCrLf & "info[1].EndTime=2020-05-11 01:59:59" & vbCrLf & "info[1].EnteredSubtotal=0" & vbCrLf & "info[1].ExitedSubtotal=0" & vbCrLf & "info[1].RuleName=NumberStat" & vbCrLf & "info[1].StartTime=2020-05-11 01:00:00" & vbCrLf & "info[2].Channel=0" & vbCrLf & "info[2].EndTime=2020-05-11 02:59:59" & vbCrLf & "info[2].EnteredSubtotal=0" & vbCrLf & "info[2].ExitedSubtotal=0" & vbCrLf & "info[2].RuleName=NumberStat" & vbCrLf & "info[2].StartTime=2020-05-11 02:00:00" & vbCrLf & "info[3].Channel=0" & vbCrLf & "info[3].EndTime=2020-05-11 03:59:59" & vbCrLf & "info[3].EnteredSubtotal=0" & vbCrLf & "info[3].ExitedSubtotal=0" & vbCrLf & "info[3].RuleName=NumberStat" & vbCrLf & "info[3].StartTime=2020-05-11 03:00:00" & vbCrLf & "info
Thanks in advance for any ideas here.
Looks like it is VBscript.
vbCrLf means new line so:
"found=23"
"info[0].Channel=0"
"info[0].EndTime=2020-05-11 00:59:59"
"info[0].EnteredSubtotal=0"
"info[0].ExitedSubtotal=0"
"info[0].RuleName=NumberStat"
"info[0].StartTime=2020-05-11 00:00:00"
"info[1].Channel=0"
"info[1].EndTime=2020-05-11 01:59:59"
"info[1].EnteredSubtotal=0"
"info[1].ExitedSubtotal=0"
"info[1].RuleName=NumberStat"
"info[1].StartTime=2020-05-11 01:00:00"
"info[2].Channel=0"
"info[2].EndTime=2020-05-11 02:59:59"
"info[2].EnteredSubtotal=0"
"info[2].ExitedSubtotal=0"
"info[2].RuleName=NumberStat"
"info[2].StartTime=2020-05-11 02:00:00"
"info[3].Channel=0"
"info[3].EndTime=2020-05-11 03:59:59"
"info[3].EnteredSubtotal=0"
"info[3].ExitedSubtotal=0"
"info[3].RuleName=NumberStat"
"info[3].StartTime=2020-05-11 03:00:00"
I think this might be useful:
How to run VBScript in .net core or .net standard project?

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

Insert Variable Into SQL Query Visual Studio 2010

I have a SQL query that looks like this;
Dim query As String = "SELECT ID, " & Environment.NewLine & _
"month_of_sale, " & Environment.NewLine & _
"client_last_name, " & Environment.NewLine & _
"client_first_name, " & Environment.NewLine & _
"deal_number, " & Environment.NewLine & _
"stock_number, " & Environment.NewLine & _
"delivery_date, " & Environment.NewLine & _
"finance_manager, " & Environment.NewLine & _
"finance_income, " & Environment.NewLine & _
"record_entered " & Environment.NewLine & _
"FROM(dodge_records)" & Environment.NewLine & _
"WHERE month_of_sale = #month_of_sale AND client_last_name = #client_last_name" & Environment.NewLine & _
"ORDER BY client_last_name"
I am trying to insert a variable into the SQL query but I don't see it happening when I debug the website. Here is the rest of the code so that it makes more sense.
Case "Last Name"
Try
Dim query As String = "SELECT ID, " & Environment.NewLine & _
"month_of_sale, " & Environment.NewLine & _
"client_last_name, " & Environment.NewLine & _
"client_first_name, " & Environment.NewLine & _
"deal_number, " & Environment.NewLine & _
"stock_number, " & Environment.NewLine & _
"delivery_date, " & Environment.NewLine & _
"finance_manager, " & Environment.NewLine & _
"finance_income, " & Environment.NewLine & _
"record_entered " & Environment.NewLine & _
"FROM(dodge_records)" & Environment.NewLine & _
"WHERE month_of_sale = #month_of_sale" & Environment.NewLine & _
"WHERE client_last_name = #client_last_name" & Environment.NewLine & _
"ORDER BY client_last_name"
Using Connection = New MySqlConnection(connStr)
Using da = New MySqlDataAdapter(query, Connection)
da.SelectCommand.Parameters.AddWithValue("#month_of_sale", CurrentMonth)
da.SelectCommand.Parameters.AddWithValue("#client_last_name", ValueBoxText)
Dim DS As New DataSet
da.Fill(DS)
DodgeSearchGridView.DataSource = DS
DodgeSearchGridView.DataBind()
Connection.Close()
End Using
End Using
Catch ex As Exception
End Try
CurrentMonth & ValueBoxText are strings that I need inserted into the SQL query. Right now this isn't working how I want it to. I don't get an error message or anything, the query doesn't return anything and I have double checked the database to make sure there is data in it that would be returned by this query. Can anyone help me error check the code so I can find out how to make this work? Thanks!
Your SQL is incorrect. You have used WHERE twice. The second condition should use AND ie
AND client_last_name
Step through the code in the VS debugger and paste the query generated from the command into SQL Server Management studio and try it there.

Format Exception Error when writing text to a file in VB

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