|
This should be a relatively easy thing to do. If your still stuck, post the code that you have so that we can see what your doing.
Any suggestions, ideas, or 'constructive criticism' are always welcome.
"There's no such thing as a stupid question, only stupid people." - Mr. Garrison
|
|
|
|
|
This is the code I am using; the result is that the insert is placed only in the last line of the text file (not immediately after all occurences of "CBS*TODAY*ZZ*" like I need it to).
Private Sub btnTranslate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTranslate.Click<br />
MessageBox.Show("you are about to translate file")<br />
<br />
'read the file to find data<br />
Dim sReader As New IO.StreamReader("C:\shadow\moviez_21.txt")<br />
Dim CurrentLine As String<br />
<br />
Dim DataLines As New System.Collections.Specialized.StringCollection<br />
<br />
CurrentLine = sReader.ReadLine<br />
If CurrentLine.StartsWith("CBS*TODAY*ZZ*") = True Then<br />
sReader.ReadToEnd()<br />
End If<br />
sReader.Close()<br />
<br />
'modifying the file<br />
Dim oError As System.IO.StreamWriter<br />
oError = New System.IO.StreamWriter("c:\Shadow\moviez_21.txt", True)<br />
<br />
'text to be inserted always after CBS*TODAY*ZZ*<br />
oError.Write("REF*LU*") ' + file number<br />
oError.Close()<br />
End Sub
Thanks
|
|
|
|
|
Can you explain how you were thinking when writing this? It's not even close to working...
IvanIT wrote: CurrentLine = sReader.ReadLine
If CurrentLine.StartsWith("CBS*TODAY*ZZ*") = True Then
sReader.ReadToEnd()
End If
Here you only read the first line of the file and throw away the rest. Then you just forget about the line that you read, because you never use that for anything.
The comment earlier in the code suggests that you attempt to find a line in the file, but you don't have any loop so the only line that you ever look at is the first one.
IvanIT wrote: oError = New System.IO.StreamWriter("c:\Shadow\moviez_21.txt", True)
Here you open the file for appending, that means that you contine to write at the end of the file.
IvanIT wrote: 'text to be inserted always after CBS*TODAY*ZZ*
oError.Write("REF*LU*") ' + file number
The comment doesn't at all correspond to what you are actually doing. You are writing that string at the end of the file, regardless if you found the string that you were looking for or not.
All the code that you have written could be replaced with just this:
Private Sub btnTranslate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTranslate.Click
MessageBox.Show("you are about to translate file")
System.IO.File.AppendAllText("c:\Shadow\moviez_21.txt", "REF*LU*")
End Sub
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
I am actually in a better place now with the code below. But here comes my problems:
1. the FileName will change from time to time
2. the myString will always StartsWith SOM*PE*ZZ*, but the last 4 digits will change. How do I make that variable?
Like I said, I have been testing the code below and except for the above 2 issues, its smooth sailing. I would greatly appreciate input in making the tweaks.
Imports System<br />
Imports System.Text<br />
Imports System.Windows.Forms<br />
<br />
Private Sub btnTranslate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnTranslate.Click<br />
<br />
MessageBox.Show("you are about to translate file")<br />
MessageBox.Show("you are about to translate file")<br />
<br />
Dim refNum As String = Me.txtFileNum.Text<br />
Dim FileName As String = "C:\moviez_21.txt"<br />
Dim myString As String = "SOM*PE*ZZ*1041"<br />
<br />
Dim newString As New StringBuilder<br />
newString.Append(My.Computer.FileSystem.ReadAllText(FileName))<br />
newString.Replace(myString, myString & vbNewLine & "TODAY*007*" & refNum & "~")<br />
My.Computer.FileSystem.WriteAllText(FileName, newString.ToString, False)End Sub
|
|
|
|
|
Hm... why are you using a StringBuilder when you are not building a string?
You can use a regular expression to match the line and do the replacement. How about a one-liner:
File.WriteAllText(fileName, Regex.Replace(File.ReadAllText(fileName), "(SOM\*PE\*ZZ\*\d{4})", "$1" + Environment.NewLine + "TODAY*007*" + refNum.ToString() + "~"))
Despite everything, the person most likely to be fooling you next is yourself.
|
|
|
|
|
Read line the lines using ReadAllLines, then open a StreamWriter on the file and write each line. When you come to the place where you want to add the line, just write it to the StreamWriter.
Despite everything, the person most likely to be fooling you next is yourself.
modified on Monday, January 26, 2009 12:51 PM
|
|
|
|
|
Hi All,
I am trying to dev a small application with login form, but i am not able to unload login form after confirming the user id and password.
Any help will be appreciated.
Thanks in advance.
Regards.
Zanzmera
|
|
|
|
|
me.close for close the form and form_name.show to show form
|
|
|
|
|
Your login form should NOT be your startup form. You applications main form should be the startup, checking to see if a user is logged in, and if not, the main form should create and show a login form.
|
|
|
|
|
Please try to use this code snippet
Sub Main()
Dim splash As New <<your-login-form-name>>
splash.Show()
Do Until isCompleted
System.Windows.Forms.Application.DoEvents()
Loop
splash.Close()
System.Windows.Forms.Application.DoEvents()
System.Windows.Forms.Application.Run(New <<your-main-form-name>>)
Catch ex As Exception
MessageBox.Show(ex.Message, "Sub Main:" + ex.Source, MessageBoxButtons.OK, MessageBoxIcon.Information)
'' Do proper exception handling stuff here
End Try
End Sub
And Insert following code in case of log in successful in <your-login-form>
isCompleted
Me.Close()
Now, Set Sub Main as your startup object in Project properties
Hope I answered your query
|
|
|
|
|
I want to insert data from DateTimePicker to Access database but have error "Data type mismatch in criteria expression"
this is the code
Dim aCon As OleDbConnection
Dim aCmd As OleDbCommand
Dim strCon As String
strAcessFilePath = System.Windows.Forms.Application.StartupPath &; "\rabotnici.mdb"
'create connection with mdb file
strCon = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strAcessFilePath & ";User ID=Admin;Password="
aCon = New OleDbConnection(strCon)
aCon.Open()
strQuery = "insert into data_pr values(" + " '" + DateTimePicker1.Value + "'" + ")"
aCmd = New OleDbCommand(strQuery, aCon)
Dim da As New OleDbDataAdapter
da.SelectCommand = aCmd
aCmd.ExecuteNonQuery()
aCon.Close()
access database field format is Date/time
Can You help me thanks
|
|
|
|
|
Instead of concatenating values to sql strings, use parameters. That way you will avoid conversion problems (among many other things such as sql injections etc). Have a look at: OleDbParameter Class[^]
|
|
|
|
|
but this work when access field format is text
|
|
|
|
|
Yes it does and also it can be done for dates and numbers.
Access accepts dates in format #mm/dd/yyyy# . So for example today would be #01/25/2009# (without quotes).
However, I hope that you don't take this shortcut since it's not the proper way to go. I already mentioned sql injections which is prevented by using parameters. To give a few more examples that would be problematic:
- numbers, what if decimal separator is comma (as it is for example in my case). When updating a column you would have for example UPDATE ... SET ColumnName = 49,6 ... This would lead to an error
- text, what if your text contains a quote, for example: UPDATE ... SET ColumnName = 'Guns 'n' Roses'. Error again
- dates, input date is in different format. For example I use 25.1.2009 (Finnish format) etc.
So what I'm trying to say is that it's few more lines of code, but the quality and robustness of your application will greatly improve when you use parameters.
|
|
|
|
|
OK I try to use parameters thanks very much, but are you sure that work
|
|
|
|
|
ivo75 wrote: are you sure that work
See for yourself, that's what they are for
You could try something like this:
...
strQuery = "insert into data_pr values(?)"
aCmd = New OleDbCommand(strQuery, aCon)
aCmd.Parameters.AddWithValue("ColumName", DateTimePicker1.Value)
aCmd.ExecuteNonQuery()
...
|
|
|
|
|
not work give me this error again "Data type mismatch in criteria expression"
|
|
|
|
|
ivo75 wrote: "Data type mismatch in criteria expression"
This just means that the data type of the field in your database is not the same as the value that you are trying to write into it.
Did you try parametric method as suggested by Mika?
|
|
|
|
|
Could you post the code you're currently having.
|
|
|
|
|
Try this in your vb code:
CONVERT(DATETIME, '" &amp; Format(datetimepicker1.value, "MM/dd/yyyy") &amp; " 00:00:00', 102)
If this doesn't work change the code 102 to 101 or 103. It might be one of those codes that you need to use.
should do the trick
|
|
|
|
|
Tahnk You very much everybody this is the code who work
strCon = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & strAcessFilePath & ";User ID=Admin;Password="
aCon = New OleDbConnection(strCon)
aCon.Open()
strQuery = "insert into rabotnici values(?,?,?)"
param1 = New OleDbParameter("@rabotnik_name", OleDbType.VarWChar, 100)
param1.Value = ComboBox1.Text
param2 = New OleDbParameter("@obekt_name", OleDbType.VarWChar, 100)
param2.Value = ComboBox2.Text
param3 = New OleDbParameter("@data_rabota", OleDbType.Date)
param3.Value = DateTimePicker1.Value.ToShortDateString()
aCmd = New OleDbCommand(strQuery, aCon)
aCmd.Parameters.Add(param1)
aCmd.Parameters.Add(param2)
aCmd.Parameters.Add(param3)
aCmd.ExecuteNonQuery()
aCon.Close()
this is work with parameters, again thanks
|
|
|
|
|
I have a stream of text on display in a multiline textbox. I would line to insert the following new line of text ("TODAY 007") in the line just below every occurences of "PRV*PE*ZZ*1041C0700X~". So the result should be:
PRV*PE*ZZ*1041C0700X~
TODAY 007
This should repeat to the end of the file.
How do I start to code this? Please if anyone has a code sample. Thanks
|
|
|
|
|
Read the file line by line and if the current line matches your pattern, put a line break character and insert the required text.
|
|
|
|
|
Here is the code I have so far, but when I invoke, nothing happens.
Dim sReader As New IO.StreamReader("C:\\File.txt")<br />
Dim CurrentLine As String = "TODAY 007"<br />
Dim DataLines As New System.Collections.Specialized.StringCollection<br />
<br />
Do While sReader.EndOfStream = False<br />
CurrentLine = sReader.ReadLine<br />
If CurrentLine.Contains("PRV*PE") = False Then<br />
DataLines.Add(CurrentLine)<br />
End If<br />
Loop<br />
sReader.Close()<br />
sReader = Nothing<br />
<br />
Dim sWriter As New IO.StreamWriter("C:\\File.txt")<br />
<br />
For Each CurrentLine In DataLines<br />
sWriter.WriteLine(CurrentLine)<br />
Next<br />
sWriter.Close()<br />
sWriter = Nothing
Any suggestions?
|
|
|
|
|
IvanIT wrote: If CurrentLine.Contains("PRV*PE") = False Then
DataLines.Add(CurrentLine)
End If
So, what are you going to do if the contains method returns True ? Right now, you're just dropping the line and not doing anything with it. You've completely dropped your string of "TODAY 007" and the logic doesn't come close to following your specified requirements. Shouldn't your code read something more like:
Do While Not sReader.EndOfStream
DataLines.Add(CurrentLine)
If CurrentLine.Contains("PRV*PE") Then
DataLines.Add("TODAY 007")
End If
Loop
|
|
|
|