Archive for January, 2009

Simple Program to create XML from table in database, .NET

Hm..after I think for a while, i decide to post this simple program..
But I just want to share my knowledge. Actually I got this from internet too..but I just hope, just hope that I can help someone who really want to make xml from database.

Okey, If I want to make xml from database and show it in GridView (in case for web application /asp.net), these are the steps..

  1. Create your new Web site application and then open Default.aspx.cs or double click on Default.aspx. On formLoad type this code (better you type so you can more understand it):
    Include these library:

    using System.Data.SqlClient;
    using System.Xml;
    using System.Xml.Serialization;
    using System.Collections.Generic;
    using System.IO;

    protected void Page_Load(object sender, EventArgs e) {
    DataSet ds = new  DataSet();
    String constr = “Data Source = (local); Initial Catalog=[Choose ur db name] User ID = sa;
    Password = [ur db passwd]“
    ;
    SqlConnection newConn =  new SqlConnection(constr);
    String strcmd = “select * from [ur table]“;
    SqlDataAdapter da = new SqlDataAdapter(strcmd, newConn);
    da.Fill(ds, “[ur table]“);
    ds.WriteXml(MapPath(“FileName.xml”));
    }

  2. Then create the interface by dragging GridView and a Button from toolbox to Default.aspx.
  3. Double click on your button (it will show file Default.aspx.cs) and type this code

    protected void Button1_Click(object sender, EventArgs e) {
    XmlDataDocument doc = null;
    DataSet ds = new DataSet();
    string path =  Path.Combine(Request.PhysicalApplicationPath, “FileName.xml”);
    ds.ReadXml(path);
    doc = new XmlDataDocument(ds);
    GridView1.DataSource = ds;
    GridView1.DataBind();
    }

  4. Build your web site and then press the button, the data will show in the GridView. Good Luck!

If  you want to use Windows Application, it’s very simple. You just need to change the code inside the Button event. Look at this code

  1. Type this code inside button1 event

    private void button1_Click(object sender, EventArgs e) {
    XmlDataDocument doc = null;
    DataSet ds = new DataSet();
    ds.ReadXml(Application.StartupPath + @”\FileName.xml”);
    doc = new XmlDataDocument(ds);
    dataGridView1.DataSource = ds.Table[0];
    }

  2. That’s all, but don’t forget to move your xml file to bin\Debug\FileName.xml. Good Luck for you guys.

Remember this is just one of the way to create xml from database and show it in GridView, there are so many ways. I hope these simple code can help you.

Comments (12)

A Poem from a Father to his child

Father and Son

Father and Son

TO MY CHILD

Just for this morning,
I am going to smile when I see your face and laugh when I feel like crying.
Just for this morning, I will let you choose what you want to wear,
and smile and say how perfect it is.
Just for this morning, I am going to step over the laundry and pick you up
and take you to the park to play.
Just for this morning, I will leave the dishes in the sink,
and let you teach me how to put that puzzle of yours together.


Just for this afternoon, I will unplug the telephone and keep the computer off,
and sit with you in the backyard and blow bubbles.
Just for this afternoon, I will not yell once, not even a tiny grumble when you scream
and whine for the ice cream truck, and I will buy you one if he comes by.
Just for this afternoon, I won’t worry about what you are going to be when you grow up,
or second guess every decision I have made where you are concerned.
Just for this afternoon, I will let you help me bake cookies,
and I won’t stand over you trying to fix them.
Just for this afternoon, I will take us to McDonald’s and buy us both a Happy Meal
so you can have both toys.


Just for this evening, I will hold you in my arms and tell you a story about how you were born
and how much I love you.
Just for this evening, I will let you splash in the tub and not get angry.
Just for this evening, I will let you stay up late while we sit on the porch and count all the stars.
Just for this evening, I will snuggle beside you for hours, and miss my favourite TV shows.
Just for this evening when I run my finger through your hair as you pray,
I will simply be grateful that God has given me the greatest gift ever given.
I will think about the mothers and fathers who are searching for their missing children,
the mothers and fathers who are visiting their children’s graves instead of their bedrooms.
The mothers and fathers who are in hospital rooms watching their children suffer senselessly
and screaming inside that little body


And when I kiss you goodnight I will hold you a little tighter, a little longer.
It is then, that I will thank God for you, and ask him for nothing, except one more day… …

Leave a Comment

Pusing seven around

Gubrakkkkk….. opss. apaan tuh. ?
Yah biasa…mata udah loyo..kepala udah oleng, jari-jari udah kaku…lengen rasanya mau copot…(mau mate aja sekalian sebernya..)

Asli cape banget otakku mikir beberapa minggu belakangan ini. Ugh..habis dari rumah, aku harus mikir keras lagi di kampus.
Untunglah malam ini sedikit tugasku udah selesai..(Tp yang lain udah nunggu lagi..Oh Em Ji).

Betul-betul pusing seven around dah…
Sebenarnya udah niatin pengen killing myself..cmn karena kerjaan belum kelar..jadi batal mlulu tuh rencana..
Tapi keknya Jesus juga belum kasih ijin tuh buat aku koit.makanya aku kasih banyak kerjaan….biar lalap…

Hehhehe..semoga aja lalap terus..hehe (Keknya aku perlu check up ke RSJ deh *bingung…* )

But whateverlah..yang jelas..sebenarnya aku cuman mau numpahin semua kotoran yang ada di kepala ini..
paling tidak sedikt berkurang nantinya..
semoga aja berhasill…

Mungkin karena dosa-dosaku nya ini semuanya..
aduh memang aku jahat kali sih..

Tuhan ampuni aku ya..
Teman-teman aku minta maaf, siapa pun diantara kalian yang udah pernah aku sakitin..aku minta maaf..
I didn’t mean it..

Thanks a lot..

Night

Comments (3)