I have a date just one "DateOfyear" and I want to check before inserting the date in the textbox is already exists in DB but using code behind in C#.
I will give a example like what I want to do, I know is not the right code or what I need, but is just a example because I don't want peoples to past or give solution with SQLCommand/SqlServer and other thing I want something like that code.
DateTime InvoiceDateFrom = new DateTime();
DateTime InvoiceDateTo = new DateTime();
InvoiceDateFrom = Convert.ToDateTime(txtDatRiferiment.Text);
InvoiceDateTo = Convert.ToDateTime(txtDatRiferiment.Text);
if (InvoiceDateFrom == InvoiceDateTo)
{
errorMessage = vea.ErrorMessage;
}
You can use the DateTime.compareTo method:
if(InvoiceDateFrom.CompareTo(InvoiceDateTo) == 0){
errorMessage = vea.ErrorMessage;
}
More info here.
Related
So in my application I extract data from a database and then extract it to a csv file and I say what I want under each columns like so:
public List<ExtractDocument> GetExtractDocuments(List<Guid>ItemDetailIds)
{
var items = GetExtractData(ItemDetailIds);
return items.Select().Select(item => new ExtractDocument
{
ItemDetailId = item.Field<Guid>("ItemDetailID"),
ItemNumber = item.Field<string>("Number"),
ItemTitle = item.Field<string>("Title"),
ItemRevision = item.Field<string>("RevisionNumber"),
ActionType = item.Field<string>("Action"),
ActionedBy = item.Field<string>("ActionedBy"),
ActionedDate = item.Field<DateTime?>("ActionedDate")?.ToString("dd/MM/yyyy HH:mm:ss.fff"),
Comment = item.Field<string>("Comment"),
TaskType = item.Field<string>("TaskType"),
StartDate = item.Field<DateTime?>("StartDate")?.ToString("dd/MM/yyyy HH:mm:ss.fff"),
CompletedDate = item.Field<DateTime?>("CompletedDate")?.ToString("dd/MM/yyyy HH:mm:ss.fff"),
Status = item.Field<string>("Status"),
Outcome = item.Field<string>("Outcome"),
ActionerName = item.Field<string>("TaskActionedBy"),
DateActioned = item.Field<DateTime?>("ActionDate")?.ToString("dd/MM/yyyy HH:mm:ss.fff"),
ActionTaken = item.Field<string>("TaskAction"),
TaskComment = item.Field<string>("TaskComment"),
Link = item.Field<string>("URL")
}).ToList();
}
This is working but when I open up the csv file and look under the date columns such as "ActionedDate" the date is showing up in a weird way, for example one of the values should be showing as: 03/03/2022 09:07:46 but it is showing in the field as:07:46.3, why is it doing this and how I can prevent that from happening? it doesn't do this for all the fields but it does for the majority
Yeah this was a face palm moment, just realised I had the format written as: "dd/MM/yyyy HH:mm:ss.fff" when it should be "dd/MM/yyyy HH:mm:ss:fff", changing it from ss.fff to ss:fff, fixed the issue
So ive come across a slight issue with Lists in C#
I have a List that has some very similar dates. Now what i need to do is remove duplicates but where a duplicate is if its within one minute of another time, so not exactly the same.
As well as this, if there were duplicates of a specific time the object should have a flag saying so in the final list.
Now i can see how this could be done with lots of for loop, but i was hoping theres a nice way with LINQ to make this simple. So far my attempts havent been going very well though.
As an example a list of the following dates:
21/12/12 12:13:00
21/12/12 12:13:20
13/12/12 10:13:00
21/10/15 07:13:00
should turn into the list of DateDuplicate objects:
{date = 21/12/12 12:13:00, isDuplcicate=true}
{date = 13/12/12 10:13:00, isDuplcicate=false}
{date = 21/10/15 07:13:00, isDuplcicate=false}
public class DateDuplicate{
public DateTime date;
public bool isDuplicate = false;
}
List<DateDuplicate> RemoveAndMark(List<DateTime> dates){
///something nice here hopefully
}
thanks for any help!
Try this
List<DateDuplicate> RemoveAndMark(List<DateTime> dates)
{
var dateValues = dates.GroupBy(y => new { AddHours = y.Date.Date.AddHours(y.Hour), y.Minute}).Select(x =>
new DateDuplicate {isDuplicate = x.Count() > 1, date = x.Key.AddHours.AddMinutes(x.Key.Minute)}
);
return dateValues;
}
i got problem with my LINQ to format date. Here is code
var resultCustomer = from row in formDg.dtCustomer.AsEnumerable()
where row.Field<int>("customerID") == customerID2
select new
{
name = row["customerName"].ToString(),
ic = row["customerIC"].ToString(),
add1 = row["customerAdd1"].ToString(),
add2 = row["customerAdd2"].ToString(),
tel = row["customerTel"].ToString(),
tel2 = row["customerTel2"].ToString(),
tel3 = row["customerTel3"].ToString(),
email = row["customerEmail"].ToString(),
dateRegister = row["customerDateRegister"].ToString(),
customerRef = row["customerRef"].ToString(),
customerGroup = row["groupCustName"].ToString(),
panelName = row["panelName"].ToString(),
};
var firstRecord = resultCustomer.First();// My record return single row only
My question is how i can format custom date like "dd/MM/yyyy" in customerDateRegister?, Because I got problem when my textbox show 16-Nov-12 12:00:00 AM. I don`t want to display time but just date only.
I have try like this tbDateRegOv.Text = firstRecord.dateRegister.ToString("dd/MM/yyyy");. not work. Then i try dateRegister = row["customerDateRegister"].ToString("dd/MM/yyyy"), in LINQ. Same problem.
So how can I solved this problem? Thanks for help. :)
You need to cast your row as a DateTime first. This will allow you to use the custom format strings for dates.
dateRegister = ((DateTime)row["customerDateRegister"]).ToString("dd/MM/yyyy")
If customerDateRegister is of type DateTime you should make it a DateTime in the first place. Therefor use the DataRow extension method Field<T>:
dateRegister = row.Field<DateTime>("customerDateRegister")
You could try this , replace your dateRegister line with this.
dateRegister = ((DateTime)row["customerDateRegister"]).ToString("dd/MM/yyyy"),
Or, you can use MaskedEditBox with the desired datetime mask
I am at the end of my knowledge and googled for the answer too but no luck :/
Week ago everything worked well.
I did a revert on the repository, recreated the tableadapter etc... nothing helped.
When I try to save in my application I get an SystemInvalidCastException at this point:
PersonListDataSet.cs:
partial class P_GroupTableAdapter
{
public int Update(PersonListDataSet.P_GroupDataTable dataTable, string userId)
{
this.Adapter.InsertCommand.Parameters["#userId"].Value = userId;
this.Adapter.DeleteCommand.Parameters["#userId"].Value = userId;
this.Adapter.UpdateCommand.Parameters["#userId"].Value = userId;
return this.Update(dataTable); **<-- Exception occurs here**
}
}
Everything is stuck here because a Guid - and I checked the datatable preview with the magnifier tool its really a true Guid in the column of the datatable - can not be converted to a string ??? How can that happen?
It's the other way around. Your userId is a string and you need a GUID value for your parameters:
Parameters["#userId"].Value = new Guid(userId);
Provided UserId is in one of the supported formats for a GUID. The constructor supports many formats.
Edit, based on comments below:
It turns out that you are asking how to run a select statement like:
SELECT ....
WHERE '{BB6DFF45-FDA7-4155-86D0-0CBF129A9104}' = `domainname\\jondoe`
I think you should re-check your datamodel and find a solution.
Have you tried:
this.Adapter.InsertCommand.Parameters["#userId"].Value = new Guid(userId);
this.Adapter.DeleteCommand.Parameters["#userId"].Value = new Guid(userId);
this.Adapter.UpdateCommand.Parameters["#userId"].Value = new Guid(userId);
Hope it helps!!!
I am am making a calendar and to make it easier on myself I break up appointments that span over multiple weeks.
For instance Jan 1st to Jan 31st spans like 6 weeks(my calendar is always 42 cells - 6 by 7). So I would basically have 6 rows stored in my database.
However somethings I do require to me to put all these rows back together into one row. For instance if I want to export my calendar in Ical format.
I have a field in my database called bindingClassName all these rows get the same unquie id to that group of tasks so I am able to get all the weeks easily.
// get all of the task rows by binding class name.
var found = plannerDb.Calendars.Where(u => u.UserId == userId && u.BindingClassName == bindingClassName)
.GroupBy(u => u.BindingClassName);
List<Calendar> allAppoingments = new List<Calendar>();
// go through each of the results and add it to a list of calendars
foreach (var group in found)
{
foreach (var row in group)
{
Calendar appointment = new Calendar();
appointment.AppointmentId = row.AppointmentId;
appointment.AllDay = row.AllDay;
appointment.BindingClassName = row.BindingClassName;
appointment.Description = row.Description;
appointment.EndDate = row.EndDate;
appointment.StartDate = row.StartDate;
appointment.Title = row.Title;
appointment.Where = row.Where;
appointment.UserId = row.UserId;
allAppoingments.Add(appointment);
}
}
// order
var test = allAppoingments.OrderBy(u => u.StartDate);
var firstAppointment = test.First();
var LastAppointment = test.Last();
Calendar newAppointment = new Calendar();
newAppointment.UserId = firstAppointment.UserId;
newAppointment.Description = firstAppointment.Description;
newAppointment.AllDay = firstAppointment.AllDay;
newAppointment.StartDate = firstAppointment.StartDate;
newAppointment.Title = firstAppointment.Title;
newAppointment.Where = firstAppointment.Where;
newAppointment.BindingClassName = firstAppointment.BindingClassName;
newAppointment.EndDate = LastAppointment.EndDate;
return newAppointment;
So basically that big blob finds all the appointments with the same binding name. Then I go through each one and make it into a Calendar object then finally once it is all made I get the first and last record to get the startDate and endDate.
So I am not good with linq but I am not sure if I can just add something after the groupBy to do what I want.
Edit.
I am trying group all my appointments together once I get all of them from the user.
So I have this so far
I tried something like this.
var allApointments = calendar.GetAllAppointments(userId);
var group = allApointments.GroupBy(u => u.BindingClassName).Select(u => new Calendar()).ToList
I was hoping that it would fill each group automatically but it does not. So I am not sure if don't need groupby again.
Edit # admin
Hi thanks for explaining sorting and grouping. How you explained it though it seems either one would work.
Like the code you have for getting the first and last date works great and does what I wanted it to.
I think grouping might have worked because in the end though I am looking just to have one row that has the startdate of the first record and the end date of the last record all the other information would be the same.
So I don't know if it would harder to write that instead or what but like I said your query does what I want.
However that query is used on a single basis. Like I use that query only when a user clicks to view that appointment on my calendar. By clicking on the appointment I get all the information about that appointment and thats where I need to look at if that task spans over multiple days and figure out when the appointment started and when it is going to end.
Now I need another query and I think it would be better if I could actually group them as how I understand it from your explanation it will make one row. the reason I think this is because I want to export all the records in the table from that user.
So if I order them into one continues block by binding name I still going to need some loops that goes through all the records and gets the first and start date. So if I could just group it in one go and the final result would be just one record for each group of binding names and it would have the first start date and the last end date from the first and last record would be better.
Why are you grouping the appointments if you aren't actually using the group? It looks like you're just using them individually. In any case, you're already filtering the rows on a single value for BindingClassName in the Where clause, so you would only end up with 1 (or 0) group(s) anyway.
You can rewrite that series of foreach loops into a Select and ToList() like this:
var allAppointments =
plannerDb.Calendars.Where(
row => row.UserId == userId &&
row.BindingClassName == bindingClassName).OrderBy(
row => row.StartDate).Select(
row => new Calendar()
{
AppointmentId = row.AppointmentId,
AllDay = row.AllDay,
BindingClassName = row.BindingClassName,
Description = row.Description,
EndDate = row.EndDate,
StartDate = row.StartDate,
Title = row.Title,
Where = row.Where,
UserId = row.UserId
}).ToList();
This will give you back the full list in the order you wanted. However, I'm curious why you're retrieving the whole list when it looks like you're only interested in the first and last appointment. You could instead do this:
var baseQuery =
plannerDb.Calendars.Where(
row => row.UserId == userId &&
row.BindingClassName == bindingClassName);
var first = baseQuery.OrderBy(row => row.StartDate).First();
var last = baseQuery.OrderByDescending(row => row.StartDate).Select(
row => row.EndDate).First();
return new Calendar()
{
AppointmentId = first.AppointmentId,
AllDay = first.AllDay,
BindingClassName = first.BindingClassName,
Description = first.Description,
EndDate = last,
StartDate = first.StartDate,
Title = first.Title,
Where = first.Where,
UserId = first.UserId
});
This should produce outputs that are the same as what you have now. I would question, however, if this is exactly what you want. Say you have two appointments:
Appointment 1 starts January 5 and ends on January 10
Appointment 2 starts January 6 and ends on January 7
Using this (and your) logic, you would get the end date as January 7, since Appointment 2 has the larger start date, but Appointment 1 actually ends later. I would recommend changing the second query to this:
var last = baseQuery.OrderByDescending(row => row.EndDate).Select(
row => row.EndDate).First();
This will give you the largest end date, which I think is what you're actually after.
EDIT
I think you're making the (very common) mistake of confusing grouping with sorting. When you say you want to "group the appointments by the binding name", it sounds like you want a full, complete list of appointments, and you want those appointments arranged in such a way as all appointments with a particular binding name form a contiguous block. If that's the case, you want to order the list by the binding name, not group them. Grouping takes the whole list and produces one row per grouping clause and allows you to perform aggregation functions on the remaining columns. For example, let's say I group the appointments on the binding name. This means that my result set will contain one row per binding name, and I can then do things like find the maximum start or end date or something like that; more formally, you can specify aggregation operations, which are operations that take a set of data (i.e. a list of start dates) and return a single piece of data (i.e. the maximum start date).
Unless I'm misunderstanding, it sounds like you still want to retrieve all of the individual assignments, you just want them arranged by binding name. If this is the case, just OrderBy(row => row.BindingName) and it will do the trick. In addition, you may want to avoid using the word "group", as people will think you mean the sort of grouping that I described above.
Just as a side point not concerning the linq, have you looked at AutoMapper? I am currently using this for populating data objects from linq and I've found it really useful for getting rid of the large sections of code where you just map to dtos. It wouldn't make the query parts of your code any shorter but would reduce:
return new Calendar()
{
AppointmentId = first.AppointmentId,
AllDay = first.AllDay,
BindingClassName = first.BindingClassName,
Description = first.Description,
EndDate = last,
StartDate = first.StartDate,
Title = first.Title,
Where = first.Where,
UserId = first.UserId
});
to:
return Mapper.Map(first,new Calendar{EndDate = last});