Datediff not returning expected value - asp.net

this code always return '0' i don't know why
Dim cur_month = DatePart(DateInterval.Month, Now).ToString()
Dim cur_date As String = "01/" + cur_month + "/" + (DatePart(DateInterval.Year, Now).ToString)
Dim sel_date As String = "01/" + (cmb_mnth.SelectedIndex + 1).ToString + "/" + txt_year.Text.ToString
Dim date_dif As String = DateDiff(DateInterval.Month, CDate(sel_date), CDate(cur_date))
MsgBox(date_dif)
cnb_mnth is ComboBox for Month
txt_year is Text Box For Entering Year

I'd guess that the regional date settings on your computer has month before day (i.e., MM/dd/yyyy), so it interprets 01 as the month for both cur_date and sel_date.

Related

my code is as shown and i get this error "The remaining text does not appear to be part of the formula">

Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
reportdoc1.Load(Server.MapPath("~\RepVoucherchq.rpt"))
CrystalReportViewer1.ReportSource = reportdoc1
ConnectionInfo()
Dim user_brno As String = CType(Session("userBrn"), String)
Dim date_from As String = CType(Session("date_from"), String)
Dim date_to As String = CType(Session("date_to"), String)
reportdoc1.RecordSelectionFormula = "{VEWVoucherchq.vocdate} between '" + date_from + "' and '" + date_to + "' and {VEWVoucherchq.chqbrNo} = " + user_brno
CrystalReportViewer1.RefreshReport()
End Sub
I don't have Visual Studio on this computer to test this completely, but I think your syntax is off in your Record Selection Formula.
Try this instead:
reportdoc1.RecordSelectionFormula = "{VEWVoucherchq.vocdate} in '" + date_from + "' to '" + date_to + "' and {VEWVoucherchq.chqbrNo} = " + user_brno
The between/and syntax you used isn't supported in Crystal Reports for evaluating a date the way you want. The supported syntax is {eval_date} IN {start_Date} to {end_Date}.
You may also run into an issue with your date_from and date_to fields being string data types instead of the Date or DateTime datatype. If that is an issue, you will want to pass the values through a function to convert them to dates properly.
Something like this should suffice:
reportdoc1.RecordSelectionFormula = "{VEWVoucherchq.vocdate} in Date('" + date_from + "') to Date('" + date_to + "') and {VEWVoucherchq.chqbrNo} = " + user_brno
If you need DateTime instead of Date data types, replace the Date() functions with DateTime() functions.

How to Select Date in SQLite

I have a problem with SQlite for DateTime in UWP-app.
Assume the SQLite DB has the following data:
PurchaseDate (Date in SQLite format)
-----------------------------------
2016-09-10 11:10:10
2016-09-10 11:10:15
2016-09-10 11:10:30
Pass in this Date:
strSQLiteDate ="2016-09-10"
I just want to check if there is any row in the tblPurchase.
There is no match from below SQL-Select. What did I miss? Did I miss the hh:mm:ss part? But I just need to check the yyyy-mm-dd part.
using (var db = new SQLite.Net.SQLiteConnection(new SQLite.Net.Platform.WinRT.SQLitePlatformWinRT(), DBPath))
{
var query = db.Query<tblPurchase>("Select * From tblPurchase where PurchaseDate = " + " date('" + strSQliteDate + "')");
intcount = query.Count;
if (intcount != 0)
{
return intcount;
}
}
Edit 1
10/8/2016 10:13:26 AM
The above date will be recreated as DateTime and SQLit.Net-PCL use it to insert into SQLite DB
string[] strAr_Date = strDate.Split('/');
string strYear = strAr_Date[0].ToString();
string strMth = strAr_Date[1].ToString();
string strDay = strAr_Date[2].ToString();
string strDateTime = strDay + "/" + strMth + "/" + strYear + " " + strTime;
DateTime dt = DateTime.Parse(strDateTime);
... where PurchaseDate = date('2016-09-10')
The date() function removes the time portion from a date/time value.
But the value 2016-09-10 does not have a time portion, so it is not changed.
The PurchaseDate values still have the time portion, so you end up with a comparison like this:
... where '2016-09-10 11:10:10' = '2016-09-10'
You have to remove the time from the PurchaseDate values:
... where date(PurchaseDate) = '2016-09-10'

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

While input of date - literal does not match format string

I have this line of code in asp.net through which inserting date into a table
CMPI_EFF_DATE = cc.GetDataSet("SELECT TRM_EFF_STDT as TRM_EFF_STDT FROM TRM_MST WHERE TRM_CODE = " + ddlTrm.SelectedValue + "").Tables[0].Rows[0]["TRM_EFF_STDT"].ToString(),
and i am using oracle database . but while inserting data into it it show's an error
literal does not match format string
Is ddlTrm.SelectedValue string value? if it's true, I thing you should put value in quotes like this
"'" + ddlTrm.SelectedValue+"'"
Full example:
CMPI_EFF_DATE = cc.GetDataSet("SELECT TRM_EFF_STDT as TRM_EFF_STDT FROM TRM_MST WHERE TRM_CODE = '" + ddlTrm.SelectedValue+"'").Tables[0].Rows[0]["TRM_EFF_STDT"].ToString()
I think you are wrong here ddlTrm.SelectedValue + "")
It should be ddlTrm.SelectedValue )
CMPI_EFF_DATE = cc.GetDataSet("SELECT TRM_EFF_STDT as TRM_EFF_STDT FROM TRM_MST WHERE TRM_CODE = " + ddlTrm.SelectedValue).Tables[0].Rows[0]["TRM_EFF_STDT"].ToString(),

how to find the max id from a generic list

i m getting a collection of data in list's object. And from this list i want to get the maximum id.`
Dim objinfo As List(Of AlbumInfo) = objPhotos.GetPhotos_Alb_ID(Me.ModuleId, hdd_AlbID.Value)
Dim Photo_Image As String = ""
Dim str As String = Photo_Image & fu_Photo.PostedFile.FileName.Substring(fu_Photo.PostedFile.FileName.LastIndexOf("."))
If objinfo.Count >= 1 Then
Photo_Image = Convert.ToString(hdd_AlbID.Value) + "_" + Convert.ToString(objinfo.Item("0").Photo_Id + 1)
Else
Photo_Image = Convert.ToString(hdd_AlbID.Value) + "_" + Convert.ToString("1")
End If
this returns the "0"th positions id from Convert.ToString(objinfo.Item("0").Photo_Id + 1)
but i want to get the last item's id.
Well, you specify max in the title, and last in the question, so here's both:
Max:
Photo_Image = hdd_AlbID.Value.ToString() & "_" & _
objinfo.Max(Function(o) o.Photo_Id) _
.Photo_Id.ToString()
Last:
Photo_Image = hdd_AlbID.Value.ToString() & "_" & _
objinfo.Last().Photo_Id.ToString()
Use Max extension method. For example,
...
maxPhotoId = objInfo.Max(Function(photo) photo.Photo_Id).Photo_Id
...

Resources