June 2008


I tried to open a template files for my apps and all I got is an error saying “Illegal characters in path”. So I google my way and found out that you need to insert @ in front of your path. Simple eh? Look at the example:

Error:

FileStream fs;

string path = Application.StartupPath + “\\..\..\templates\template.xml”;
fs = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.ReadWrite);

Correction:

string path = Application.StartupPath + @“\\..\..\templates\template.xml”;

Further explanation you can read from here: http://forums.asp.net/p/1043313/1457497.aspx

Hey, just found a comment on a book mentioning about SharpDevelop. It say’s that SharpDevelop IDE is more better that Microsoft Visual C# 2008. No comment on that because I just used IDE to drag n drop the menu n button. Other than that is pure programming. If you like to have an open source version, I would suggest you used SharpDevelop. Click here to go to the home page.

New C# Book n Rist Supporter

These 2 weeks I was (and still) working on php website. Yap, I need to add few feature (one actually, text editor) to dept website. Previously the content or text info used textarea but now I uses rich text editor. I get some comment from the user that they forget the html tag. This is the main reason why they dont used it. Ya, like all the content need a text editor. So as a responsible web developer, I try to add the feature. It just pain in the a**. I need to change my css file and eventually make some pages distorted. I’m still on the journey to fix this (be sure it will take a long time) as the main corncern is to make sure all the function run smooth. Maybe next time I will share how I manage to insert a rich text editor to our dept website.

I just finish a work asked by someone (confidential) regarding converting pdf document to a text document.  I found few source code on the net explaining how to this. They even share the source code. Great, so I just need to patch here n there to make it nice (ie interface). At first, I was surprise why does the code doesn’t work. I thought it happen because the different version of VS that we used. Then I found my self wrong when I really2 read the code. Silly me.

It’s a console apps where you need to supply the document name when running it. You can’t just run it using run button. So I break the code and made few changes to ease my work. Yey! It’s done and ready to be used. Simple and only work with text. Maybe after this I’ll find a way to properly align the text and include picture rendering. Oh, I want to improve this apps but I got other commitment that need an urgent attention. Never mind. Next time, a better and complete apps will be release.

Sorry I can’t share the code right now. Maybe after I made some alteration I will put it here. So back to VE and try to finish my current project. Oh, selecting from dataGridView. Not easy.

For the past few days, I have been battling with C# to populate my xml file inside a dataGridView. Yes, it is not easy and there is a better way to do this, but this is how I do it right now.

1. Sample.xml

<?xml version=”1.0″ encoding=”utf-8″?>
<project id=”1″ name=”BookStore” domain=”POS”>
<class id=”1″ name=”Customer” relationship=”inheritance”>
<attribute>
<id>1</id>
<name>customerName</name>
<type>string</type>
<behavior>-</behavior>
</attribute>
<attribute>
<id>2</id>
<name>customerAddress</name>
<type>string</type>
<behavior>-</behavior>
</attribute>
<method>
<id>1</id>
<name>Print_Customer_Name</name>
<type>public</type>
<parameter>2</parameter>
</method>
<method>
<id>2</id>
<name>Print_Customer_Address</name>
<type>public</type>
<parameter>2</parameter>
</method>
</class>
<class id=”2″ name=”Books” relationship=”aggregation”>
<attribute>
<id>1</id>
<name>bookTitle</name>
<type>string</type>
<behavior>-</behavior>
</attribute>
<method>
<id>1</id>
<name>Print_Book_Title</name>
<type>private</type>
<parameter>3</parameter>
</method>
</class>
</project>

2. Program.cs – (I did cut a few corner but you know the important part)

projectDoc = new XmlDocument();
projectDoc.Load(“Sample.xml”);
projectDoc.Normalize();

String selected = comboBox2.SelectedItem.ToString();
XPathNavigator nav = ((IXPathNavigable)projectDoc).CreateNavigator();
String expr = “/project/class[@name='"+selected+"']/attribute”;
XPathNodeIterator iter = nav.Select(expr);

XmlNodeList nodes = projectDoc.SelectNodes(expr);

for (int i = 0; i < nodes.Count; i++)
{
XmlNodeList x = nodes[i].ChildNodes;
object[] data = {(object)x.Item(0).InnerText, (object)x.Item(1).InnerText, (object)x.Item(2).InnerText, (object)x.Item(3).InnerText};
dataGridView1.Rows.Add(data);
}

Bang of Death (BoD)

These are the list of errors that I have found today. Some got solve, other just bug me like hell:

  1. Unrecognized Version
    • Description: I send my c# project to someone else and they can’t open it.
    • Solve – Please check your VS@VE version and Framework. Open your solution file and see the 2nd line. It mention about the distribution of the project.
  2. XMLException: Root Element Missing
    • Description: I can’t reload my xml file into a different method
    • Solve – Just declare in once.

So thats for today. Next time, how I solve DataGridView binding..

Ait, I was normally asked by my colleague regarding visual studio and where they can get the free version aka pirate copy. Normally I would suggest a shop at padang or mines or better yet if you have time, just went to lowyat. But that is the last option that I would give it to them. I will asked what kind of usage that they will be expecting from VS. As globally known, VS requires a lot (lot2) resource from your pc (in my case, notebook). If you have a pc with a little resource (yes, you install anti-virus, anti-spyware, massive background program etc) then you will experience a slow motion world in VS. Please don’t blame Bill Gates for this because they do mention the basic requirement at the back of your cd or dvd case. My mistake, you bought the pirate version and they change the cd case. Let’s go on.

