Convert vb.net to asp Classic - asp-classic

I have the following VB Code:
Dim value As Date = Date.Now
Dim intHour As Integer
Dim intMinute As Integer
Dim intSecond As Integer
intHour = 17
intMinute = 0
intSecond = 0
Dim newdatetime As DateTime = New Date(value.Year, value.Month, value.Day, intHour, intMinute, intSecond)
If DateTime.Now < newdatetime Then
If value.DayOfWeek = DayOfWeek.Saturday Then
value = value.AddDays(2)
Return value
End If
If value.DayOfWeek = DayOfWeek.Sunday Then
value = value.AddDays(1)
Return value
End If
Return value
ElseIf DateTime.Now > newdatetime Then
Do
value = value.AddDays(1)
Loop While (value.DayOfWeek = DayOfWeek.Saturday) Or (value.DayOfWeek = DayOfWeek.Sunday)
Return value
End If
How would that be in ASP CLassic?
Can someone help ?
EDIT:
Dim CurrentDate
CurrentDate = Date()
Dim intHour
Dim intMinute
Dim intSecond
intHour = 17
intMinute = 0
intSecond = 0
Dim NewDate
Dim NewDate1
Dim NewDate2
NewDate = DatePart("yyyy", CurrentDate)
NewDate1 = DatePart("m", CurrentDate)
NewDate2 = DatePart("d", CurrentDate)
Dim Dates
Dates = DateSerial(NewDate, NewDate1, NewDate2)
Dim Time
Time = TimeSerial(intHour, intMonth, intSecond)

Year: http://msdn.microsoft.com/en-us/library/fhzx965c%28v=vs.85%29.aspx
Month: http://msdn.microsoft.com/en-us/library/0eeeket2%28v=vs.85%29.aspx
Day of month: http://msdn.microsoft.com/en-us/library/yyhfe92k%28v=vs.85%29.aspx
Date comparison: http://msdn.microsoft.com/en-us/library/xhtyw595%28v=vs.85%29.aspx
Day of week: http://msdn.microsoft.com/en-us/library/t51x9wtx%28v=vs.85%29.aspx
Incrementing date: http://msdn.microsoft.com/en-us/library/cb7z8yf9%28v=vs.85%29.aspx
http://msdn.microsoft.com/en-us/library/d1wf56tt%28v=VS.85%29.aspx is a fantastic reference for VBScript/ASP classic.

Related

IDataReader - Get value of SQL time(7) field to use in calculations

Using IDataReader, I need to get time values so that I can get the timespan. In my SQL database, these values are time(7). I am getting an error - InvalidCast Exception was unhandled by user code. Here is my code (and I know I have to rewrite the Select statement):
commandvct.CommandText = "Select begindateoff, enddateoff, begintimeoff, endtimeoff, allday_yesno from tblworkhours where Employee = " & rve & " and workcode = 2"
commandvct.CommandType = CommandType.Text
commandvct.Connection = sqlConnection
sqlConnection.Open()
Using reader As IDataReader = commandvct.ExecuteReader
While reader.Read()
Dim begdate As Date = reader.GetDateTime(0)
Dim enddate As Date = reader.GetDateTime(1)
If begdate = enddate Then
Dim allday As Boolean = reader.GetBoolean(4)
'If all day, add 8 hours
If (allday = True) Then
workinghoursv = workinghoursv + 8 'Change me to your needs.
TextBoxva2.Text = workinghoursv
'If not all day, add difference between times
Else
'tried defining it as datetime and as string - neither work
Dim begtime As String = reader.GetDateTime(2)
Dim endtime As DateTime = reader.GetDateTime(3)
Dim diffv As TimeSpan = (endtime).Subtract(begtime)
Dim diff2v As Decimal = (Convert.ToDecimal(diffv.TotalMinutes)) / 60
workinghoursv = workinghoursv + diff2v
TextBoxva2.Text = workinghoursv
End If

How can i minus two Dates asp.net

How can i minus two Dates (LblExpirydate.Text - Label3.Text )
LblExpirydate.Text = String.Format("{0:dd/MM/yyyy}", dataReader(0))
Label3.Text = System.DateTime.Now.ToString(("dd/MM/yyyy"))
LblExpirydate.Text = 01/05/2013
Label3.Text = 01/04/2011
You can subtract one date from another to get a TimeSpan. You should not try to do date calculations on strings: your program is likely to fall foul of an assumed date format somewhere.
Dim dateFormat As String = "dd/MM/yyyy"
Dim rightNow As DateTime = DateTime.Now
Dim expiryDate As DateTime = rdr.GetDateTime(0)
Dim daysToExpiry As Integer = (expiryDate - rightNow).Days
LblExpirydate.Text = expiryDate.ToString(dateFormat)
Label3.Text = expiryDate.ToString(dateFormat)
LabelExpires.Text = daysToExpiry.ToString & " days"

