0

I am a new ASP.NET developer and I have 2 inputs in a form as DateTimes; StartDate and EndDate. I want to make sure that both of them are within the StartDate and EndDate of the Parent from another table. The logic should be as following:

  1. Check if the StartDate of the items comes before the EndDate.
  2. Check if the StartDate & EndDate of the inserted item is within the range of the StartDate and EndDate in the Customers table

So how can I do this logic?

For your information, I have two tables in the database, namely; Customers and Items.Their schema are: Customers Table:

CREATE TABLE [dbo].[Customers](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NULL,
    [Job] [varchar](50) NULL,
    [StartDate] [datetime] NOT NULL,
    [EndDate] [datetime] NOT NULL,
 CONSTRAINT [PK_Customers] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

Items Table:

CREATE TABLE [dbo].[Items](
    [ID] [int] IDENTITY(1,1) NOT NULL,
    [Name] [varchar](50) NOT NULL,
    [Description] [varchar](50) NULL,
    [StartDate] [datetime] NOT NULL,
    [EndDate] [datetime] NOT NULL,
    [CustomerID] [int] NOT NULL,
 CONSTRAINT [PK_Items] PRIMARY KEY CLUSTERED 
(
    [ID] ASC
)WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
) ON [PRIMARY]

GO

SET ANSI_PADDING OFF
GO

ALTER TABLE [dbo].[Items]  WITH CHECK ADD  CONSTRAINT [FK_Items_Customers] FOREIGN KEY([CustomerID])
REFERENCES [dbo].[Customers] ([ID])
GO

ALTER TABLE [dbo].[Items] CHECK CONSTRAINT [FK_Items_Customers]
GO

My C# Code:

protected void btnSubmit_Click(object sender, EventArgs e)
    {
        DateTime startDate = Convert.ToDateTime(txtStartDate.Text);
        DateTime endDate = Convert.ToDateTime(txtEndDate.Text);
        if (DateTime.Compare(startDate, endDate) <= 0)
        {
            using (TestDBEntities1 TEntities = new TestDBEntities1())
            {
                var newItem = new Item
                {
                    Name = txtName.Text,
                    Description = txtDesc.Text,
                    StartDate = startDate,
                    EndDate = endDate,
                    CustomerID = Convert.ToInt32(ComboBox1.SelectedValue)
                };
                TEntities.Items.AddObject(newItem);
                TEntities.SaveChanges();
            }
            lblMessage.Text = "Successfully Added :)";
        }
        else
        {
            lblMessage.Text = "Error!!!";
        }
    }

NOTE: I am using LINQ for querying the database. I am only missing the part of doing the logic for checking if the StartDate & EndDate of the inserted item is within the Dates in the Customers table.

Explanation:

For example, let us assume we have StartDate#1 and EndDate#1 for an item that will be inserted. In the Customers table, I have StartDate#2 and EndDate#2. The logic should do the following:

  1. check if the StartDate#1 comes before EndDate#1
  2. Then, check if StartDate#1 and EndDate#1 are falling within the range of StartDate#2 and EndDate#2
3
  • Check SO entry, Determine Whether Two Date Ranges Overlap stackoverflow.com/questions/325933/… Oct 12, 2013 at 18:00
  • Thanks for your comment. However, my question is different than that one. Could you please help me? Oct 12, 2013 at 18:36
  • I would start by splitting this up into two pieces: validating the singular daterange, and validating the daterange against another daterange. We actually have a DateRange object which has 'StartDate' and 'EndDate' properties and Validate() ensures that the StartDate (if required) always comes before the EndDate (also if required). For the second validation, you could do a DateRange1IsWithingDateRange2(DateRange firstDateRange, DateRange secondDateRange) or something along those lines. In SQL we have a function too which checks overlapping days. Oct 12, 2013 at 19:43

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Browse other questions tagged or ask your own question.