How can you avoid all this trouble? I would suggest that you understand your need and requirement. Yes, VS have a lot of functionalities and feature that would help your development but do you really3 need it? Maybe we are unaware that we actually used only 40% of ms word functionalities. Yap, never though about eh? Do you ever used auto toc? indexing? And many other function that existed but fade by lack of knowledge. If you just need a simple drag n drop function, code editing space, compile and run function, then Visual Express is more than enough. MS also throw away for free sql server version if you need database. See, VE is good for student and night programmer like me (only I’m doing it by day) to do programming. For me, VS is for company and organization that need a faster, robust and secure development. It also help you coordinate among team member. Neat.

If you’re just one man show for one short term goal. Please don’t waste your mind n time to use VS. You need to learn more also. Too many button and icon. I don’t really work with too many function (simple minded guy). This is why I don’t play final fantasy. I played tekken, street fighter and go action game. Just hit the button and something will hit and be hit. Hehehehe…. So, final word. Pick the tool based on your need. Don’t over do it. Make it simple and easy. Tq

Erk, this two days I have been squishing my brain on how to dynamically assign value to my array. Yes, it just not enough after thinking on creating array that suit my data structure, now how do I get it in. I can do the old fashion and non-dynamic way but it’s not enough. Why? This is a prototype and it works fine. What happen when the real production start to come out? The data wont stay static forever and few changes must be made (I’m forecasting based on the seriousness of the owner) to complete this apps.

Well, it struck me to used recursive method on the array but I cant clone the node list. Thinking of using XPathQuery but it just start to eerie me back. For those who like a ref on XPath, you can read this two article:

  1. XQuery by W3School (Duh!)
  2. How to Query in C# by Big M (Do figure the headache eh mate?)

I’m referring to this at the moment and maybe I’ll add more just to share with all. Oh, still struggle with Pdf to Text Project. Man power is insufficient here. Help me!!!

Huhuhu, setelah 3 hari 4 malam berfikir, maka pada hari ini aku berjaya memanipulasi array didalam c# untuk kegunaan satu projek. Masalah utama dalam projek ini ialah apabila aku memerlukan beberapa class array untuk menjadi variable didalam class yang lain. Puas berfikir bagaimana membuat setter dan getter method untuk array tersebut.

Seingat aku, didalam version vc# yang lepas terdapat function yang secara automatik membuat setter dan getter method setelah kita mengisytiharkan variable tersebut. Didalam version vc# yang baru ni aku kurang menjumpainya. Mungkin ada tapi disebabkan aku begitu tensen mencari nota yang berkaitan dengan array, aku biarkan ia ketepi. Bukan susah nak buat setter dan getter method tapi amat susah (pada aku) jika ia melibatkan array. Pening..

Dibawah ialah contoh yang telah berjaya aku buat. Komen tidak disertakan kerana aku malas :) )

1. Class1.cs (Base class)

class Class1
{
private int t1;
private int t2;

public Class1()
{
t1 = 0;
t2 = 0;
}

public Class1(int t1, int t2)
{
this.t1 = t1;
this.t2 = t2;
}

public int T1
{
get { return t1; }
set { t1 = value; }
}

public int T2
{
get { return t2; }
set { t2 = value; }
}
}

2. Class2.cs (Calling Class)

class Class2
{
private int t3;
private Class1 a1;
private Class1[] a2;

public Class2()
{
t3 = 0;
a1 = new Class1();
}

public Class2(int t3)
{
this.t3 = t3;
a1 = new Class1();
}

public Class2(int t3, int bil)
{
this.t3 = t3;
this.a2 = new Class1[bil];

for (int i = 0; i < bil; i++)
this.a2[i] = new Class1();
}

public int T3
{
get { return t3; }
set { t3 = value; }
}

public Class1 A1
{
get { return a1; }
set { a1.T1 = value.T1; a1.T2 = value.T2; }
}

public Class1 A2(int i)
{
return a2[i];
}

public void A2(int i, int v1, int v2)
{
this.a2[i].T1 = v1;
this.a2[i].T2 = v2;
}
}

3. Program.cs (Run Class)

class Program
{
static void Main(string[] args)
{
Class1 m1 = new Class1();

m1.T1 = 5;
m1.T2 = 10;

Console.WriteLine(m1.T1.ToString());
Console.WriteLine(m1.T2.ToString());

Class2 m2 = new Class2();

m2.T3 = 15;
m2.A1.T1 = 6;
m2.A1.T2 = 11;

Console.WriteLine(m2.T3.ToString());
Console.WriteLine(m2.A1.T1.ToString());
Console.WriteLine(m2.A1.T2.ToString());

int bil = 2;

Class2 m3 = new Class2(0,bil);

for (int i = 0; i < bil; i++)
m3.A2(i, 21+i, 22+i);
m3.T3 = 20;

for (int i = 0; i < bil; i++)
Console.WriteLine(m3.A2(i).T1.ToString() + ” ” + m3.A2(i).T2.ToString());
Console.WriteLine(m3.T3.ToString());

Console.ReadLine();
}
}

Console.WriteLine(“Hello World”);

Hehehe… itu adalah perkataan keramat sebenarnya bila kita mula mempelajari pengaturcaraan. Jadi hari ini bermula semula laman web pengaturcaraan yang dah lama ditutup. Aku akan cuba untuk menampal beberapa contoh penyelesaian yang aku telah berjaya dapat dan tidak dapat. Diharap kita semua dpt berkongsi ilmu :D