This ishow i resolved my problem
for (int i = 0; i < dgv1.Rows.Count - 1; i++)
{
DataGridViewRow dgvbills = dgv1.Rows[i];
string ac_code = dgvbills.Cells["ac_code"].Value.ToString();
string zippath = Path.GetTempPath() + ac_code + ".Zip";
finescompressed = 0;
//emailsent = false;
while (ac_code.Equals(dgvbills.Cells["ac_code"].Value.ToString()))
{
string gateoutdate = dgvbills.Cells["gateoutdate"].Value.ToString();
string SignedEinv = @"C:\eInvoices\Response\" +
dgvbills.Cells["doc_no"].Value.ToString() + "x" +
dgvbills.Cells["doc_gl"].Value.ToString() + "x" +
dgvbills.Cells["finyear"].Value.ToString() + ".json";
if (string.IsNullOrEmpty(gateoutdate))
{
sw.WriteLine(SignedEinv + ":Pending For Gateout");
}
else
{
if (File.Exists(SignedEinv))
{
//https://learn.microsoft.com/en-us/dotnet/standard/io/how-to-compress-and-extract-files
//create zip file
using (FileStream zipToOpen = new FileStream(zippath, FileMode.OpenOrCreate))
{
using (ZipArchive archive = new ZipArchive(zipToOpen, ZipArchiveMode.Update))
{
archive.CreateEntryFromFile(SignedEinv, Path.GetFileName(SignedEinv), CompressionLevel.Optimal);
finescompressed++;
}
}
}
else
{
sw.WriteLine(SignedEinv+":Not Found");
}
}
if (i < dgv1.Rows.Count - 1)
{
i++;
dgvbills = dgv1.Rows[i];
}
else
{
break;
}
}
if (finescompressed <= 0)
{
sw.WriteLine("No Files In Zip To Email");
continue;
}
lblNoofRecord.Text = zippath;
//get email id from contact table
string emailid = "";
using (SqlConnection con = new SqlConnection(Appvaribales.ConnectionString))
{
//SQL Function Example
/*
* Get email id from mainsl contacts for department = 'EINVOICE'
*/
SqlCommand cmd = new SqlCommand("select dbo.fnEinvEmailId(@mainsl_ui,@department)", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new SqlParameter("@mainsl_ui", dgvbills.Cells["mainsl_ui"].Value.ToString()));
cmd.Parameters.Add(new SqlParameter("@department", "EINVOICE"));
try
{
con.Open();
emailid = (string)cmd.ExecuteScalar();
con.Close();
}
catch (Exception ex)
{
MessageBox.Show(ex.ToString());
}
}
if (string.IsNullOrWhiteSpace(emailid))
{
sw.WriteLine("No Email Id in EINVOICE deaprtment Of A/c:" +
dgvbills.Cells["ac_code"].Value.ToString() + " is Found");
continue;
}
sendOutlookemail(zippath, emailid,ref sw);
if (pgbBillItemQuery.Value == pgbBillItemQuery.Maximum)
{
pgbBillItemQuery.Value = pgbBillItemQuery.Minimum;
}
else
{
//pgbEmail.Value += 1;
pgbBillItemQuery.PerformStep();
}
}