-1

I want to add to my project countdown to expire column. I've created medicine database.

And I'm struggling on the countdown to expire a medicine But it's not showing the correct number of days to expire. The last column. I presented it on image:

enter image description here

My Expire Date function:

public void UpdateCountdown()
{
    DateTime d1;
    DateTime d2;
    for (int i = 0; i < dataGridView1.RowCount - 1; i++)
    {
        d1 = Convert.ToDateTime(DateTime.Now.ToString("yyyy-MM-dd")); // date now
        d2 = Convert.ToDateTime(dataGridView1.Rows[i].Cells[5].Value); // expiry date column
        TimeSpan ts = d2- d1;

        dataGridView1.Rows[i].Cells[9].Value = ts.Days;
        string Query3 = "Update Medicine set DaysLeft ='" + ts.Days + "'" + " where rowid ='" + dataGridView1.Rows[i].Cells[0].Value + "'";
        ExcecuteQuery(Query3);
        LoadData();
    }
}   
marc_s
  • 732,580
  • 175
  • 1,330
  • 1,459
Medic2133
  • 21
  • 5
  • 1
    *"something is wrong"* What is wrong? I've looked at the picture but I can't figure out about what's the problem – Cid Dec 07 '21 at 22:47
  • The problem is last column countdown to expire in days – Medic2133 Dec 07 '21 at 22:48
  • It doesn;t show correct number of days to expire – Medic2133 Dec 07 '21 at 22:48
  • I've corrected my question – Medic2133 Dec 07 '21 at 22:50
  • Use the debugger to step through your code line by line and see what happens. Are you getting the dates that you expect to see? Is the database getting updated as you think it should? Try narrowing down the problem to a specific line of code where an input is not producing the output or result you're expecting. – StriplingWarrior Dec 07 '21 at 22:53
  • 1
    Use the debugger and look at the values of d1, d2, and ts. Use parameters to avoid sql injection and formatting errors. – LarsTech Dec 07 '21 at 22:53
  • I just wanted that it will show how many days left to expire in the last column – Medic2133 Dec 07 '21 at 22:56
  • I spoted something on your code, and as i recall you might be using 1 thing wrong there: the "ts.Days" is not the same as "ts.TotalDays". A TimeSpan object would return a format in number of days if you call the TotalDays property instead of the Days property. – SammuelMiranda Dec 08 '21 at 11:41
  • We need to see a [mcve] to help you. We have no idea whether the problem is that the number of days to expire is **totally wrong**, or **off by one**, or something else entirely. The problem might be with your `Querry3` string -- but there might also be problems with `ExcecuteQuery()` and `LoadData()`. You're more likely to get useful help here if you follow the suggestions from [ask], specifically the section ***Help others reproduce the problem***. – dbc Dec 14 '21 at 00:57
  • That being said, if the problem is that the number of days to expire is **off by one** then @SammuelMiranda's comment is relevant. As explained in [What is the diffrence beetween Days and TotalDays?](https://stackoverflow.com/q/25212667/3744182), `ts.Days` is just `(int) ts.TotalDays` -- i.e. the number of days rounded down. Maybe you want it rounded up? – dbc Dec 14 '21 at 01:02

1 Answers1

1

Consider using the following code SO answer which is done as per below.

enter image description here

public class DateHelper
{
    public static string CalculateExpirationTime(DateTime expiryDate)
    {
        var currentDate = DateTime.Now;
        var dateDifference = (expiryDate - currentDate);

        if (dateDifference.Days >= 1)
            return $"{ dateDifference.Days } day(s) remained";
        else if (dateDifference.Hours >= 1)
            return $"{ dateDifference.Hours } hour(s) remained";
        else if (dateDifference.Minutes >= 1)
            return $"{ dateDifference.Minutes } minute(s) remained";
        else if (dateDifference.TotalSeconds >= 1)
            return $"{ dateDifference.Seconds } second(s) remained";

        return "Expired!";
    }
}

Form code where in this case the button code outputs to Visual Studio's output window.

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private readonly BindingSource _bindingSource = new BindingSource();
    private void Form1_Load(object sender, EventArgs e)
    {
        var table = new DataTable();
        table.Columns.Add("NameColumn", typeof(string));
        table.Columns.Add("DateColumn", typeof(DateTime));
        table.Columns.Add("ExpireColumn", typeof(string));

        table.Rows.Add("Jane", new DateTime(2021,12,1));
        table.Rows.Add("Mike", new DateTime(2021,12,10));
        table.Rows.Add("Karen", new DateTime(2021,12,23));
        table.Rows.Add("Anne", new DateTime(2022,1,1));

        for (int rowIndex = 0; rowIndex < table.Rows.Count; rowIndex++)
        {
            table.Rows[rowIndex].SetField("ExpireColumn", 
                DateHelper.CalculateExpirationTime(
                    table.Rows[rowIndex].Field<DateTime>("DateColumn")));
        }

        _bindingSource.DataSource = table;
        dataGridView1.DataSource = _bindingSource;

    }
    private void IterateButton_Click(object sender, EventArgs e)
    {
        DataTable table = (DataTable)_bindingSource.DataSource;

        for (int rowIndex = 0; rowIndex < table.Rows.Count; rowIndex++)
        {
            Console.WriteLine(table.Rows[rowIndex]["ExpireColumn"]);
        }
    }
}
Karen Payne
  • 4,341
  • 2
  • 14
  • 31