I am trying to see what exception is happening on my dev clients devserver (our servers are fine) and I have an TestException.cs class that handles the most of my exceptions from my sql statement class.
I have a method like so:
StringBuilder errorMessages = new StringBuilder();
for (int i = 0; i < innerException.Errors.Count; i++)
{
errorMessages.Append("Index #" + i + "\n" +
"Message: " + innerException.Errors[i].Message + "\n" +
"LineNumber: " + innerException.Errors[i].LineNumber + "\n" +
"Source: " + innerException.Errors[i].Source + "\n" +
"Procedure: " + innerException.Errors[i].Procedure + "\n");
}
//Console.WriteLine(errorMessages.ToString());
Response.Clear();
Response.Write("FAILURE");
Response.End();
But the Response.XX won't compile and if I use console.writeline when it hits it on the client server, the browser gets a connection lost webpage.
Is there a better way to do this?
You could try using the Trace class. If you link with a tracewriter in web.config pointing to a text file you can generate logging with Trace.WriteLine. Then you would just need the file uploaded to your network for reviewing.
Related
private void ConvertHTMLtoDOCX(string txtcode)
{
System.Text.StringBuilder strBody = new System.Text.StringBuilder("");
strBody.Append("<html " + "xmlns:o='urn:schemas-microsoft-com:office:office' " + "xmlns:w='urn:schemas-microsoft-com:office:word'" + "xmlns='http://www.w3.org/TR/REC-html40'>" + "<head><title>Time</title>");
//The setting specifies document's view after it is downloaded as Print
//instead of the default Web Layout
strBody.Append("<!--[if gte mso 9]>" + "<xml>" + "<w:WordDocument>" + "<w:View>Print</w:View>" + "<w:DoNotOptimizeForBrowser/>" + "</w:WordDocument>" + "</xml>" + "<![endif]-->");
strBody.Append("<style>" + "<!-- /* Style Definitions */" + "#page Section1" + " {size:8.5in 11.0in; " + " margin:1.0in 1.25in 1.0in 1.25in ; " + " mso-header-margin:.5in; " + " mso-footer-margin:.5in; mso-paper-source:0;}" + " div.Section1" + " {page:Section1;}" + "-->" + "</style></head>");
strBody.Append("<body lang=EN-US style='tab-interval:.5in'>" + "<div class=Section1>" + Html_editor.Content + "</div></body></html>");
//Force this content to be downloaded
//as a Word document with the name of your choice
string FullFilePath = #"C:\Users\ravikant\Desktop\AR GitHub\07-05-2014\FinalTestARGithub\LetterTemplate\"+ txtcode+ ".docx";
FileInfo file = new FileInfo(FullFilePath);
if (file.Exists)
{
ClientScript.RegisterStartupScript(this.GetType(), "disExp", "<script>alert('File Already Exists');</script>");
}
else
{
Response.AppendHeader("Content-Type", "application/vnd.openxmlformats-officedocument.wordprocessingml.document");
Response.AppendHeader("Content-disposition", "inline; filename="+txtcode+".docx");
Response.Write(strBody);
}
}
Here is the code using the CONTENT-TYPE for .DOCX "application/vnd.openxmlformats-officedocument.wordprocessingml.document", the Content is corrupt while opening the file.
Try this to find out what is going on with the file.
I've done this with native word .docx, but not .docx generated in this manner, so it may or may not work.
Make a copy of the saved file, change its extention from .docx to .zip.
Try and open that. We are trying to find a file document.xml, which is normally in the "word" folder.
Open that in a text editor an see if anything is jumping out as wrong or try putting it through an XML validator. VisualStudio should be good enough to show any malformating.
Online XML Validator that might help: http://www.xmlvalidation.com/
The following lines are also suspect:
strBody.Append("<!--[if gte mso 9]>" + "<xml>" + "<w:WordDocument>" + "<w:View>Print</w:View>" + "<w:DoNotOptimizeForBrowser/>" + "</w:WordDocument>" + "</xml>" + "<![endif]-->");
As I'm unsure how word will handle IE conditional comments. Comment out or remove this line and see what happens.
strBody.Append("<style>" + "<!-- /* Style Definitions */" + "#page Section1" + " {size:8.5in 11.0in; " + " margin:1.0in 1.25in 1.0in 1.25in ; " + " mso-header-margin:.5in; " + " mso-footer-margin:.5in; mso-paper-source:0;}" + " div.Section1" + " {page:Section1;}" + "-->" + "</style></head>");
Due to the nested comments. <!-- /* */-->. Perhaps try changing it to: strBody.Append("</head>"); and see if that works.
I am using HttpUtility.HtmlEncode(input) to encode the html script tags to prevent XSS..
the encoding is working but when the content gets dislpalyed on the browser the scripts are running..
here's my code,
<address>" + HttpUtility.HtmlEncode(dr["Street"]) + ", " + HttpUtility.HtmlEncode(dr["City"]) + ", " + HttpUtility.HtmlEncode(dr["State"]) + ", " + dr["ZipCode"] + "</address>"
in the dr["Street"] I am passing "alert('fdsf')"
I'am exporting some data to a .txt file as follows:
String content;
String path=#"e:\coding\";
String name="test.txt";
path+=name;
System.IO.File.Delete(path);
for (i=0;i<row-1;i++)
{
try
{
if (r[i].points.Count() > 2)
{
content = "Route " + (i + 1).ToString() +" Truck_id:"+trk[i].truck_name.ToString()+ " Max_load="+trk[i].capacity.ToString()+ "\n";
System.IO.File.AppendAllText(path, content + Environment.NewLine);
System.IO.File.AppendAllText(path, "Points Load Reached_AT Max_load" + Environment.NewLine);
System.IO.File.AppendAllText(path, "========================================" + Environment.NewLine);
for (int j = 0; j < (r[i].points.Count()); j++)
{
content = r[i].points[j].ToString() + " " + c[r[i].points[j]].load.ToString() +" "+ r[i].time_list[j].ToString()+" "+c[r[i].points[j]].max_load.ToString()+"\n";
System.IO.File.AppendAllText(path, content + Environment.NewLine);
}
content = "Total " + r[i].ld.ToString() + "\n";
System.IO.File.AppendAllText(path, content + Environment.NewLine );
content = "Route Complete: " + r[i].reach_at.ToString();
System.IO.File.AppendAllText(path, content + Environment.NewLine+Environment.NewLine);
}
}
catch (IndexOutOfRangeException e)
{
break;
}
}
As expected the output I get is not properly formatted:
The spaces are causing the text to be jumbled and not arranged. My reputation does'nt allow me to post a screenshot but I guess It can be understood what is happening.
Is there way so that the text is properly formatted neatly column wise without looking jumbled.
If you need a text, you can use tabs:
System.IO.File.AppendAllText(path, "Points\t\tLoad\t\tReached_AT\t\tMax_load" + Environment.NewLine);
// ...
content = r[i].points[j].ToString() + "\t\t " + c[r[i].points[j]].load.ToString() +"\t\t"+ r[i].time_list[j].ToString()+"\t\t"+c[r[i].points[j]].max_load.ToString()+"\n";
Just play with amount of tabs (\t for one, \t\t for two, etc...). Hope it can help.
Another solution would be to use commas:
System.IO.File.AppendAllText(path, "Points,Load,Reached_AT,Max_load" + Environment.NewLine);
and save to CSV-file (comma-separated values). Then you can import the data to Microsoft Excel or to other software.
You can find bunch full of good information on how to format the string contents in the The format item MSDN but for quick answer, an example for your string
content = "Route " + (i + 1).ToString() + " Truck_id:" + trk[i].truck_name.ToString() + " Max_load=" + trk[i].capacity.ToString() + "\n";
If we assume,
i maximum 10 digits,
Truck_name max 45 characters
capacity max 10 digits
content = String.Format("{0,-20}{1,55}{2,20} " + Environment.NewLine, "Route " + (i + 1).ToString(), " Truck_id:" + trk[i].truck_name.ToString(), " Max_load=" + trk[i].capacity.ToString());
I tried to update a page in OneNote with the Microsoft reference :
http://msdn.microsoft.com/en-us/library/office/jj680118.aspx
Here's my problem. When i tried to update my page with the correct ID, it throwed me an error saying : Exception from HRESULT: 0x80042000.
Here is my code :
static void UpdatePageContent()
{
ApplicationClass onApplication = new ApplicationClass();
String strImportXML;
strImportXML = #"<?xml version="+"1.0"+" encoding="+"utf-16"+"?>" +
" <one:Page xmlns:one="+"http://schemas.microsoft.com/office/onenote/12/2004/onenote\""+"" +
"ID=\"{5BE09697-903A-45DD-88D4-8AD301A3D91F}{1}{B0}\">" +
" <one:PageSettings RTL=\"false\" color=\"automatic\">" +
" <one:PageSize>" +
" <one:Automatic/>" +
" </one:PageSize>" +
" <one:RuleLines visible=\"false\"/>" +
" </one:PageSettings>" +
" <one:Title style=\"font-family:Calibri;" +
" font-size:17.0pt\" lang=\"en-US\">" +
" <one:OE alignment=\"left\">" +
" <one:T>" +
" <![CDATA[My Sample Page]]>" +
" </one:T>" +
" </one:OE>" +
" </one:Title>" +
" <one:Outline >" +
" <one:Position x=\"120\" y=\"160\"/>" +
" <one:Size width=\"120\" height=\"15\"/>" +
" <one:OEChildren>" +
" <one:OE alignment=\"left\">" +
" <one:T>" +
" <![CDATA[Sample Text]]>" +
" </one:T>" +
" </one:OE>" +
" </one:OEChildren>" +
" </one:Outline>" +
" </one:Page>";
// Update page content
try
{
onApplication.UpdatePageContent(strImportXML, System.DateTime.MinValue);
}
catch (COMException e)
{
Console.WriteLine("Error Message : " + e.Message);
}
}
I really don't know how to solve this.
Your XML is not OneNote friendly.
Here's a list of error codes:
http://msdn.microsoft.com/en-us/library/office/jj680117.aspx
You can get rid of the first line, as #Sebastian has stated it's malformed anyway and my experience is that OneNote doesn't need it.
Also, remember that you don't need to send the entire page. You just need to send the page's objId and any updated objects. So one outline needs adding then this should also work:
"<one:Page xmlns:one=\"http://schemas.microsoft.com/office/onenote/12/2004/onenote\" +
"ID=\"{5BE09697-903A-45DD-88D4-8AD301A3D91F}{1}{B0}\">" +
" <one:Outline >" +
" <one:Position x=\"120\" y=\"160\"/>" +
" <one:Size width=\"120\" height=\"15\"/>" +
" <one:OEChildren>" +
" <one:OE alignment=\"left\">" +
" <one:T>" +
" <![CDATA[New Text]]>" +
" </one:T>" +
" </one:OE>" +
" </one:OEChildren>" +
" </one:Outline>";
Just this new outline will get added.
If you still get problems (it doesn't complain but the content doesn't update) then check the extra parameters for UpdatePageContent, certainly in the 2013 API one can send a last modified date to check and also there's a parameter to force a local over-write.
There are some issues in the strImportXML string, which causes the update to fail.
Adjust
#"<?xml version="+"1.0"+" encoding="+"utf-16"+"?>" to "<?xml version=\"" + "1.0" + "\" encoding=\"" + "utf-16" + "\"?>"
Add an empty space before the page ID attribute ( + " " + "ID...
instead of + "" + "ID)
Make sure that the page ID is found/ present in your onApplication
hierarchy
Make sure that you reference the COM library with the matching
namespace defined in the one:Page element (e.g. Office 2013/ 15.0
Object Library has another namespace)
I am trying to create a simple appplication that can read from a text file and write to the text file. I have a main form which is shown below, also i have a secondary form also shown below; there are also getter and setter classes for Customers, accounts and Transactions. What i would like to do is search for a Customer based on any data held, but mostly there account number. How would i get it to return the correct customer with ALL of there information. Linked to this it would be good to use the search as a point for inserting say a new account.
Below is the main form for pritning out the customer and getting the information from the file.
//create account if all ok
if (allInputOK)
{
//create Account
Account temp = new Account(tempAccSortCode, tempAccNumber, tempAccNickName, tempAccDate, tempAccCurBal, tempAccOverDraft, tempNumTrans);
//add to array
Form1.accDetails.Add(temp);
//finish up
MessageBox.Show("Success Account added ");
resetForm();
}
}
foreach (Customer c in bankDetails)
{
lstOutput.Items.Add(" ");
lstOutput.Items.Add(c.getCustomerNumber() + " " + c.getCustomerTitle() + " " + c.getFirstName()
+ " " + c.getInitials() + " " + c.getSurname() + " " + c.getDateOfBirth()
+ " " + c.getHouseNameNumber() + " " + c.getStreetName() + " " + c.getArea()
+ " " + c.getCityTown() + " " + c.getCounty() + " " + c.getPostcode()
+ " " + c.getPassword() + " " + c.getNumberAccounts());
foreach (Account a in c.Accounts)
{
lstOutput.Items.Add("\t" + a.getAccSort() + " " + a.getAccNumber() + " " + a.getAccNick() + " " + a.getAccDate()
+ " " + a.getAccCurBal() + " " + a.getAccOverDraft() + " " + a.getAccNumTrans());
foreach (Transaction t in a.Transactions)
{
lstOutput.Items.Add("\t \t" + t.getDate() + " " + t.getType() + " " + t.getDescription() + " " + t.getAmount()
+ " " + t.getBalAfter());
}
}
}
In the above code for adding an account, where is adds it to the class Account and the arraylist i think is incorrect with the rest of the program on the main form which uses an underlying list to store the customer information and there accounts/transactions.
EDIT: the above code snippet shows the adding of a new account, however it doesnt seem to be working as i need to find the correct customer by searching the customer array, and then inserting it into the correct place. The second snippet of code shows the arraylist with the underlying lists connected to each customer.
Where to begin?
Generally, for querying objects you can use Linq to Objects.
Also, I would recommend against using text files to keep track of things. They are fine at first, but quickly grow unwieldy and slow. On top of that, what if your app crashes before you saved? Look into a "light database" like SQLite (yes, that's one 'L').