Convert visual basic to C#.net RRS feed

  • Question

  • Hello all , Sorry i know this is too much to ask , but i am helpless as i am very new to c# .net .

    I was using this below code to automatically send mail to the concerned if the task is over due in access database , the overdue criteria was fulfilled by a query.

    but now i am creating a winform app for the same database :"CSC.accdb", "Tasks_Bindingsource", "Tasks_tableadapter" , "Tasks_table".

    Now i want to convert this below code from VBA to C# and use it in a button click event , which i searched for hours in internet but couldn't find anything & also i don't know how can i pass the criteria (which is less today of the "scheduled" column) ,So by looking at the code below kindly help how to achieve the same in winforms app.

    Option Compare Database
    Private Sub cmdtrial_Click()
    Dim db As DAO.Database
    Dim rs As DAO.Recordset
    Dim emailto As String
    Dim emailcc As String
    Dim emailsubject As String
    Dim emailtext As String
    Dim outapp As Outlook.Application
    Dim outmail As Outlook.MailItem
    Dim outstarted As Boolean
    Dim rpt As Report
    Dim pdfmail As String
    On Error Resume Next
    Set outapp = GetObject(, "outlook.application")
    On Error GoTo 0
    If outapp Is Nothing Then
    Set outapp = CreateObject("outlook.application")
    outstarted = True
    End If
    Set db = CurrentDb
    Set rs = db.OpenRecordset("qryeMailOverdueTasks")
    DoCmd.OutputTo acOutputReport, "tasks", acFormatPDF, "tasks.pdf"
    Do Until rs.EOF
    emailto = rs.Fields("responsible person").Value
    emailcc = "Abishek.Gowda@plansee.com"
    emailsubject = "Pending tasks to complete the issue regarding" & " " & rs.Fields("issue").Value
    emailtext = "Hello" & vbCrLf & "Kindly complete the task which is in the attached file to complete the cutomer isue regarding" & " " & rs.Fields("issue").Value
    pdfmail = "tasks.pdf"
    Set outmail = outapp.CreateItem(olMailItem)
    outmail.To = emailto
    outmail.CC = emailcc
    outmail.Subject = emailsubject
    outmail.Body = emailtext
    outmail.Attachments.Add ("tasks.pdf")
    Set rs = Nothing
    Set db = Nothing
    If outstarted Then
    End If
    Set outmail = Nothing
    Set outapp = Nothing
    End Sub

    Friday, August 7, 2020 12:13 PM

All replies

  • Hi,
    try this code:

        private void cmdtrial_Click()
          string connectionString = Properties.Settings.Default.cnDatabase1;
          using(OleDbConnection cn = new OleDbConnection())
          using (OleDbCommand cmd = new OleDbCommand("SELECT [responsible person], [issue] FROM qryeMailOverdueTasks", cn))
            OleDbDataReader rs = cmd.ExecuteReader();
            while (rs.Read())
              using (MailMessage outmail = new MailMessage())
                outmail.From = new MailAddress("yourAdress@xxx.de");
                outmail.Subject = $"Pending tasks to complete the issue regarding {rs.GetString(1)}";
                outmail.Body = $"Hello{Environment.NewLine}Kindly complete the task which is in the attached file to complete the cutomer isue regarding {rs.GetString(1)}";
                SmtpClient smtp = new SmtpClient("mail.xxx.net"); // SMTP Provider
                smtp.Credentials = new NetworkCredential("yourAddress@xxx.net", "yourPasswort"); // send credentials

    Best Regards / Viele Grüße
    Peter Fleischer (former MVP for Developer Technologies)
    Homepage, Tipps, Tricks

    Friday, August 7, 2020 1:13 PM
  • Hello,

    Since you are using a TableAdapter you can write code such as this to loop through records. Then replace the Console.WriteLine with the code for emailing, as Peter pointed out SMTP gets the job done.

    foreach (var row in TaskDataSet.Tasks_table)
        Console.WriteLine($"{row.responsible_person}, {row.Issue}");

    EDIT: In regards to the schedule part

    Create a new query in the .xsd file 

    Modified code

    var date = DateTime.Now.AddDays(-1);
    Tasks_tableadapter.FillByScheduleIssues(this.TaskDataSet.Tasks_table, date);
    foreach (var row in TaskDataSet.Tasks_table)
        Console.WriteLine($"{row.responsible_person}, {row.Issue}, {row.Scheduled:d}");

    Note the following site has everything needed for basics on sending SMTP email messages.

    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    profile for Karen Payne on Stack Exchange

    Friday, August 7, 2020 2:31 PM