Specific Date and Time in ASP Classic

Hi i have the following Code :
Dim CurrentDate
CurrentDate = Date()
Dim intHour
Dim intMinute
Dim intSecond
intHour = 17
intMinute = 0
intSecond = 0
Dim NewDate
Dim NewDate1
Dim NewDate2
NewDate = DatePart("yyyy", CurrentDate)
NewDate1 = DatePart("m", CurrentDate)
NewDate2 = DatePart("d", CurrentDate)
Dim Dates
Dates = DateSerial(NewDate, NewDate1, NewDate2)
Dim Time
Time = TimeSerial(intHour, intMonth, intSecond)
I have done something equal in VB:
Dim value As Date = Date.Now
Dim intHour As Integer
Dim intMinute As Integer
Dim intSecond As Integer
intHour = 17
intMinute = 0
intSecond = 0
Dim newdatetime As DateTime = New Date(value.Year, value.Month, value.Day, intHour, intMinute, intSecond)
In VB i can do
Dim newdatetime As DateTime = New Date(value.Year, value.Month, value.Day, intHour, intMinute, intSecond).
In my ASP Code i have Dates = DateSerial(NewDate, NewDate1, NewDate2) and Time = TimeSerial(intHour, intMonth, intSecond). How can i put them together as DateTime like in VB?
Two ways:
dim h,n,s
h = 17
n = 1
s = 2
dim t
t = timeserial(h,n,s)
dim d
d = date()
dim ts1
ts1 = dateadd("h",h, _
dateadd("n",n, _
dateadd("s",s, d )))
dim ts2
ts2 = d + T
Both produce the same output. The one with the additions has some gotchas depending on how close to day 0 you play it. I think the first way is "saner". As far as I recall VBScript just stored the date part in the integer or a float and the time part in the fraction part of a float (as parts of a 24 hour day, so 12:00 is 0.5), hence you can just add them together with +

Loop not incrementing ASP.NET VB

'Add items to db'
Function recordOrder()
Dim objDT As System.Data.DataTable
Dim objDR As System.Data.DataRow
objDT = Session("Cart")
Dim intCounter As Integer
Dim con2 As New System.Data.OleDb.OleDbConnection
Dim myPath2 As String
myPath2 = Server.MapPath("faraxday.mdb")
con2.ConnectionString = "Provider=Microsoft.Jet.OLEDB.4.0; Data source=" & myPath2 & ";"
Dim myCommand2 As New System.Data.OleDb.OleDbCommand
Dim sql As String
myCommand2.Connection = con2
con2.Open()
'variables'
Dim order_date As String
Dim coupon_ID As String
Dim customer_id As String
Dim quantity As String
'variables'
Try
For intCounter = 0 To objDT.Rows.Count - 1
objDR = objDT.Rows(intCounter)
order_date = System.DateTime.Now.Date
coupon_ID = objDR("ID")
quantity = objDR("quantity")
myCommand2.Parameters.Add("#order_date", SqlDbType.VarChar).Value = order_date
myCommand2.Parameters.Add("#coupon_ID", SqlDbType.VarChar).Value = coupon_ID
myCommand2.Parameters.Add("#customer_id", SqlDbType.VarChar).Value = custID
myCommand2.Parameters.Add("#quantity", SqlDbType.VarChar).Value = quantity
myCommand2.CommandText = "INSERT INTO orders(order_date, coupon_id, customer_id, quantity) VALUES ( #order_date ,#coupon_ID,#customer_id,#quantity)"
myCommand2.ExecuteNonQuery()
Next
Catch ex As Exception
Finally
If con2.State = ConnectionState.Open Then
con2.Close()
End If
End Try
End Function
The loop is not incrementing (intCounter). Please HELP...
Would you not be better to use:
For Each objDR In objDT.Rows
order_date = System.DateTime.Now.Date
coupon_ID = objDR("ID")
quantity = objDR("quantity")
myCommand2.Parameters.Add("#order_date", SqlDbType.VarChar).Value = order_date
myCommand2.Parameters.Add("#coupon_ID", SqlDbType.VarChar).Value = coupon_ID
myCommand2.Parameters.Add("#customer_id", SqlDbType.VarChar).Value = custID
myCommand2.Parameters.Add("#quantity", SqlDbType.VarChar).Value = quantity
myCommand2.CommandText = "INSERT INTO orders(order_date, coupon_id, customer_id, quantity) VALUES ( #order_date ,#coupon_ID,#customer_id,#quantity)"
myCommand2.ExecuteNonQuery()
Next
'custID' is not defined anywhere ..
anyway you should insert a break point and step in with the debugger. Then watch the data to catch which statement is causing the exception. Then modify the code accordingly to handle it.
Where you have:
For intCounter = 0 To objDT.Rows.Count - 1
Replace with:
For intCounter = 0 To objDT.Rows.Count - 1 Step 1
that will increment intCounter by 1 each loop.

bulk copy duplicating each row when it is added to database

Why is my bulk copy duplicating each row, so in my database table the row shows twice.
Label1.Visible = True
Dim tourid As New List(Of String)
tourid.Add(TextBox1.Text)
Dim tasktype As New List(Of String)
Dim tourname1 As New List(Of String)
Dim tasknamelist As New List(Of String)
Dim dboxdates As New List(Of String)
Dim dates As New List(Of String)
Dim firstdates As New List(Of String)
Dim agent As New List(Of String)
Dim desc As New List(Of String)
Dim checkitem As ListItem
Dim departuredate As Date
For Each checkitem In dboxes.Items
If checkitem.Selected Then
departuredate = checkitem.Text
dboxdates.Add(departuredate)
For Each row As GridViewRow In GridView1.Rows
' Selects the text from the TextBox
Dim checkboxstatus As CheckBox = CType(row.FindControl("tasknamebox"), CheckBox)
If checkboxstatus.Checked = True Then
tasknamelist.Add(checkboxstatus.Text)
Dim dates1 As TextBox = CType(row.FindControl("tdays"), TextBox)
Dim gracep As TextBox = CType(row.FindControl("tgrace"), TextBox)
Dim aftersubtraction As DateTime
Dim fromatafter As DateTime
aftersubtraction = departuredate.AddDays(-dates1.Text)
fromatafter = aftersubtraction.AddDays(-gracep.Text)
firstdates.Add(fromatafter.ToString("MM/dd/yyyy"))
While fromatafter.DayOfWeek = DayOfWeek.Saturday OrElse fromatafter.DayOfWeek = DayOfWeek.Sunday
fromatafter = fromatafter.AddDays(-2)
End While
dates.Add(fromatafter.ToString("MM/dd/yyyy"))
Dim txtdesc2 As TextBox = CType(row.FindControl("txtdesc"), TextBox)
desc.Add(txtdesc2.Text)
Dim tasktype1 As Label = CType(row.FindControl("tasktype"), Label)
Dim agentdlist As DropDownList = CType(row.FindControl("agentdlist"), DropDownList)
tasktype.Add(tasktype1.Text)
agent.Add(agentdlist.text)
Dim tourname As String
tourname = tname.Text
Dim sChars As String = " "
tourname1.Add(tourname.TrimEnd(sChars))
End If
Next
End If
If tasknamelist.Count > dboxdates.Count Then
Do
dboxdates.Add(checkitem.Text)
Loop Until tasknamelist.Count = dboxdates.Count
End If
If tasknamelist.Count > tourid.Count Then
Do
tourid.Add(TextBox1.Text)
Loop Until tasknamelist.Count = tourid.Count
End If
Next
table.clear()
For i As Integer = 0 To ((dates.Count) - 1)
Dim row = table.NewRow()
row("Tour") = tourid(i)
row("TourName") = tourname1(i)
row("Task") = tasknamelist(i)
row("Departure") = dboxdates(i)
row("Due Date") = dates(i)
row("Task Type") = tasktype(i)
row("Agent Name") = agent(i)
row("Completed") = "NO"
row("Description") = desc(i)
row("Orig Due") = firstdates(i)
table.Rows.Add(row)
Next
toptable.Visible = False
bottom.Visible = True
GridView2.DataSource = table
GridView2.DataBind()
Using bcp As SqlBulkCopy = New SqlBulkCopy(SqlDataSource2.ConnectionString)
bcp.ColumnMappings.Add(0, 1)
bcp.ColumnMappings.Add(1, 2)
bcp.ColumnMappings.Add(2, 3)
bcp.ColumnMappings.Add(3, 4)
bcp.ColumnMappings.Add(4, 7)
bcp.ColumnMappings.Add(5, 5)
bcp.ColumnMappings.Add(6, 10)
bcp.ColumnMappings.Add(7, 13)
bcp.ColumnMappings.Add(8, 6)
bcp.DestinationTableName = "dbo.stagingtasks"
bcp.WriteToServer(table)
End Using
Your doing a table.Rows.Add(row) and a New SqlBulkCopy()
They seem to be the same?

Resources