
using System;
using System.Data;
using System.Drawing;
using System.Windows.Forms;
using Microsoft.VisualBasic;
namespace xmlapp
{
public partial class studdata : Form
{
public studdata()
{
InitializeComponent();
}
DataSet ds;
string dir;
int rno = 0;
private void studdata_Load(object sender, EventArgs e)
{
ds = new DataSet();
searchbtn.Enabled = firstbtn.Enabled = prevbtn.Enabled = nextbtn.Enabled = lastbtn.Enabled = clearbtn.Enabled = false;
}
private void loadbtn_Click(object sender, EventArgs e)
{
try
{
searchbtn.Enabled = firstbtn.Enabled = prevbtn.Enabled = nextbtn.Enabled = lastbtn.Enabled = clearbtn.Enabled = false;
ds.Clear(); // for clearing grid view
dataGridView1.DataSource = null;
sno_txtbx.Text = sname_txtbx.Text = course_txtbx.Text = fee_txtbx.Text = "";
pictureBox1.Image = null;
openFileDialog1.Filter = "xml|*.xml|all files|*.*";
DialogResult res = openFileDialog1.ShowDialog();
if (res == DialogResult.OK)
{
string fpath = openFileDialog1.FileName;
ds.ReadXml(fpath);
// setting primary key inorder to search a record using finding method
ds.Tables[0].Constraints.Add("pk_sno", ds.Tables[0].Columns[0], true);
dataGridView1.DataSource = ds.Tables[0];
dir = fpath.Substring(0, fpath.LastIndexOf("\\") + 1);
showdata();
}
}
catch
{
MessageBox.Show("invalid input");
}
}
void showdata()
{
if (ds.Tables[0].Rows.Count > 0)
{
searchbtn.Enabled = firstbtn.Enabled = prevbtn.Enabled = nextbtn.Enabled = lastbtn.Enabled = clearbtn.Enabled = true;
pictureBox1.Image = null;
sno_txtbx.Text = ds.Tables[0].Rows[rno][0].ToString();
sname_txtbx.Text = ds.Tables[0].Rows[rno][1].ToString();
course_txtbx.Text = ds.Tables[0].Rows[rno][2].ToString();
fee_txtbx.Text = ds.Tables[0].Rows[rno][3].ToString();
pictureBox1.ImageLocation = dir + ds.Tables[0].Rows[rno][4].ToString();
}
else
MessageBox.Show("No records");
}
private void searchbtn_Click(object sender, EventArgs e)
{
int n = Convert.ToInt32(Interaction.InputBox("Enter sno to search:", "Search", "10", 200, 200));
// searching using find method
DataRow drow = ds.Tables[0].Rows.Find(n);
if (drow != null)
{
rno = ds.Tables[0].Rows.IndexOf(drow);
sno_txtbx.Text = drow[0].ToString();
sname_txtbx.Text = drow[1].ToString();
course_txtbx.Text = drow[2].ToString();
fee_txtbx.Text = drow[3].ToString();
pictureBox1.ImageLocation = dir + drow[4];
}
else
MessageBox.Show("record not found");
}
private void clearbtn_Click(object sender, EventArgs e)
{
sno_txtbx.Text = sname_txtbx.Text = course_txtbx.Text = fee_txtbx.Text = "";
pictureBox1.Image = null;
}
private void firstbtn_Click(object sender, EventArgs e)
{
rno = 0;
showdata();
}
private void prevbtn_Click(object sender, EventArgs e)
{
if (rno > 0)
{
rno--;
showdata();
}
else
{
MessageBox.Show("first record");
}
}
private void nextbtn_Click(object sender, EventArgs e)
{
if (rno < ds.Tables[0].Rows.Count - 1)
{
rno++;
showdata();
}
else
{
MessageBox.Show("LastRecord");
}
}
private void lastbtn_Click(object sender, EventArgs e)
{
rno = ds.Tables[0].Rows.Count - 1;
showdata();
}
private void exitbtn_Click(object sender, EventArgs e)
{
this.Close();
}
}
}
Note. Goto Project Menu.
// Add Reference -> select 'Microsoft.VisualBasic' from .NET tab; include namespace 'using Microsoft.VisualBasic' (To get input box)
Add OpenFileDialog control to the form, which appears below the form
Add PictureBox Control and change properties as follows.
BorderStyle=Fixed3D; SizeMode=StrechImage
Example for Creating an XML file
Open Notepad and type the following & save it with extension .xml.
<students>
<student>
<sno>10</sno>
<sname>Prashanth</sname>
<course>dotnet</course>
<fee>3500</fee>
<photo>prash.jpg</photo>
</student>
<student>
<sno>20</sno>
<sname>Satyapal</sname>
<course>java</course>
<fee>3000</fee>
<photo>satya.jpg</photo>
</student>
<student>
<sno>30</sno>
<sname>Mahender</sname>
<course>php</course>
<fee>2500</fee>
<photo>mahi.jpg</photo>
</student>
</students>
Note. XML file and respective jpeg images should be in the same folder.
To execute the program click loadxml button and select xml file.

Gohil JayendrasinhPosted May 14, 2012, 2:27 AM
Good One
thiago costaPosted Sep 26, 2011, 7:50 PM
hey man, thank you for providing us with this awesome code. I don't know much, but I usually take for ever to do this kind of thing, now its easy to learn from what is more likely to be obvious.
Arsenio MadrazoPosted Mar 29, 2011, 5:03 AM
Hi Prashanth First thnaks you for this great article. I've read it and tried to adapt to my own xml schema. This is a fragment of my xml file("Carrusel.xml") <Datos> <dato img_Url="images/galeria/foto_1.png" page_Url="http://www.atesa.es/webcorporativa/paginas/ofertas1.aspx" /> . . </Datos> And this is the code to write(to intend) a new node. void InsertItems(String sImagenUrl, String sPaginaUrl) { String rootPath = Server.MapPath("~"); String archivoXmlPath = Path.Combine(rootPath, "Xml//Carrusel.xml"); XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(archivoXmlPath); XmlElement dato = xmlDoc.CreateElement("dato"); XmlAttribute imgenUrl = xmlDoc.CreateAttribute("img_Url"); XmlAttribute paginaUrl = xmlDoc.CreateAttribute("page_Url"); imgenUrl.InnerText = sImagenUrl; paginaUrl.InnerText = sPaginaUrl; dato.AppendChild("dato"); imgenUrl.InsertAfter("img_Url", "dato"); paginaUrl.InsertAfter("page_Url", "dato"); xmlDoc.DocumentElement.AppendChild("dato"); xmlDoc.Save(archivoXmlPath); } But I received errors. Where is the bug?. Thanks in advanced. Arsenio
angelo sucksPosted Oct 12, 2010, 7:22 PM
great code.. but can i add adding,edit,and del button..?whats the code..?thx b4