C# Post-Publish Issues


Joined
Feb 21, 2018
Messages
160
Reaction score
62
I wrote a ClickOnce-windows-form application ...it executes very well (but when I run in development mode)...gives no error....
but when I try to run after publishing it....it stops responding ...no error is displayed..Seems as if it silently exits...

Please rescue me....
 
Ad

Advertisements

Joined
Feb 21, 2018
Messages
160
Reaction score
62
When you publish, are you also pushing the dependencies?
Thank you ...I didn't do anything like that... Let me check.. That's where probably the problem lies....
I would appreciate if you can give me an example ...which dependencies are mattering and how to push the dependencies?


Please have a look of my Program.cs code

using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace GEWP
{
static class Program
{
/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
Application.Run(new Form1());
}
}
}








Form1.cs code is as follows

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using Dapper;
using iTextSharp.text;
using iTextSharp.text.pdf;
using System.IO;
using System.Net;
using System.Net.Mail;
using Font = iTextSharp.text.Font;

namespace GEWP
{
public partial class Form1 : Form
{
static string MainDir = AppDomain.CurrentDomain.BaseDirectory;

static string RunThis = System.IO.File.ReadAllText("RunThis.txt");
static string connStr = System.IO.File.ReadAllText("DapperCrud.txt");
static string PdfPath = System.IO.File.ReadAllText("PdfPath.txt");
SqlConnection SqlCon = new SqlConnection(@connStr);
//int ContactID = 0;

private System.Windows.Forms.Timer tmr;

public Form1()
{

InitializeComponent();
{
//in 30 seconds the form will close automatically
tmr = new System.Windows.Forms.Timer();
tmr.Tick += delegate {
this.Close();
};
tmr.Interval = (int)TimeSpan.FromMinutes(.5).TotalMilliseconds;
tmr.Start();

try
{
MessageBox.Show("My Main Directory " + MainDir);
MessageBox.Show("Stored Procedure " + RunThis);
MessageBox.Show("Connection Str" + connStr);
MessageBox.Show("PDF-Save Path" + PdfPath);

FilldataGridView();

}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}
}
exportGridtoPDF(dgv1, "BannerInventory" + DateTime.Now.ToString("yyyyMMddHHmmssfff") + ".pdf");

}


void FilldataGridView()
{


List<item> list = SqlCon.Query<item>(RunThis, commandType:
CommandType.StoredProcedure).ToList<item>();

dgv1.DataSource = list;
//dgv1.Columns[0].AutoSizeMode=DataGridViewAutoSizeColumnMode.Fill;
}
class item
{
public string ItemDesc { get; set; }
public string Category { get; set; }
public int Qty_IN { get; set; }
public int Qty_Out { get; set; }
public int Qty_Bal { get; set; }

}

public void exportGridtoPDF(DataGridView dgv1, string filename)

{
filename = @PdfPath + filename + ".pdf";
BaseFont bf = BaseFont.CreateFont(BaseFont.TIMES_ROMAN, BaseFont.CP1250, BaseFont.EMBEDDED);
try
{
PdfPTable pdftable = new PdfPTable(dgv1.Columns.Count);
pdftable.DefaultCell.Padding = 3;
pdftable.WidthPercentage = 100;
pdftable.HorizontalAlignment = Element.ALIGN_LEFT;
pdftable.DefaultCell.BorderWidth = 1;




iTextSharp.text.Font text = new iTextSharp.text.Font(bf, 10, iTextSharp.text.Font.NORMAL);
// Add Header
foreach (DataGridViewColumn column in dgv1.Columns)
{
PdfPCell cell = new PdfPCell(new Phrase(column.HeaderText, text));
cell.BackgroundColor = new iTextSharp.text.BaseColor(240, 240, 240);
pdftable.AddCell(cell);

}


// Add data Row
foreach (DataGridViewRow row in dgv1.Rows)
{
foreach (DataGridViewCell cell in row.Cells)
{
pdftable.AddCell(new Phrase(cell.Value.ToString(), text));
}
}

var savefiledialoge = new SaveFileDialog();
savefiledialoge.FileName = filename;
savefiledialoge.DefaultExt = ".pdf";
//if (savefiledialoge.ShowDialog() == DialogResult.OK)

if (1 == 1)
{

using (FileStream streamx = new FileStream(filename, FileMode.Create))
{
Document pdfdoc = new Document(PageSize.A4, 10f, 10f, 10f, 0f);
PdfWriter.GetInstance(pdfdoc, streamx);
pdfdoc.Open();
pdfdoc.Add(pdftable);
pdfdoc.Close();
//streamx.Close();
}
}

}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}

var fromAddress = new MailAddress("(e-mail address removed)", "SuCorporation");
var toAddress = new MailAddress("(e-mail address removed)", "Fa");
const string fromPassword = "m%";
const string subject = "SSC Reporting System";
const string body = "This is an Auto-generated email by SSC Reporting System";

var smtp = new SmtpClient
{
Host = "smtp.gmail.com",
Port = 587,
EnableSsl = true,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = true,
Credentials = new NetworkCredential(fromAddress.Address, fromPassword)
};
using (var message = new MailMessage(fromAddress, toAddress)
{
Subject = subject,
Body = body
})
{
System.Net.Mail.Attachment attachment;
attachment = new System.Net.Mail.Attachment(filename);
message.Attachments.Add(attachment);
smtp.Send(message);
}

}
private void Form1_Load(object sender, EventArgs e)
{
try
{
FilldataGridView();

}
catch (Exception ex)
{

MessageBox.Show(ex.Message);
}
}
void Data2PDF(DataTable dataTable, string filename)
{

}

}

internal class Cell
{
internal void Add(Chunk chunkCols)
{
throw new NotImplementedException();
}
}
}
 
Last edited:
Ad

Advertisements

Joined
Feb 21, 2018
Messages
160
Reaction score
62
Alhamdolillah...the issue got resolved itself after using the previous copy of the source code....Thanks for your enlightening me with importance of dependency injections etc...I have decided to learn this from now on.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top