Export directly to pdf - c#

I am using two stored procedures, one for main report and another for subreport. Below is the code.
private void LoadSalesOrderReport()
{
string Type = gvQuotationDetails.Rows[QuoteIndex].Cells["Type"].EditedFormattedValue.ToString();
FilePath = ConfigurationManager.AppSettings["EMP_IMG_PATH"].ToString() + "\\" + ValQuoteID.ToString() + ".pdf";
DeleteExistingFile(FilePath);
try
{
AccountsPayableMaster objAPM = new AccountsPayableMaster();
QuotationReport obj = new QuotationReport();
objReportDocument.Load(Application.StartupPath + #"\rptQuotationReport.rpt");
obj.crysQuotationReport.LogOnInfo = objAPM.ConnectionDetails("SD_SalesOrderReport;1");
obj.crysQuotationReport.LogOnInfo = objAPM.ConnectionDetails("SD_GetBatchReportDetails;1");
obj.crysQuotationReport.ReportSource = objReportDocument;
objReportDocument.SetParameterValue("#QuoteID", ValQuoteID);
objReportDocument.SetParameterValue("Type", "-" + Type.ToUpper() + "-");
objReportDocument.SetParameterValue("#QuoteID", ValQuoteID, objReportDocument.Subreports[0].Name.ToString());
string[] Print = objAPM.GetPrintDetails();
SetPrintParameters(objReportDocument, Print);
obj.Show();
objReportDocument.ExportToDisk(ExportFormatType.PortableDocFormat, FilePath);
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
OpenPdfFile();
}
private void OpenPdfFile()
{
try
{
Process.Start(FilePath);
}
catch (Exception ex)
{
MessageBox.Show("Please install MicrosoftOffice/Pdf Reader to view files", "", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
The code is working fine.But the problem is. when i click the button in front end to show the pdf directly.The crystal report form is also displayed and I know the reason as I am using obj.Show in my code.I tried to comment it but it throws an error.Can any one advise changes in my code to directly display the pdf and not the crystalreport form.

Related

C# - Using loop to open dialog box

I'm trying to make a program that loads a configuration file from another application.
If the file exists, it loads it and displays a message, but if the configuration file is not valid, it displays an error message and then opens a dialog box to load the correct file. But if the user reloads the wrong file, the same dialog box should appear again but that's when my code fails.
Similarly, if the file did not exist from the beginning, it displays a dialog box to load the file, but if it is given to cancel the dialog box or an incorrect file is selected again, my code fails.
I know that the solution would be to use loops but I'm not sure how to structure it.
Pd: searchfile() is my function to open dialog box and readconfig() is my function to read config file of another application.
strfilenamepath = #"C:\Users\test\dogs.exe.config";
if (File.Exists(strfilenamepath))
{
onlyFilename = System.IO.Path.GetFileName(strfilenamepath);
textBox1.Text = onlyFilename;
try
{
string[] valores = readConfig(strfilenamepath);
MessageBox.Show(valores[0] + valores[1] + valores[2]);
}
catch (Exception ex)
{
MessageBox.Show("Error loading config file." + ex.Message);
searchFile();
onlyFilename = System.IO.Path.GetFileName(strfilenamepath);
textBox1.Text = onlyFilename;
string[] valores = readConfig(strfilenamepath);
MessageBox.Show(valores[0] + valores[1] + valores[2]);
}
}
else
{
searchFile();
onlyFilename = System.IO.Path.GetFileName(strfilenamepath);
textBox1.Text = onlyFilename;
try
{
readConfig(strfilenamepath);
string[] valores = readConfig(strfilenamepath);
MessageBox.Show(valores[0] + valores[1] + valores[2]);
}
catch (Exception ex)
{
MessageBox.Show("Error loading config file." + ex.Message);
searchFile();
onlyFilename = System.IO.Path.GetFileName(strfilenamepath);
textBox1.Text = onlyFilename;
string[] valores = readConfig(strfilenamepath);
MessageBox.Show(valores[0] + valores[1] + valores[2]);
}
}
It is easier to design it if you extract the reading logic to another method that handles exceptions and returns a Boolean to signal the success and the computed result. The TryDoSomething pattern does exactly this.
In pseudo code
public bool TryReadConfig(string path, out string[] valores)
{
valores = null;
try {
valores = read the values;
return true;
} catch {
Display message;
return false;
}
}
The main loop in pseudo code
strfilenamepath = #"C:\Users\test\dogs.exe.config";
while (true) {
if (File.Exists(strfilenamepath) && TryReadConfig(strfilenamepath, out var valores)) {
Do something with the valores;
break;
}
var ofd = new OpenFileDialog{ ... };
if (ofd.ShowDialog() == DialogResult.OK) {
strfilenamepath = ofd.Filename;
} else {
break; // The user canceled the operation.
}
}
You can do something like this:
try
{
//Code to try open the file to memory
}
catch (Exception ex)
{
while (true)
{
MessageBox.Show(#"Select an valid file");
var path = searchFile();
if (string.IsNullOrWhiteSpace(path))
continue;
try
{
//Code to try open the file to memory
}
catch (Exception ex2)
{
MessageBox.Show(#"The selected file is not valid");
continue;
}
break;
}
}

C# Copy TextBox and the settings i've used copy aswel

Basically, I have around 40 textBoxes on a form, which I want to copy, the settings are on the first boxes I have, but I copied the first row and the save settings I used did not copy, so only the first row of boxes save the text. The code is:
textBox1.Text = WindowsFormsApplication12.Properties.Settings.Default.Method.ToString();
textBox2.Text = WindowsFormsApplication12.Properties.Settings.Default.Price.ToString();
textBox3.Text = WindowsFormsApplication12.Properties.Settings.Default.PerHour.ToString();
textBox4.Text = WindowsFormsApplication12.Properties.Settings.Default.Viable;
try
{
WindowsFormsApplication12.Properties.Settings.Default.Method = textBox1.Text;
WindowsFormsApplication12.Properties.Settings.Default.PerHour = Convert.ToInt32(textBox3.Text);
WindowsFormsApplication12.Properties.Settings.Default.Total = Convert.ToInt32(textBox4.Text);
WindowsFormsApplication12.Properties.Settings.Default.Viable = textBox40.Text;
WindowsFormsApplication12.Properties.Settings.Default.Save();
}
catch (FormatException ex)
{
MessageBox.Show("Error : Make sure you have only entered numbers! \n\n" + ex.ToString());
}

How to create a folder then save an image to the created folder using fileuploader in C# asp.net?

I have a simple program here wherein I can create a folder then save the an image to the folder created. The folder gets successfully created but I'm getting error into saving it to the newly created file. The error is:
The file could not be uploaded. The following error occured: 'N:/Kim's New Project/Safety Accident Report/File Uploader 2/File
Uploader 2/Uploads/asdsa' is a physical path, but a virtual path was
expected.
Can you please check my code. Please help. Thank you.
protected void button1_Click(object sender, EventArgs e)
{
if (FileUpload2.HasFile)
{
try
{
if (FileUpload2.PostedFile.ContentType == "image/jpeg")
{
if (FileUpload2.PostedFile.ContentLength < 512000)
{
string strpath = #"N:\Kim's New Project\Safety Accident Report\File Uploader 2\File Uploader 2\Uploads\" + txtName.Text;
if (!(Directory.Exists(strpath)))
{
Directory.CreateDirectory(strpath);
lblResult.Text = "Directory Created";
if ((Directory.Exists(strpath)))
{
string filename = Path.GetFileName(FileUpload2.FileName);
FileUpload2.SaveAs(Server.MapPath(strpath) + filename);
Label1.Text = "File uploaded successfully!";
}
}
else
{
lblResult.Text = "Already Directory Exists with the same name";
}
}
else
Label1.Text = "File maximum size is 500 Kb";
}
else
Label1.Text = "Only JPEG files are accepted!";
}
catch (Exception exc)
{
Label1.Text = "The file could not be uploaded. The following error occured: " + exc.Message;
}
}
Instead of
FileUpload2.SaveAs(Server.MapPath(strpath) + filename);
try
FileUpload2.SaveAs(Path.Combine(strPath, filename));
you already know the physical save path - no need for Server.MapPath
Try..
string strpath = Server.MapPath("~/Test");
if (!(Directory.Exists(strpath)))
{
Directory.CreateDirectory(strpath);
}

To delete a desk top file in asp.net c#

i want a solution for this , i want to delete a file which is residing in my desk top using asp.net c# , i used below code:
try
{
FileInfo TheFile = new FileInfo(MapPath(".") + "\\" + FileNameTextBox.Text);
if (TheFile.Exists)
{
File.Delete(MapPath(".") + "\\" + FileNameTextBox.Text);
}
else
{
throw new FileNotFoundException();
}
}
catch (FileNotFoundException ex)
{
lblStatus.Text += ex.Message;
}
catch (Exception ex)
{
lblStatus.Text += ex.Message;
}
but it always says the file location cannot be found , please help me
thanks in advance `
If you are trying to delete a user's desktop file using an asp .net page you cannot do it. The code executes on the server side and the path will access to the desktop of the server that your application is being hosted.
I would try doing it this way instead:
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
File.Delete(Path.Combine(desktopPath, "filetobedeleted"));

How to open the new window in C#

I like to open a new window in my project. I am using two panels in my project When I click the "Viewdocument" link in gridview(panel1) it should display the window to open that file. But in my code its not working can any one help me to solve this issue. Here is the code.
if (myReader.Read())
{
myReader.Close();
openWIndow("fr_OpenFile.aspx", "", fileName);
Linkbutton_ModalPopupExtender.Show();
//OpenMyFile();
}
else
{
myReader.Close();
Message("Cannot open selected file");
Linkbutton_ModalPopupExtender.Show();
return;
}
con.Close();
//OpenMyFile();
}
else
{
Message("File not found");
Linkbutton_ModalPopupExtender.Show();
}
}
catch (Exception ex)
{
lblmsg.Text = ex.Message;
}
}
private void openWIndow(String FileName, String WindowName, String qString)
{
String fileNQuery = FileName + "?value=" + qString;
String script = #"<script language=""javascript"">" + "window.open(" + fileNQuery + WindowName + "," + "menubar=Yes,toolbar=No,resizable=Yes,scrollbars=Yes,status=yes" + " );" + "</script>";
ClientScript.RegisterStartupScript(this.GetType(), "OpenWindow", script);
}
Thanks in Advance
There are many ways. You can do this by simply using target="_blank" in asp.net hyperlink control. You can do this using window.open() function in JavaScript. You can even register this JavaScript code from asp.net code behind. All detail is discussed here in How to open new window in asp.net, c# using JavaScript? Follow http://dotnetspidor.blogspot.com/2009/01/open-new-window-in-aspnet-web-page_28.html.
read this: http://www.velocityreviews.com/forums/t112713-open-new-browser-window-on-server-side-button-click.html

Categories