Results 1 to 5 of 5

Thread: How can I set an event using the data from a SQL database

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2016
    Posts
    5

    How can I set an event using the data from a SQL database

    Hi all
    I have written simple programs that show in DataGridView some info. The most important info is the expiration date , which I manage to color it read when the expiration date is near(like 5 days). The person responsible has an email in the database. I use a SQL database. What is the code to send email only to the persons that their products are near the expiration date? If I call the function send_mail() in the code that colors the line red it sends infinite mails to 1 person. This is my code:

    Private Sub dgvExcel_CellFormatting(sender As Object, e As DataGridViewCellFormattingEventArgs) Handles DataGridView1.CellFormatting

    If CDate(DataGridView1.Rows(e.RowIndex).Cells(DataGridView1.Columns(5).Index).Value) <= Date.Now Then
    e.CellStyle.BackColor = Color.Red
    'send_mail()
    End If

    If CDate(DataGridView1.Rows(e.RowIndex).Cells(DataGridView1.Columns(4).Index).Value) <= Date.Now Then
    e.CellStyle.BackColor = Color.White
    e.CellStyle.ForeColor = Color.Black
    End If


    End Sub

  2. #2
    PowerPoster PlausiblyDamp's Avatar
    Join Date
    Dec 2016
    Location
    Pontypool, Wales
    Posts
    2,458

    Re: How can I set an event using the data from a SQL database

    So what does send_mail() look like? if that is responsible for sending the emails then there is a good chance that is where the problem lies.
    Last edited by PlausiblyDamp; Jul 29th, 2020 at 09:21 AM.

  3. #3
    Smooth Moperator techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,532

    Re: How can I set an event using the data from a SQL database

    That's because you're calling it from the CellFormatting event... which is not the appropriate place to call it... or it might be... but you only want to call it once... not every single time. Do you even want to send them an email? They're already in the app. They can see the row/cell in red... Is sending them an email from inside the app telling them something they should already know really what you want?
    A more appropriate (in my opinion) method for sending the emails would be for a SQL Server job to scan the table, and send the emails once a day, so that it alerts them at the beginning of the day that these are the ones to watch for. But that's the server/database doing it, not the app, and is a different thing alltogeher.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    38,989

    Re: How can I set an event using the data from a SQL database

    I like that approach. If they run the app, the email is redundant. If they don't run the app, the email will never happen. An email that is separate from the app seems more likely to be useful...with a few caveats. If the database is on their local computer, then I'd say that no email is ever appropriate, because there are better options, but I'm thinking that's not the case.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2016
    Posts
    5

    Re: How can I set an event using the data from a SQL database

    Thank you all

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width