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.