http://sufikatha.com
Nishanth's Blog | C#

ChartDirector font problem with chinese/japanese/korean fonts

by Nishanth 12. May 2009 00:11

I was working on a web app which supported 20+ languages across the world. One report which we developed was using ChartDirector (http://www.advsofteng.com/gallery.html) in verdana font. After development we found that ChartDirector is not displaying chinese/japanese/korean characters. it was displaying  junk boxes instead. On Investigation, it was found that the issue was with  Verdana as Verdana does not support Unicode characters for japanese, chinese , korean etc. I searched in the net for a solution and found in the ChartDirector forum that the tech support of ChartDirector is advising people to use a font called "MingLiU" (which I don't think is free). I didn't like the idea of installing seperate fonts for each locale that we support. Just then I thought why don't we use a common unicode font which is available with Windows. This thought took me to "Arial Unicode MS" which is the best bet for displaying all unicode characters and it was free with Windows XP/ Windows 2003. I replaced verdana with "Arial Unicode MS" in the code and it worked perfectly!!!

sample code:     bc.xAxis().setLabelStyle("Arial Unicode MS", 9);

 

 

Page_Load method being called twice (randomly) in ASP.NET web application

by Nishanth 2. December 2008 21:03

First lets look in to the Architecture of this web application which I was working on:

The user will be redirected to a static HTML landing pagefrom where the user selects products using radio buttons and hits an htmlbutton which is coded as follows: 

<input name="image" type="image" src="../Images/buynow.jpg" class="buynowbutton_and"  id="BuyNow_0" onclick="return Redirect(this);"/>

As you can see, when the button is clicked, the javascriptmethod Redirect() is being called.

In this method, a url is constructed using the selected productids and redirected to the shopping cart page.

  document.forms[0].action= url;

 document.forms[0].submit();

 

Now the missing piece is that we should return a “false”after the redirection statement. This will prevent the page from posting  back again(which we don’t want to happen) aswe already redirected the page.

 Otherwise the browsermight get posted back ‘again’(This is quite random and depends on browser. Wehad issues with IE 7 ) and 2 ‘post backs’ will happen on the same page and itwill cause Page_Load to execute twice  ina multithreaded manner sharing the same session variables. This is a dangeroussituation. It leads to unexpected behavior of the ASP.NET application and itaffects the output.

In my case there was an if else loop in the Redirect()method which was causing the issue.

If(condition){

Do something;

//Missed the return false here.

}else

{return false;}

As you saw above the return false was not in if block, itsonly in else block. This code used to work initially and suddenly started tocreate issues.  

 

If your application has a client side redirect,then make sure that you disable the post back  returning a false in the onclick event toprevent the second post back. From another perspective, if you have two ormultiple unexpected Page_Load events happening on your aspx page and you are clueless(just like me initiallyJ), check whether you are returning the false inyour javascript after its execution(make specific checks on conditional statementsin the JS which might prevent the ‘false’ to be returned 

Solution File missing in VS 2008 / 2005 ??

by Nishanth 17. November 2008 22:43

Are you searching for the missing solution file in your solution explorer of Visual Studio 2005 or Visual Studio 2008??

If yes, go to Tools - > Options- > Projects and Solutions-> General . On the Right side, check Always show solution.

Click OK. And you are done. Now You can see your Solution file in the Solution explorer.. Happy Coding!!! 

 

Currently rated 5.0 by 1 people

  • Currently 5/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

.NET | C# | Visual Studio

Validating an XML File using XSD in .NET .20

by Nishanth 2. November 2008 07:04

Recently I had an issue with .NET 2.0 XML validation. Lets see whats the difference in .NET 2.0 from .NET 1.1 

Below given is the.NET 1.1 code for validating an XML File using an XSD File.

.NET 1.1 Code

//.NET1.1 code
///
/// Methode to validate XML File///
///

This method expects input XML as string
///
Path to schema file
/// true if xml is validated else false
private bool ValidateXmlUsingXsd(string XmlData,String SchemaPath)
{
XmlValidatingReader v = new XmlValidatingReader(XmlData, XmlNodeType.Document, null);
v.ValidationType = ValidationType.Schema;
v.ValidationEventHandler +=
new ValidationEventHandler(MyValidationEventHandler); 
 

while (v.Read())
{
// Can add code here to process the content.
}
v.Close();

return isValid;

}

///

public static void MyValidationEventHandler(object sender,
ValidationEventArgs args)
{
//these two variables should be initialized as class level variables
isValid = false;
errorMessage = “Validation event\n” + args.Message;///
/// Method to get XML in a string from an XML file
///
///

}

///
private string GetStringFromXML(string fileName)
{
StreamReader rd = new StreamReader(fileName);
string str = rd.ReadToEnd();
rd.Close();
return str;
}

Calling the method :
bool valid = ValidateXmlUsingXsd(str, txtXSD.Text);

 

There are some changes in the .NET 2.0 code for XML Validation .
XmlValidatingReader is marked as obsolete. Need to use XMLReader.Create() using XmlReaderSettngs instead

There are some behavioral changes between validation using the XmlReaderSettings and XmlSchemaSet classes and validation using the XmlValidatingReader class.

The XmlReaderSettings and XmlSchemaSet classes do not support XML-Data Reduced (XDR) schema validation.

The most important difference I found out is that to do XML data validation using a schema, settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
Flag must be enabled. Otherwise the Schema check error will not be displayed.

//.NET2.0 code
private bool ValidateXmlUsingXsd2(string XmlData,String SchemaPath)
{
XmlReaderSettings settings = new XmlReaderSettings();
settings.ValidationType = ValidationType.Schema;
settings.Schemas.Add(null, SchemaPath);
settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings;
settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
More about XmlSchemaValidationFlags Enumeration

StringReader xmlStream = new StringReader(XmlData);
XmlReader reader = XmlReader.Create(xmlStream, settings);
while (reader.Read()) ;

return isValid;
}

private static void ValidationCallBack(object sender, ValidationEventArgs e)
{
isValid = false;
errorMessage = “Validation Error: ” + e.Message;
}

 


Member name
Description
AllowXmlAttributes
Allow xml:* attributes even if they are not defined in the schema. The attributes will be validated based on their data type.
None
Do not process identity constraints, inline schemas, schema location hints, or report schema validation warnings.
ProcessIdentityConstraints
Process identity constraints (xs:ID, xs:IDREF, xs:key, xs:keyref, xs:unique) encountered during validation.
ProcessInlineSchema
Process inline schemas encountered during validation.
ProcessSchemaLocation
Process schema location hints (xsi:schemaLocation, xsi:noNamespaceSchemaLocation) encountered during validation.
ReportValidationWarnings
Report schema validation warnings encountered during validation.

In a nutshell, Always set settings.ValidationFlags = XmlSchemaValidationFlags.ReportValidationWarnings; when a schema validation is required for the XML in .NET 2.0

Be the first to rate this post

  • Currently 0/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , ,

.NET | C# | XML

Truncation of fields when CSV file is read using ADO.NET

by Nishanth 2. November 2008 06:57

I encountered a major problem with ADO.NET CSV reader in one of the projects where CSV File Import played a major role.
When a field in the CSV file with a “–“(hyphen) is read, the characters before the “–“are discarded. For example the model F-150 is read as –150 and T-Bird in the model field is not being read. And some of the values were missing in some of the fields.
Given below is the code I used :

 string strConnString = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” + folderName + “;Extended Properties=\”text;HDR=Yes; FMT=Delimited; IMEX=1\””;
string sqlSelect = “select * from [" + fileName + "]“;
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(strConnString.Trim());
conn.Open();
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(sqlSelect, conn);
DataSet ds = new DataSet();

ds.ReadXmlSchema(Server.MapPath(”xmlschema.xsd”));

ds.EnforceConstraints = false; 
F-100 reads as -100
K-100 reads as -100
S-100 reads as -100
100-F reads as -100
100-K reads as -100
100-S reads as -100
100$F reads as 100
100$K reads as 100
100$S reads as 100
F100 reads as 100
K100 reads as 100
S100 reads as 100
F\100 reads as 100
K\100 reads as 100
S\100 reads as 100
F.100 reads as .1
K.100 reads as .1
S.100 reads as .1
K-.\\$$$.\\1 reads as -0.1
K\\-22..$$\\21 reads as -22.221
-$\.FSK1 reads as -0.1
This happens only wen there is only one row in thecsv file or more than half the values in a column is having the avove specifiedvalues

 

string strConnString = “Provider=Microsoft.Jet.OLEDB.4.0;Data Source=” + folderName + “;Extended Properties=\”text;HDR=Yes; FMT=Delimited\””;
string sqlSelect = “select * from [" + fileName + "]“;
System.Data.OleDb.OleDbConnection conn = new System.Data.OleDb.OleDbConnection(
strConnString.Trim());
conn.Open();
System.Data.OleDb.OleDbDataAdapter adapter = new System.Data.OleDb.OleDbDataAdapter(sqlSelect, conn);
DataSet ds = new DataSet();
adapter.Fill(ds, “Inventory”);
return ds.Tables[0];

Samir found a solution, to add a schema file which identifies all fields in the csv file as string values.
And call DataSet.ReadXMLSchema() method to attach the schema to the DataSet. Also the schema constraints are not enforced .

modified code looks like this..

 

 

adapter.Fill(ds, “Inventory”);
return ds.Tables[0];

Even after using this code values like “F-150″ was read as -150 if there is only one row in the csv file.

May be because ADO.NET does some internal calculation to treat F as floating point or something.. The fun part is that f-150 is read correctly.. problem is with capital letters  . 
So we ended up using a custom third party csv reader . 
Moral of the story… never use ADO.NET csv reader… Always go for a custom CSV parser or a third party library.

Currently rated 4.0 by 1 people

  • Currently 4/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , ,

.NET | C#

All Good Websites

Powered by BlogEngine.NET 1.4.5.0
Theme by Mads Kristensen

About Me

Nishanth Nair  

 A die hard fan of Rise of Nations... I write few softwares for McAfee Inc. for a living.

 

Portfolio:
http://Necab.org
http://SufiKatha.com
http://DeepaRecipes.com
http://ManipalFoundation.in
http://NetraArts.com
http‌://EnWebTech.com
Contact : Nair.Nishanth@Gmail.com 

All entries in this blog are my opinion and don't necessarily reflect the opinion of my employer.

Tag cloud

The World This Second

free counters