How To Make A File Be Named From A Texbox in VB - vb.net

Im trying to get a file to be created and named when a button is pressed. But I need the file name to contain text from a textbox.
Code:
My.Computer.FileSystem.WriteAllText("c:\temp\" + txtUser.Text + ".txt", "[" + TimeOfDay + "]" + "Email: " + txtEmail.Text + vbNewLine + "Username: " + txtUser.Text + vbNewLine + "Password: " + txtPass.Text + vbNewLine + "Secuirty: " + txtSecuirty.Text + vbNewLine + vbNewLine, True)
Simple Code:
My.Computer.FileSystem.WriteAllText("c:\temp\" + txtUser.Text + ".txt", {loads of stuff}, True)
I have loads of if functions, to stop the disallowed characters.
The error I am getting is:
Expression does not produce a value

Try to see if this will work for you.
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
My.Computer.FileSystem.WriteAllText("c:\temp\" + txtUser.Text.ToString() + ".txt", "test message", True)
End Sub
Or you can change your code to
Dim strFileText As String = ""
strFileText =
"[" & TimeOfDay.ToString() & "] " & vbCrLf &
"Email: " & txtEmail.Text.ToString() & vbCrLf &
"Username: " & txtUser.Text.ToString() & vbCrLf &
"Password: " & txtPass.Text.ToString() & vbCrLf &
"Security: " & txtSecurity.Text.ToString() & vbCrLf & vbCrLf
My.Computer.FileSystem.WriteAllText("c:\temp\" + txtUser.Text.ToString() + ".txt", strFileText, True)

Related

If Statement to change field.control source

I have a small If statement that that changes the controlsource of a field if another field is empty. The " just before = is incorrect, and I'm not sure what to use to ensure the entire string starting with the = is included.
Private Sub Report_Load()
If IsNull(FirstName2) Then
OwnersNames.ControlSource = "=FirstName1] & " " & [LastName1]"
Else
OwnersNames.ControlSource = "=[FirstName1] & " " & [LastName1] & " and " & [FirstName2] & " " & [LastName2]"
End If
End Sub
If I'm reading this correctly, then try this which uses more quote marks to concatenate the statement:
Private Sub Report_Load()
If IsNull(FirstName2) Then
OwnersNames.ControlSource = "=[FirstName1]" & " " & "[LastName1]"
Else
OwnersNames.ControlSource = "=[FirstName1]" & " " & "[LastName1]" & " and " & "[FirstName2]" & " " & "[LastName2]"
End If
End Sub
If you are trying to concatenate the fields (e.g. 'add' them together). You need something like:
Private Sub Report_Load()
If IsNull(FirstName2) Then
OwnersNames.ControlSource = "=[FirstName1] & ' ' & [LastName1]"
Else
OwnersNames.ControlSource = "=[FirstName1] & ' ' & [LastName1] & ' ' & [FirstName2] & ' ' & [LastName2]"
End If
End Sub
Keep in mind you can concatenate text fields (e.g. text boxes) together.
I'm uncertain if combining the control sources of fields works the same way. To be sure, one option is to rename each textbox to something different from its control source.
Here's the code now, which when loaded produces 'John Smith And' even though the Firstname2 field is null.
Private Sub Report_Load()
If Me.FirstName2 = vbNullString Then
OwnersNames.ControlSource = "=[FirstName1] & ' ' & [LastName1]"
Else
OwnersNames.ControlSource = "=[FirstName1] & ' ' & [LastName1] & ' and ' & [FirstName2] & ' ' & [LastName2]"
End If
End Sub
Are you really meaning to set the control source or just the value of the field?
I think this might be what you want to accomplish????
Private Sub Report_Load()
If Me.FirstName2 = "" Then
me.OwnersNames = [FirstName1] & " " & [LastName1]
Else
me.OwnersNames = [FirstName1] & " " & [LastName1] & _
" and " & [FirstName2] & " " & [LastName2]
End If
End Sub

want to update record if exists and insert if not exists

want to update record if exists and insert if not exists into vsolv_trn_tsalesregdump Is tsalesregdump_name is unique in table vsolv_trn_tsalesregdump
when i click check box that particular row should be check on other table and how to use it for loop in form loop i can use the "tsalesdump_date,tsalesdump_name,executivename_executive" this three if the value is same its go to update and otherwise its not to same its move on insert how can i write it can any 1help me
if i have use two table one temp and other 1 is orginal\ temp table =vsolv_tmp_tsalesdump orginal table=vsolv_trn_tsalesregdump.
if already i hav save the data in orginal table,again if i hav assing data for temp table and view in radgrid if select and submit the button its go to check on orginal table if the value is already there its update it otherwise insert how can use it
Protected Sub btn_Submit_Click(ByVal sender As Object, ByVal e As System.Web.UI.ImageClickEventArgs) Handles btn_Submit.Click
lblerrmsg.Text = ""
Dim chk As CheckBox
Dim lbtn As LinkButton
Dim Res As Long
Dim lobjrow As DataRow
Dim lObjErrTable As New DataTable
Dim a As Integer = 0
Dim dtcarbooking As New DataTable
Dim dtDumpBooking As New DataTable
Dim lsRefid As String = String.Empty
Dim dtdumpsample As New DataTable
Dim name As String = String.Empty
If Check_Validation() = True Then
Exit Sub
End If
Gobjdbconn.OpenConn()
lObjErrTable = Error_TabelFill()
For i As Integer = 0 To GVUpload.Items.Count - 1
chk = GVUpload.Items(i).FindControl("chkupload")
If chk.Checked = True Then
lsRefid = GVUpload.Items(i).GetDataKeyValue("tsalesdump_gid").ToString()
MsSql = ""
MsSql &= "Select tsalesdump_gid,tsalesdump_date,tsalesdump_name,executivename_executive,tsalesdump_vch_no,tsalesdump_debit,tsalesdump_credit"
MsSql &= " from vsolv_tmp_tsalesdump as a"
MsSql &= " left join vsolv_trn_executivename as b on a.tsalesdump_name = b.executivename_particulars"
MsSql &= " where tsalesdump_gid = '" & lsRefid & "' and tsalesdump_isremoved = 'N'"
dtDumpBooking = Gobjdbconn.GetDataTable(MsSql)
MsSql = ""
MsSql &= "Insert into vsolv_trn_tsalesregdump(tsalesregdump_date,tsalesregdump_name,tsalesregdump_executive,tsalesregdump_vch_no"
MsSql &= " ,tsalesregdump_debit,tsalesregdump_credit,tsalesregdump_importby)"
MsSql &= " Values ('" & Format(CDate(dtDumpBooking.Rows(0).Item("tsalesdump_date")), "yyyy-MMM-dd").ToString() & "'"
MsSql &= ",'" & dtDumpBooking.Rows(0).Item("tsalesdump_name").ToString() & "'"
MsSql &= ",'" & dtDumpBooking.Rows(0).Item("executivename_executive").ToString() & "'"
MsSql &= " ,'" & dtDumpBooking.Rows(0).Item("tsalesdump_vch_no").ToString() & "'"
MsSql &= " ,'" & dtDumpBooking.Rows(0).Item("tsalesdump_debit").ToString() & "','" & dtDumpBooking.Rows(0).Item("tsalesdump_credit").ToString() & "','SIVA')"
MnResult = Gobjdbconn.ExecuteNonQuerySQL(MsSql)
If MnResult = 1 Then
MsSql = ""
MsSql &= " Delete from vsolv_tmp_tsalesdump where tsalesdump_gid = '" & lsRefid & "'"
Gobjdbconn.ExecuteNonQuerySQL(MsSql)
Else
lobjrow = lObjErrTable.NewRow()
a = a + 1
lobjrow("Sno") = a
lobjrow("Booking Ref No") = dtcarbooking.Rows(0).Item("tsalesregdump_name").ToString
lobjrow("Description") = "Duplicate Record"
lObjErrTable.Rows.Add(lobjrow)
End If
End If
Next
POPSummary()
If Not lObjErrTable Is Nothing Then
If lObjErrTable.Rows.Count > 0 Then
Call Pop_Data(lObjErrTable)
End If
End If
End Sub
If you want to update record if already exists else insert in SQL then you can achieve by following: You need to change MsSql string for Insert command
MsSql = "" + _
"IF EXISTS(Select tsalesregdump_name From vsolv_trn_tsalesregdump Where tsalesregdump_name = '" & dtDumpBooking.Rows(0).Item("tsalesdump_name").ToString() & "') " + _
"BEGIN " + _
" Update vsolv_trn_tsalesregdump " + _
" Set tsalesregdump_date = '" & Format(CDate(dtDumpBooking.Rows(0).Item("tsalesdump_date")), "yyyy-MMM-dd").ToString() & "' " + _
" ,tsalesregdump_executive = '" & dtDumpBooking.Rows(0).Item("executivename_executive").ToString() & "' " + _
" ,tsalesregdump_vch_no = '" & dtDumpBooking.Rows(0).Item("tsalesdump_vch_no").ToString() & "' " + _
" ,tsalesregdump_debit = '" & dtDumpBooking.Rows(0).Item("tsalesdump_debit").ToString() & "' " + _
" ,tsalesregdump_credit = '" & dtDumpBooking.Rows(0).Item("tsalesdump_credit").ToString() & "' " + _
" ,tsalesregdump_importby = 'SIVA' " + _
" Where tsalesregdump_name = '" & dtDumpBooking.Rows(0).Item("tsalesdump_name").ToString() & "' " + _
"END " + _
"ELSE " + _
"BEGIN " + _
" Insert into vsolv_trn_tsalesregdump(tsalesregdump_date,tsalesregdump_name,tsalesregdump_executive,tsalesregdump_vch_no " + _
" ,tsalesregdump_debit,tsalesregdump_credit,tsalesregdump_importby) " + _
" Values ('" & Format(CDate(dtDumpBooking.Rows(0).Item("tsalesdump_date")), "yyyy-MMM-dd").ToString() & "' " + _
" ,'" & dtDumpBooking.Rows(0).Item("tsalesdump_name").ToString() & "' " + _
" ,'" & dtDumpBooking.Rows(0).Item("executivename_executive").ToString() & "' " + _
" ,'" & dtDumpBooking.Rows(0).Item("tsalesdump_vch_no").ToString() & "' " + _
" ,'" & dtDumpBooking.Rows(0).Item("tsalesdump_debit").ToString() & "','" & dtDumpBooking.Rows(0).Item("tsalesdump_credit").ToString() & "','SIVA') " + _
"END "
MnResult = Gobjdbconn.ExecuteNonQuerySQL(MsSql)
I am assuming here that tsalesregdump_name is unique.

Treenode not appearing when program restarts

the below code is run from a double click event on a listbox that copies the selected file to the selected node directory on a treeview then adds the selected text as a child node.
It appears to work fine however when the program is closed and then re-opened its not showing the child node.
Any pointers........
Dim Copy2 = aMailbox & tvProgress.SelectedNode.Text & "\" & lstRequired.Text
Dim Copy1 = rPath & "\" & lstRequired.Text
If File.Exists(Copy2) Then
MsgBox("File already added. Please edit from the view above", MsgBoxStyle.OkOnly, "Lynx Control Panel")
Exit Sub
End If
If File.Exists(Copy1) Then
File.Copy(Copy1, Copy2)
tvProgress.SelectedNode.Nodes.Add(lstRequired.Text)
tvProgress.ExpandAll()
Else
MsgBox("This file no longer exists in your Lynx Repository. Please select another", MsgBoxStyle.OkOnly, "Lynx Control Panel")
Exit Sub
End If
The below code is taken entirely from a doubleclick event of the 1st listbox
Dim n As Integer
Dim i As Integer = lstPlanned.SelectedIndex
If lstPlanned.SelectedItems.Count = 0 Then Exit Sub
For n = 0 To UBound(AllDetails)
If AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps = lstPlanned.SelectedItem Then
If Not My.Computer.FileSystem.DirectoryExists(zMailbox & AllDetails(n).uFile) Then
MsgBox("No files located for " & vbNewLine & (AllDetails(n).uName & " (" & AllDetails(n).uCode) & ")" & vbNewLine & " " & vbNewLine & "Please try another...", MsgBoxStyle.OkOnly, "Lynx Control Panel")
Exit Sub
End If
System.IO.Directory.CreateDirectory(aMailbox & "\" & AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps)
For Each f In Directory.GetFiles(zMailbox & AllDetails(n).uFile, "*.dbf")
If File.Exists(f) Then
File.Copy(f, Path.Combine(aMailbox & "\" & AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps, Path.GetFileName(f)), True)
End If
Next
For Each f In Directory.GetFiles(zMailbox & AllDetails(n).uFile, "*.ini", SearchOption.AllDirectories)
If File.Exists(f) Then
File.Copy(f, Path.Combine(aMailbox & "\" & AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps, Path.GetFileName(f)), True)
End If
Next
For Each f In Directory.GetFiles(zMailbox & AllDetails(n).uFile, "*.txt", SearchOption.AllDirectories)
If File.Exists(f) Then
File.Copy(f, Path.Combine(aMailbox & "\" & AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps, Path.GetFileName(f)), True)
End If
Next
tvProgress.Nodes.Remove(rN)
tvProgress.Nodes.Insert(0, rN)
tvProgress.Nodes.Add(New TreeNode(AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps))
If i >= 0 And i < lstPlanned.Items.Count Then
lstPlanned.Items.RemoveAt(i)
End If
Exit Sub
End If
Next
Think that's want you want..maybe ;)
...
System.IO.Directory.CreateDirectory(aMailbox & "\" & AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps)
' After the Dir is created Node is added to the TreeView
tvProgress.Nodes.Remove(rN)
tvProgress.Nodes.Insert(0, rN)
tvProgress.Nodes.Add(New TreeNode(AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps))
For Each f In Directory.GetFiles(zMailbox & AllDetails(n).uFile, "*.dbf")
If File.Exists(f) Then
File.Copy(f, Path.Combine(aMailbox & "\" & AllDetails(n).uName & " - " & AllDetails(n).uCode & " - " & AllDetails(n).uOps, Path.GetFileName(f)), True)
'As ParentNode already exists as Pos 0 you can add child nodes to it
tvProgress.Nodes(0).Nodes.Add(Path.GetFileName(f))
End If
Next
...

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

VB.NET Lambda expression instead of iterator

I am fairly certain that this could be rewritten as a Lambda expression, but every attempt fails miserably. I know, C# Lambda reads cleaner, but I'm stuck with VB.NET. Here is the code - could someone point me in the right direction? Thanks!
For Each e As EventToMonitor In Events
If e.TypeID = 1 Then
If ("," & e.Values).Contains("," & b.ChoiceID & ",") Then
Notify(cacheValues, e, "Event notification (button press)", "Button pressed: " & b.Text & " on screen: " & b.GroupBox.Text & Environment.NewLine &
"User: " & cacheValues.CurrentUserName & Environment.NewLine & _
"Pressed at: " & Date.Now.ToShortDateString & " " & Date.Now.ToShortTimeString)
End If
End If
Next
Something like this should work if you want to convert the whole thing to lambda:
Events.Where(Function(e) e.TypeID = 1 AndAlso ("," & e.Values).Contains("," & b.ChoiceID & ",")) _
.ToList() _
.ForEach(Sub(e) Notify(cacheValues, e, "Event notification (button press)", "Button pressed: " & b.Text & " on screen: " & b.GroupBox.Text & Environment.NewLine & _
"User: " & cacheValues.CurrentUserName & Environment.NewLine & _
"Pressed at: " & Date.Now.ToShortDateString & " " & Date.Now.ToShortTimeString))
Honestly, though, I usually prefer to just use them to filter down results or build a collection:
Dim eventsList = Events.Where(Function(e) e.TypeID = 1 AndAlso ("," & e.Values).Contains("," & b.ChoiceID & ","))
For Each e As EventToMonitor In eventsList
Notify(cacheValues, e, "Event notification (button press)", "Button pressed: " & b.Text & " on screen: " & b.GroupBox.Text & Environment.NewLine &
"User: " & cacheValues.CurrentUserName & Environment.NewLine & _
"Pressed at: " & Date.Now.ToShortDateString & " " & Date.Now.ToShortTimeString)
Next