|
Hello there,
I tried a couple of ways and made a lot of changes, however if I run this in debugging mode it works, it stops the timer and display msgbox.
But if I run this outside it does not stop timer and does not display msgbox..
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick<br />
<br />
Timer1.Start()<br />
<br />
Label1.Text = formatDigits(Now.Hour) & ":" & formatDigits(Now.Minute) & ":" & formatDigits(Now.Second)<br />
<br />
Shutdown()<br />
<br />
End Sub<br />
<br />
Private Sub Shutdown()<br />
<br />
Dim myTime As Date = Format(Now(), "hh:mm:ss tt")<br />
Dim StartHour, StartMinute As Integer<br />
Dim EndHour, EndMinute As Integer<br />
Dim StartTime As String<br />
Dim EndTime As String<br />
<br />
StartHour = 6<br />
StartMinute = 0<br />
EndHour = 11<br />
EndMinute = 16<br />
StartTime = TimeValue(StartHour & ":" & StartMinute)<br />
EndTime = TimeValue(EndHour & ":" & EndMinute)<br />
<br />
If myTime >= StartTime And myTime < EndTime Then<br />
<br />
If String.Compare(Now.Hour.ToString, EndHour.ToString) = 0 Then<br />
If String.Compare(Now.Minute.ToString, EndMinute.ToString) = 0 Then<br />
Timer1.Stop()<br />
MsgBox("1st piece")<br />
End If<br />
End If<br />
<br />
End If<br />
<br />
End Sub<br />
<br />
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click<br />
<br />
Timer1.Enabled = True<br />
Timer1.Start()<br />
<br />
End Sub<br />
<br />
Private Function formatDigits(ByVal Value As Integer) As String<br />
<br />
Dim newValue As String<br />
Dim len As Integer = Value.ToString.Length<br />
<br />
If len = 1 Then<br />
newValue = "0" + Value.ToString<br />
Return newValue<br />
Else<br />
Return Value.ToString<br />
End If<br />
<br />
End Function
I looked at several possibilities and no luck any ideas why this might be?
thanks
|
|
|
|
|
It obvious you don't know how datatype work, or how DateTime works, or how If statements work in the .NET Framework. I would strongly suggest picking up a book on VB.NET for beginners. I lost count of the number of things wrong with this code. But, here are a few of the major problems.
First, In the Timer1_Tick handler, you do NOT nee to call Timer1.Start. It's already started.
The Shutdown method is doing all kinds of thing it does not need to do, if you only understood how Date/Time works in VB.NET, you could reduce this down to a few lines of code.
In the Button1_Click handler, you set Timer1.Enabled to True, then you call Timer1.Start, which does the exact same thing. You only need one of these lines.
FormatDigits isn't even required. The same thing can be accomplished just by specifying the correct formating string where needed.
Your (shortened) code might look something like:
Private Sub Timer1_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Timer1.Tick
Label1.Text = DateTime.Now().ToString("HH:mm:ss")
Shutdown()
End Sub
Private Sub Shutdown()
' This can be rewritten even shorter, but this is of a level that you should easily understand.
Dim StartTime As New DateTime(Now.Year, Now.Month, Now.Day, 6, 0, 0)
Dim EndTime As New DateTime(Now.Year, Now.Month, Now.Day, 11, 16, 0)
If StartTime <= Now() AndAlso Now() <= EndTime Then
Timer1.Stop()
MsgBox("1st piece")
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Timer1.Start()
End Sub
|
|
|
|
|
Thanks for pointing out the errors in my code.
As a matter of fact you're right when you suggest me starting from scratch as I only done my first 2 week course in vb.net 3 months ago
So what do you start coding when finished with "hello world" messages. Maybe one day I'll be as good a coder as you helping others on their way..
I made a change to this line in your code because it fires all the time
If StartTime <= Now() AndAlso Now() <= EndTime Then
to this
If Now() <= StartTime Or Now() >= EndTime Then
to have the event fired outside my start and end times
If I want this code to run in the background while other applications are running, will this affect other resources?
thanks Chris
|
|
|
|
|
Xooku wrote: So what do you start coding when finished with "hello world" messages.
You start reading the documentation on the .NET Framework and start writing small apps that let you test and see what various classes do and how to work with them.
Xooku wrote: I made a change to this line in your code because it fires all the time
If StartTime <= Now() AndAlso Now() <= EndTime Then
"Fires all the time"?? What did you set the Interval property on the Timer to?? This code will run every x number of milliseconds because the Timer Tick event is being called every x number of milliseconds.
Xooku wrote: If I want this code to run in the background while other applications are running, will this affect other resources?
Depends on that Timer and what your code is really doing. The faster the Timer Ticks, the more CPU is going to be taken up by whatever code is in the Tick event.
|
|
|
|
|
I've set the timer interval to "1000" for second intervals.
And made the change to this
If Now() <= StartTime Or Now() >= EndTime Then
because ie. startime "10H00" to endtime "18H00", the event must fire any time between "18H00" to the next morning "10H00".
In my previous reply I used a timer. Would this be the correct way then to use threading?
Public Class Form5<br />
Dim t As System.Threading.Thread = Nothing<br />
<br />
Private Sub Shutdown()<br />
<br />
Do<br />
System.Threading.Thread.Sleep(1000)<br />
Dim StartTime As New DateTime(Now.Year, Now.Month, Now.Day, 6, 0, 0)<br />
Dim EndTime As New DateTime(Now.Year, Now.Month, Now.Day, 11, 56, 0)<br />
<br />
If Now() <= StartTime Or Now() >= EndTime Then<br />
MsgBox("1st piece")<br />
t.Abort()<br />
End If<br />
Loop<br />
<br />
End Sub<br />
<br />
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click<br />
<br />
t = New System.Threading.Thread(New System.Threading.ThreadStart(AddressOf Shutdown))<br />
t.Start()<br />
<br />
End Sub<br />
End Class
thankyou Chris
|
|
|
|
|
Xooku wrote: In my previous reply I used a timer. Would this be the correct way then to use threading?
No, it's not. You normally wouldn't start a thread just to run a simple method like that, since it executes in under a few milliseconds. You would normally use threading to start a long-running process that would otherwise hang your user interface so it still responds when the user does something.
In your case, you'd put the entire Timer operation in a seperate thread. You'd create a seperate class that includes the loop, the start and end times, and some code to launch whatever process you need when the time comes.
|
|
|
|
|
Thank you
this has been very helpfull
Chris
|
|
|
|
|
If I want the user to change and save other start & end times and not hard code them the way I did, how would I do this?
|
|
|
|
|
Hi
can someone maybe point me in the right direction
Xooku wrote: If I want the user to change and save other start & end times and not hard code them the way I did, how would I do this?
|
|
|
|
|
hi ,
I want to monitor the power status constantly, and on Power status change launch a program.
I understand that using PowerStatus call in .net 2.0 would do that for me. What I cannot make out is how to keep the form containing PowerStatus call execute it over and over again. Should I do like a do loop for the PowerStatus call or is their a better method that would keep the method execute constantly till the form is loaded.
Using VB VB.NET 2005
modified on Thursday, January 10, 2008 6:46:55 AM
|
|
|
|
|
How about using one of the Timers built into the .NET Framework??
You might want to start with the Timer in the Toolbox and dropping it on a Form.
|
|
|
|
|
Sorry, me again...
I thought of something.
How would one go about loading data from a select statement... like this one:
<br />
Dim connectionString As String = "Initial Catalog=DBName;Data Source=InstanceName;Integrated Security=SSPI;"<br />
Dim cn As SqlConnection = New SqlConnection(connectionString)<br />
Dim sqlCommand As SqlCommand = New SqlCommand("SELECT * FROM StagingEmployer ORDER BY StagingDate", cn)<br />
cn.Open()<br />
Dim TestRowset As SqlDataReader = sqlCommand.ExecuteReader()<br />
Into a dataset. Then you can close the connection to the database... and work with the sorted data in the dataset???
I need help on this matter please.
Thank you in advance
"Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
|
|
|
|
|
Okay, you wont beleive it but i figured it out... Now can someone please show me or point me to a link on how to read from a Dataset Once it has the records from the database in it???
"Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
|
|
|
|
|
NEVER MIND...
I got it all figured out
"Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
|
|
|
|
|
Support123 wrote: I got it all figured out
Great, but it would be really great if you could post your findings back here too... it will help others looking for something similar and also puts you in the position of giving something back to the community
|
|
|
|
|
Sorry...
Here is what i have and it is working fine now
<br />
Dim DS As System.Data.DataSet<br />
Dim MyCommand As System.Data.OleDb.OleDbDataAdapter<br />
Dim MyConnection As System.Data.OleDb.OleDbConnection<br />
Dim connectionString As String = "Initial Catalog=DBName;Data Source=ServerName;Integrated Security=SSPI;"<br />
Dim cn As SqlConnection = New SqlConnection(connectionString)<br />
Dim strSqlCommand As SqlCommand = Nothing<br />
<br />
' Set Up the Connection String.<br />
MyConnection = New System.Data.OleDb.OleDbConnection( _<br />
"provider=SQLOLEDB; database=DBName; " & _<br />
"Integrated Security=SSPI; server=ServerName;")<br />
<br />
' Set Up the Command to be executed.<br />
MyCommand = New System.Data.OleDb.OleDbDataAdapter( _<br />
"SELECT * FROM StagingGeneric ORDER BY StagingDate", MyConnection)<br />
<br />
' Fill the DataSet with the returned Values from the Database.<br />
DS = New System.Data.DataSet()<br />
MyCommand.Fill(DS, "StagingGeneric")<br />
<br />
' Close The Connection To The DataBase.<br />
MyConnection.Close()<br />
<br />
' Get Data Table<br />
Dim dt As DataTable = DS.Tables("StagingGeneric")<br />
<br />
' For Each Row In The Data Row Check What Action To Follow<br />
For Each row As DataRow In dt.Rows<br />
'MsgBox(Convert.ToString(row(3)))<br />
If Convert.ToString(row(3)) = "U" Then<br />
Dim LastContribDate As String = "''"<br />
If (Len(Convert.ToString(row(1))) > 9) Then<br />
LastContribDate = Left(Convert.ToString(row(1)), 10)<br />
End If<br />
Dim StageDate As String = "''"<br />
If (Len(Convert.ToString(row(4))) > 9) Then<br />
StageDate = Left(Convert.ToString(row(4)), 10)<br />
End If<br />
cn.Open()<br />
strSqlCommand = New SqlCommand("UPDATE stagingGenericTest SET" _<br />
+ " [Member_Number] = '" + Convert.ToString(row(0)) _<br />
+ "', [LastContributionDate] = " + LastContribDate _<br />
+ ", [PaymentStatus] = '" + Convert.ToString(row(2)) _<br />
+ "', [Action] = '" + Convert.ToString(row(3)) _<br />
+ "', [StagingDate] = " + StageDate _<br />
+ " WHERE [Member_Number] = '" + Convert.ToString(row(0)) + "'", cn)<br />
strSqlCommand.ExecuteNonQuery()<br />
cn.Close()<br />
ElseIf Convert.ToString(row(3)) = "I" Then<br />
Dim LastContribDate As String = "''"<br />
If (Len(Convert.ToString(row(1))) > 9) Then<br />
LastContribDate = Left(Convert.ToString(row(1)), 10)<br />
End If<br />
Dim StageDate As String = "''"<br />
If (Len(Convert.ToString(row(4))) > 9) Then<br />
StageDate = Left(Convert.ToString(row(4)), 10)<br />
End If<br />
cn.Open()<br />
strSqlCommand = New SqlCommand("INSERT INTO stagingGenericTest" _<br />
+ " ([Member_Number] " _<br />
+ ", [LastContributionDate] " _<br />
+ ", [PaymentStatus] " _<br />
+ ", [Action] " _<br />
+ ", [StagingDate])" _<br />
+ " VALUES ('" + Convert.ToString(row(0)) _<br />
+ "', " + LastContribDate _<br />
+ ", '" + Convert.ToString(row(2)) _<br />
+ "', '" + Convert.ToString(row(3)) _<br />
+ "', " + StageDate _<br />
+ ")", cn)<br />
strSqlCommand.ExecuteNonQuery()<br />
cn.Close()<br />
ElseIf Convert.ToString(row(3)) = "D" Then<br />
cn.Open()<br />
strSqlCommand = New SqlCommand("DELETE FROM stagingGenericTest" _<br />
+ " WHERE [Member_Number] = '" + Convert.ToString(row(0)) + "'", cn)<br />
strSqlCommand.ExecuteNonQuery()<br />
cn.Close()<br />
End If<br />
Next<br />
Not bad for a days work
"Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
|
|
|
|
|
So now i can just change the connectionString to point to another server (i just used the same server to test on.)
"Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
|
|
|
|
|
May I STRONGLY suggest you read up on parameterized queries. Using string concantentation to build your UPDATE query is a severe security risk and makes for some very unstable code.
Read this...[^]
|
|
|
|
|
i am going to run this vb.net code in a ssis package (script task)... i will be running this package in a scheduled job on SQL... so i dont think the user will be able to add bits to the query... this is just to keep our db up to date with their live database. The user wont see the results of these queries as it does not return any values to them... i only updates our database if the staging tables (that are populated by means of triggers) have any values available.
(if i am wrong please correct me)
Thank you anyway
"Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
|
|
|
|
|
Support123 wrote: i am going to run this vb.net code in a ssis package (script task)...
Even more of a reason to secure the code.
Support123 wrote: so i dont think the user will be able to add bits to the query...
All it takes is a screwed piece of data, that you're assuming is good, to screw up your database. You do know what "assume" really means, don't you?
Support123 wrote: The user wont see the results of these queries as it does not return any values to them...
Well, the database returns data to someone at some time in the future. The users will eventually see the results.
|
|
|
|
|
Support123 wrote: Not bad for a days work
Yeah... but you don't want your DB hacked, do you?
Read up on SQL Injection Attacks...
|
|
|
|
|
Hi all,
Please help me on the migration task from vb to that of vb.net
for that code is written in vb 6.0 and i want to it in vb.net .
visual studion 2005 environment.
Thanks
|
|
|
|
|
That's a very generic question covering an awful lot of ground. To start with, just open the VB6 project in Visual Studio 2005. Do not double click the projects .VBP file. You have to open Open the Project with VS2005 already started. This will automatically launch the upgrade wizard which will TRY AND DO ITS BEST to convert the code to VB.NET. Since there are lots of changes between VB6 and VB.NET code, chances are you WILL have errors and need to correct the code by hand to get it working.
|
|
|
|
|
Hi,
Please help me...
I am struggling to run more then one query on sql server 2005... i get errors and i dont really know how to get going.
Here is what i have tried now:
<br />
Dim mySQLConnection As New SqlConnection( _<br />
"Initial Catalog=DBName;Data Source=InstanceName;Integrated Security=SSPI;")<br />
mySQLConnection.Open()<br />
<br />
Dim mySQLTransaction As SqlTransaction = _<br />
mySQLConnection.BeginTransaction()<br />
<br />
Dim mySQLCommand As SqlCommand = _<br />
mySQLConnection.CreateCommand()<br />
<br />
mySQLCommand.CommandText = "SELECT * FROM StagingEmployer ORDER BY StagingDate"<br />
<br />
mySQLTransaction.Commit()<br />
<br />
mySQLConnection.Close()<br />
Now... how do i get the Select * from StagingEmployer Order by StagingDate return values into a rowset that i can use???
Once there... how do i run a second query like for instance:
<br />
Update StagingEmployer<br />
Set EndDate = GetDate()<br />
Where StagingDate < '2007/01/01'<br />
Sorry... i have tried it but not like this... i can post my previouse attempt but i will do this in 2nd post... it is a bit large.
"Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
|
|
|
|
|
See: here i tried to run second query but no success with this:
<br />
Dim connectionString As String = "Initial Catalog=DBName;Data Source=InstanceName;Integrated Security=SSPI;"<br />
Dim cn As SqlConnection = New SqlConnection(connectionString)<br />
Dim sqlCommand As SqlCommand = New SqlCommand("SELECT * FROM StagingEmployer ORDER BY StagingDate", cn)<br />
cn.Open()<br />
Dim TestRowset As SqlDataReader = sqlCommand.ExecuteReader()<br />
<br />
While TestRowset.Read()<br />
If (Convert.ToString(TestRowset(35)) = "U") Then<br />
Dim sqlCommand2 As SqlCommand = New SqlCommand("UPDATE stagingEmployerTest SET" _<br />
+ " [Employer_Number] = '" + Convert.ToString(TestRowset(0)) _<br />
+ "', [Name] = '" + Convert.ToString(TestRowset(1)) _<br />
+ "', [Trading_Name] = '" + Convert.ToString(TestRowset(2)) _<br />
+ "', [Business_Number] = '" + Convert.ToString(TestRowset(3)) _<br />
+ "', [Company_Number] = '" + Convert.ToString(TestRowset(4)) _<br />
+ "', [Tax_File_Number] = '" + Convert.ToString(TestRowset(5)) _<br />
+ "', [Street_Address_1] = '" + Convert.ToString(TestRowset(6)) _<br />
+ "', [Street_Address_2] = '" + Convert.ToString(TestRowset(7)) _<br />
+ "', [Street_Address_3] = '" + Convert.ToString(TestRowset(8)) _<br />
+ "', [Street_Suburb] = '" + Convert.ToString(TestRowset(9)) _<br />
+ "', [Street_State] = '" + Convert.ToString(TestRowset(10)) _<br />
+ "', [Street_Postcode] = '" + Convert.ToString(TestRowset(11)) _<br />
+ "', [Street_Country] = '" + Convert.ToString(TestRowset(12)) _<br />
+ "', [Postal_Address_1] = '" + Convert.ToString(TestRowset(13)) _<br />
+ "', [Postal_Address_2] = '" + Convert.ToString(TestRowset(14)) _<br />
+ "', [Postal_Address_3] = '" + Convert.ToString(TestRowset(15)) _<br />
+ "', [Postal_Suburb] = '" + Convert.ToString(TestRowset(16)) _<br />
+ "', [Postal_State] = '" + Convert.ToString(TestRowset(17)) _<br />
+ "', [Postal_Postcode] = '" + Convert.ToString(TestRowset(18)) _<br />
+ "', [Postal_Country] = '" + Convert.ToString(TestRowset(19)) _<br />
+ "', [Contact_Name] = '" + Convert.ToString(TestRowset(20)) _<br />
+ "', [Contact_Title] = '" + Convert.ToString(TestRowset(21)) _<br />
+ "', [Contact_Salutation] = '" + Convert.ToString(TestRowset(22)) _<br />
+ "', [Phone] = '" + Convert.ToString(TestRowset(23)) _<br />
+ "', [Fax] = '" + Convert.ToString(TestRowset(24)) _<br />
+ "', [Email] = '" + Convert.ToString(TestRowset(25)) _<br />
+ "', [Broker] = '" + Convert.ToString(TestRowset(26)) _<br />
+ "', [Underwriter] = '" + Convert.ToString(TestRowset(27)) _<br />
+ "', [Diplomatic_Indemnity] = '" + Convert.ToString(TestRowset(28)) _<br />
+ "', [Status] = '" + Convert.ToString(TestRowset(29)) _<br />
+ "', [Image] = '" + Convert.ToString(TestRowset(30)) _<br />
+ "', [Created] = '" + Convert.ToString(TestRowset(31)) _<br />
+ "', [Created_By] = '" + Convert.ToString(TestRowset(32)) _<br />
+ "', [Modified] = '" + Convert.ToString(TestRowset(33)) _<br />
+ "', [Modified_By] = '" + Convert.ToString(TestRowset(34)) _<br />
+ "', [Action] = '" + Convert.ToString(TestRowset(35)) _<br />
+ "', [StagingDate] = '" + Convert.ToString(TestRowset(36)) + "'" _<br />
+ " WHERE [Employer_Number] = '" + Convert.ToString(TestRowset(0)) + "'", cn)<br />
sqlCommand2.ExecuteScalar()<br />
End If<br />
MsgBox("Result :" + Convert.ToString(TestRowset(35)))<br />
End While<br />
In this example the first Query is the Select * from StagingEmployer Order By StagingDate (this one works fine) the second Query is the Update... (not working) i posted this section before but without the ,cn in the 2nd query. Sorry.
This doesnt work???
I thought it should and i am not sure why and how to get this going? Sorry for the big post.
"Many of life's failures are people who did not realize how close they were to success when they gave up." Thomas A. Edison
|
|
|
|
|