I'm new to ASP.NET. I am developing a search application in ASP.NET webforms and it will receive from an XMl file. When I type in the textbox like student_name or ID and click submit button it should retrieve the data of the student from the XML like their Honours in a nice format. This is the sample that I have made but it's not working.
aspx file
<body style="height: 171px">
<h1>Student Graduation</h1>
<form runat="server">
Search for
<asp:TextBox ID="SearchTextBox" runat="server" />
<asp:Button ID="SearchButton" Text="Search!" runat="server" OnClick="SearchButton_Click" />
</form>
<div id="student_output" runat="server"></div>
aspx.cs
protected void Page_Load(object sender, EventArgs e)
{
XmlDocument xmlDoc = new XmlDocument();
protected void Page_Load(object sender, EventArgs e)
{
xmlDoc.Load(Server.MapPath("student_list.xml"));
XmlNodeList Students = xmlDoc.DocumentElement.GetElementsByTagName("Student");
String htmlString = "";
for (int i = 0; i < Students.Count; i )
{
XmlNode Student = Students[i];
htmlString = "<h2>" Student.SelectSingleNode("Student_Name").InnerText "</h2>";
htmlString = "<p>";
htmlString = Student.SelectSingleNode("ID").InnerText ", ";
htmlString = Student.SelectSingleNode("Honours").InnerText ", ";
htmlString = Student.SelectSingleNode("Programme").InnerText ", ";
htmlString = Student.SelectSingleNode("Book_Price").InnerText ". ";
htmlString = "</p>";
}
student_output.InnerHtml = htmlString;
}
protected void SearchButton_Click(object sender, EventArgs e)
{
String searchKey = SearchTextBox.Text;
XmlNode Student = xmlDoc.SelectSingleNode("//Student[Student_Name='" searchKey "']");
if (Student != null)
{
string htmlString = "";
htmlString = "<h2>" Student.SelectSingleNode("Student_Name").InnerText "</h2>";
htmlString = "<p>";
htmlString = Student.SelectSingleNode("ID").InnerText ", ";
htmlString = Student.SelectSingleNode("Honours").InnerText ", ";
htmlString = Student.SelectSingleNode("Programme").InnerText ". ";
htmlString = Student.SelectSingleNode("Book_Price").InnerText ". ";
htmlString = "</p>";
student_output.InnerHtml = htmlString;
}
}
}
}
XML File
<Graduate>
<Student>
<ID> 01944422</ID>
<Student_Name>Peter Parker</Student_Name>
<Honours> First Class </Honours>
<Book_Price>Yes</Book_Price>
<Programme>Comp. Science</Programme>
</Student>
<Student>
<ID>01923455</ID>
<Student_Name>Bryan Adam</Student_Name>
<Honours>Second class</Honours>
<Book_Price>No</Book_Price>
<Programme>Mathematics</Programme>
</Student>
<Student>
<ID>01952345</ID>
<Student_Name>Maggie Fong</Student_Name>
<Honours>First class</Honours>
<Book_Price>Yes</Book_Price>
<Programme>Accounting</Programme>
</Student>
<Student>
<ID>01998745</ID>
<Student_Name>Melissa Teh</Student_Name>
<Honours>First class</Honours>
<Book_Price>Yes</Book_Price>
<Programme>Finance</Programme>
</Student>
<Student>
<ID>01899888</ID>
<Student_Name>Ahmad bin Suhail</Student_Name>
<Honours>Second class</Honours>
<Book_Price>No</Book_Price>
<Programme>Engineering</Programme>
</Student>
<Student>
<ID>01900847</ID>
<Student_Name>Lechumanan a/l Vicky</Student_Name>
<Honours>Third class</Honours>
<Book_Price>No</Book_Price>
<Programme>Comp. Science</Programme>
</Student>
<Student>
<ID>04503967</ID>
<Student_Name>Soo Tong Wei</Student_Name>
<Honours>Third class</Honours>
<Book_Price>No</Book_Price>
<Programme>Mathematics</Programme>
</Student>
CodePudding user response:
convert the xml to a dataset, then everything becomes a walk in the park.
So, this markup:
<div>
<h1>Student Graduation</h1>
Search for
<asp:TextBox ID="SearchTextBox" runat="server" />
<asp:Button ID="SearchButton" Text="Search!" runat="server" OnClick="SearchButton_Click"
CssClass="btn" style="margin-left:15px" />
<br />
<asp:GridView ID="GridView1" runat="server" CssClass="table" Width="40%">
</asp:GridView>
</div>
Ok, and then this code:
protected void SearchButton_Click(object sender, EventArgs e)
{
string sFile = @"C:\Test7\sData.txt";
DataSet MyData = new DataSet();
MyData.ReadXml(sFile);
// if blank, then show all data
DataTable MyTable = MyData.Tables[0];
if (SearchTextBox.Text != "")
{
string s = SearchTextBox.Text;
// user entered somthing
// if text, then match on stuent name
if (s.All(char.IsNumber))
{
// number, lets filter by ID
MyTable.DefaultView.RowFilter = "ID = " s;
}
else
{
// search Student name - partial match
MyTable.DefaultView.RowFilter = "Student_Name like '%" s "%'";
}
}
GridView1.DataSource = MyTable;
GridView1.DataBind();
}
So, if we enter nothing, then we show all and see this:
And I used a wild card search for the name - we could I suppose JUST match on say the start, but you can say search for "B", and you get this:
Of course, if you enter a number, then I search by ID, and say this:
And I suppose we should add to the GV a "no data row, say like this:
<asp:GridView ID="GridView1" runat="server" CssClass="table" Width="40%">
<EmptyDataTemplate>
<h2>No data found</h2>
</EmptyDataTemplate>
</asp:GridView>
So, now for a failed search, we would get this: