I'm writing in ASP.NET 4 / VB.NET. I am querying an MSSQL database and sometimes have records come back with no results...so I enclosed the call I was making upon the results in an If..Else clause to set a default value if the database comes back with no results...but now I am getting this "Object variable or With block variable not set error". Here is the relevant code:
Dim clcfirst
Dim rhcfirst
Dim clcdate As Date
Dim rhcdate As Date
If IsNothing(clcexists) Then
clcfirst = Date.Now.Subtract(year)
rhcfirst = Date.Now.Subtract(year)
clcdate = clcfirst
rhcdate = rhcfirst
Else
clcfirst = clcexists.FirstOrDefault()
rhcfirst = rhcexists.FirstOrDefault()
clcdate = clcfirst.SignatureDate
rhcdate = rhcfirst.SignatureDate
End If
Where is the DateTime year variable being set? Could that be null?
If you want to subtract a year, you could just do:
clcdate = Date.Now.AddYears(-1)
rhcdate = Date.Now.AddYears(-1)
Related
I'm trying to print a list of dates using a for loop. I'm getting an error saying 'AddDays' is not a member of 'System.Array'.
Dim payDates(10) as Date
For index As Integer = 1 to 10
Redim Preserve payDates(index)
payDates(index) = payDates.AddDays(1)
index +=1
NEXT
Response.Write(payDates)
I'm no vb expert, but you need to access the index of the array, not the array itself:
Dim payDates(10) as Date
For index As Integer = 1 to 10
Redim Preserve payDates(index)
payDates(index) = payDates(index).AddDays(1)
index +=1
NEXT
Response.Write(payDates)
Also you migzht want to write every date. In this case change you code to this:
Dim payDates(10) as Date
For index As Integer = 1 to 10
Redim Preserve payDates(index)
payDates(index) = payDates(index).AddDays(1)
Response.Write(payDates(index))
index +=1
NEXT
In essence:
payDates is of type System.Array
payDates(index) contains a DateTime variable at the position index, where index is of type int
Well, AddDays isn't a member of System.Array. But you're trying to call it anyway:
payDates.AddDays(1)
If you're trying to get a modified date, reference the date element of the array. Something like this:
payDates(index).AddDays(1)
(Note that if you're looking to display this information on a web page, it's a lot better to set the information on elements of that page than to use Response.Write(). You have very little control over where Response.Write() emits its output in the resulting page.)
I have a gridview that has three bound fields. "Date", which is the day a support ticket was added; "Date Fixed", which is the day a support ticket was resolved; and "Days Waited", which should be the number of days a ticket has gone unresolved.
Each row has a different date and could or could not have a dateFixed. I can use the RowDataBound event to calculate daysWaited from date and dateFixed, but I do not know how to then set daysWaited to the calculated value for each row.
I am using VB.NET and the .NET 4.0 framework.
Not sure how you are loading data, hopefully setting the datasource of the gridview. After you set the datasource, you can call this method. It will go through and compare dates on each row, it also checks if anything is there. This worked good during my testing. Hopefully you find this good for your use.
Private Sub CalcDays()
For i As Integer = 0 To GridView1.Rows.Count - 1
Dim stDate As Date
Dim endDate As Date
Dim daysPassed As Integer = -1
If Not (GridView1.Rows(i).Cells(1).Text = " ") Then
stDate = DateTime.Parse(GridView1.Rows(i).Cells(0).Text)
endDate = DateTime.Parse(GridView1.Rows(i).Cells(1).Text)
While (stDate <= endDate)
stDate = stDate.AddDays(1)
daysPassed += 1
End While
GridView1.Rows(i).Cells(2).Text = daysPassed.ToString
Else
GridView1.Rows(i).Cells(2).Text = "NA"
End If
Next
End Sub
Here's a screenshot of my test...
I have one data table in VB page which contain bulk data.In that data table one column named as vType and values in that column is one of Pr defined values such as 'A','B','C','D' etc , which comes from one Datable.
Now I want count of each type at the end.
For ex : CountA = 20,CountB=25 and so on .
Till now I have compared Each value using If condition which is static
For each dr as dataRow in dsType.rows
If dr("vType") = 'A' Then
CountA += 1
ElseIf dr("vType") = 'B' Then
CountB +=1
Next dr
and this If condition will repeat depend upon no of types in that data table (at max 8 fix values) I want to do this in single if condition ( Dynamic if Possible) Can I Count these values and store the same into single varaible? appreciate for you prompt reply.
You can use Linq-To-DataSet and Enumerable.GroupBy + Enumerable.Count on each group:
Dim typeGroups = dsType.AsEnumerable().
GroupBy(Function(row) row.Field(Of String)("vType")).
Select(Function(g) New With{ .Type = g.Key, .Count = g.Count(), .TypeGroup = g })
Note that New With creates an anonymous type in VB.NET with custom properties. So like a class on-the-fly which you can use in the current method.
Now you can enumerate the query with For Each:
For Each typeGroup In typeGroups
Console.WriteLine("Type:{0} Count:{1}", typeGroup.Type, typeGroup.Count)
Next
I cannot use Linq, i need to use simple vb only
Then use a Dictionary:
Dim typeCounts = New Dictionary(Of String, Integer)
For Each row As DataRow In dsType.Rows
Dim type = row.Field(Of String)("vType")
If (typeCounts.ContainsKey(type)) Then
typeCounts(type) += 1
Else
typeCounts.Add(type, 1)
End If
Next
Now you have a dictionary where the key is the type and the value is the count of the rows with this type.
why not getting the pretend result from the db itself?
Like so:
select count(*), vType
from someTable
group by vType
Not so sure about your question .. but this is what I've considered ..
You can make it as Sub ..
Sub AssignIncr(ByVal ds as DataSet,byval sFi as String,byval sCrit as String,ByRef Counter as Integer)
For each dr as dataRow in ds.rows
If dr(sFi) = sCrit Then Counter += 1
Next dr
End Sub
So you may use it by ..
AssignIncr(dsType,"vType","A",CountA)
I have 2 columns, one for start-time and the other for the end-time. They both are of the type nvarchar so that i can compare them.
I have a text box that will receive time from the user and will post-back automatically to check if the the time is valid or not.
Dim compared_time_1 As DateTime
Dim compared_time_2 As DateTime
Dim select_time As SqlCommand
select_time = New SqlCommand("select Start_Time , End_Time from Clinic_Schedule where Schedule_no = #sch_no", appt_DB_1)
select_time.Parameters.AddWithValue("#sch_no", Sch_no)
Dim time_rdr As SqlDataReader
time_rdr = select_time.ExecuteReader()
While time_rdr.Read
compared_time_1 = DateTime.Parse(start_time_1)
compared_time_2 = DateTime.Parse(end_time_1)
start_time_1 = time_rdr(0).ToString
end_time_1 = time_rdr(1).ToString
If appt_time_txt0.Text >= start_time_1 And appt_time_txt0.Text <= end_time_1 Then
date_valid_lbl0.Visible = True
date_valid_lbl0.Text = "*Valid Time"
Else
time_valid_lbl0.Visible = True
time_valid_lbl0.Text = "*Not Valid Time"
End If
End While
time_rdr.Close()
I don't know if i have a problem with my logic XD.
the data filled in thoses columns are in this format : 00:00AM or 00:00PM.
I will appreciate your help .. thanks
Seems like you are comparing a string against a date datatype.
Ensure that you are converting both into date datatype as follows.
Note the space between the seconds and 'AM'.
time1=CDate("3:19:40 AM")
time2=CDate("3:10:40 AM")
Then perform comparisms as follows:
if time1>time2 then
'logic
end if
It looks like you're doing the parse before loading the data from your reader.
compared_time_1 = DateTime.Parse(start_time_1)
compared_time_2 = DateTime.Parse(end_time_1)
start_time_1 = time_rdr(0).ToString
end_time_1 = time_rdr(1).ToString
should be
start_time_1 = time_rdr(0).ToString
end_time_1 = time_rdr(1).ToString
compared_time_1 = DateTime.Parse(start_time_1)
compared_time_2 = DateTime.Parse(end_time_1)
But I would even do it differently than that. If you're trying to tell if the times are valid, you can use the datetime TryParse method. Perhaps a bit of refactoring might also help you. Finally, just be aware that comparing times might be problematic if there is ever a chance that schedule item might begin before midnight and end after midnight the next day.
Sub ReadingData()
'initializing reader stuff here...
Dim dtStart As DateTime, dtEnd As DateTime
If DateTime.TryParse(time_rdr(0).ToString, dtStart) = False Then
HandleInvalidTime()
End If
If DateTime.TryParse(time_rdr(1).ToString, dtEnd) = False Then
HandleInvalidTime()
End If
'Closing out reader stuff here...
HandleValidTime(dtStart, dtEnd)
End Sub
Sub HandleValidTime(TheStartTime As DateTime, TheEndTime As DateTime)
'Do Stuff
End Sub
Sub HandleInvalidTime()
'Do Stuff
End Sub
How do I subtract a month from a date object in VB.NET?
I have tried:
Today.AddMonths(-1)
However, given that Today is 01-Jan-2010, the result I get is 01-Dec-2010. The answer I want is 01-Dec-2009.
Is there a convenient way of doing this within the .NET framework?
You actually have to transport Today into a variable and let that assignment work there. The following code will produce the result you expect (I just verified it because your post made me think twice).
Dim dt As DateTime = Date.Today
dt = dt.AddMonths(-2)
Dim x As String = dt.ToString()
This works fine, you need to remember that the DateTime is imutable.
Dim d As DateTime
d = New DateTime(2010, 1, 1)
d = d.AddMonths(-1)
Have a look at DateTime Structure
A calculation on an instance of
DateTime, such as Add or Subtract,
does not modify the value of the
instance. Instead, the calculation
returns a new instance of DateTime
whose value is the result of the
calculation.
Dim d As DateTime = #1/1/2010#
d = d.AddMonths(-1)
I have used the following and it works.
Dim dtToday As DateTime = Date.Today
dtToday = dtToday.AddMonths(-2)