Hello,
I've already done the work load.. but its not what i wanted
The thing is that, the value input from the textboxes are stored in the xml file
I have this Xml file structure:
but in my output, i'm getting this:
Here's my Interface:
Here's my Code Behind in C#:
Please help me to get this right
Thank You
I've already done the work load.. but its not what i wanted
The thing is that, the value input from the textboxes are stored in the xml file
I have this Xml file structure:
<Contacts xmlns:xsi='http://www.w3.org/2001/XMLSchema-instance'><Contact><FirstName>sample</FirstName><MidName>sample</MidName><LastName>sample</LastName><Age>20</Age><Address><City>sample</City><Street>sample</Street><State>sample</State><Country>sample</Country></Address><Phone><Mobile>1234567</Mobile><Home>7654321</Home><Office>7894561</Office></Phone><Company><Name>sample</Name><Department>IT Technician</Department></Company><Email>xyz@xxx.com</Email></Contact></Contacts>
but in my output, i'm getting this:
<Contacts><contact><FirstName>sample</FirstName><MidName>sample</MidName><LastName>sample</LastName><Age>20</Age><City>sample</City><Street>sample</Street><State>sample</State><Country>sample</Country><Mobile>1234567</Mobile><Home>7894561</Home><Office>1122334</Office><Company>sample</Company><Name>sample</Name><Department>IT Technician</Department><Email>xyz@xxx.com</Email></contact><Contacts>
Here's my Interface:
<asp:GridView ID="gvProducts" AutoGenerateColumns="False" CssClass="GridViewStyle" runat="server" OnRowCommand="gvProducts_RowCommand" OnRowEditing="gvProducts_RowEditing" OnRowCancelingEdit="gvProducts_RowCancelingEdit" OnRowUpdating="gvProducts_RowUpdating" OnRowDeleting="gvProducts_RowDeleting" AllowPaging="True" AllowSorting="True" ><RowStyle CssClass="RowStyle" /><FooterStyle CssClass="RowStyle" /><SelectedRowStyle CssClass="SelectedRowStyle" /><HeaderStyle CssClass="HeaderStyle" /><AlternatingRowStyle CssClass="AltRowStyle" /><Columns><asp:TemplateField HeaderText="First Name"><ItemTemplate><%#Eval("FirstName")%></ItemTemplate><FooterTemplate><asp:TextBox ID="txtNewFname" runat="Server" /></FooterTemplate><EditItemTemplate><asp:TextBox ID="txtFname" Text='<%#Eval("FirstName")%>' runat="server" /></EditItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Mid Name"><ItemTemplate><%#Eval("MidName")%></ItemTemplate><FooterTemplate><asp:TextBox ID="txtNewMname" runat="Server" /></FooterTemplate><EditItemTemplate><asp:TextBox ID="txtMname" Text='<%#Eval("MidName")%>' runat="server" /></EditItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Last Name"><ItemTemplate><%#Eval("LastName")%></ItemTemplate><EditItemTemplate><asp:TextBox ID="txtLname" Text='<%#Eval("LastName")%>' runat="server" /></EditItemTemplate><FooterTemplate><asp:TextBox ID="txtNewLname" runat="server" /></FooterTemplate></asp:TemplateField><asp:TemplateField HeaderText="Age"><ItemTemplate><%#Eval("Age")%></ItemTemplate><EditItemTemplate><asp:TextBox ID="txtAge" Text='<%#Eval("Age")%>' runat="server" /></EditItemTemplate><FooterTemplate><asp:TextBox ID="txtNewAge" runat="server" /></FooterTemplate></asp:TemplateField><asp:TemplateField HeaderText="City"><ItemTemplate><%#Eval("City")%></ItemTemplate><FooterTemplate><asp:TextBox ID="txtNewCity" runat="Server" /></FooterTemplate><EditItemTemplate><asp:TextBox ID="txtCity" Text='<%#Eval("City")%>' runat="server" /></EditItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Street"><ItemTemplate><%#Eval("Street")%></ItemTemplate><FooterTemplate><asp:TextBox ID="txtNewStreet" runat="Server" /></FooterTemplate><EditItemTemplate><asp:TextBox ID="txtStreet" Text='<%#Eval("Street")%>' runat="server" /></EditItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="State"><ItemTemplate><%#Eval("State")%></ItemTemplate><FooterTemplate><asp:TextBox ID="txtNewState" runat="Server" /></FooterTemplate><EditItemTemplate><asp:TextBox ID="txtState" Text='<%#Eval("State")%>' runat="server" /></EditItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Country"><ItemTemplate><%#Eval("Country")%></ItemTemplate><FooterTemplate><asp:TextBox ID="txtNewCountry" runat="Server" /></FooterTemplate><EditItemTemplate><asp:TextBox ID="txtCountry" Text='<%#Eval("Country")%>' runat="server" /></EditItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Mobile No."><ItemTemplate><%#Eval("Mobile")%></ItemTemplate><FooterTemplate><asp:TextBox ID="txtNewMobile" runat="Server" /></FooterTemplate><EditItemTemplate><asp:TextBox ID="txtMobile" Text='<%#Eval("Mobile")%>' runat="server" /></EditItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Home No."><ItemTemplate><%#Eval("Home")%></ItemTemplate><FooterTemplate><asp:TextBox ID="txtNewHome" runat="Server" /></FooterTemplate><EditItemTemplate><asp:TextBox ID="txtHome" Text='<%#Eval("Home")%>' runat="server" /></EditItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Office No."><ItemTemplate><%#Eval("Office")%></ItemTemplate><FooterTemplate><asp:TextBox ID="txtNewOffice" runat="Server" /></FooterTemplate><EditItemTemplate><asp:TextBox ID="txtOffice" Text='<%#Eval("Office")%>' runat="server" /></EditItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Company"><ItemTemplate><%#Eval("Company")%></ItemTemplate><FooterTemplate><asp:TextBox ID="txtNewCompany" runat="Server" /></FooterTemplate><EditItemTemplate><asp:TextBox ID="txtCompany" Text='<%#Eval("Company")%>' runat="server" /></EditItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Company Name"><ItemTemplate><%#Eval("Name")%></ItemTemplate><FooterTemplate><asp:TextBox ID="txtNewName" runat="Server" /></FooterTemplate><EditItemTemplate><asp:TextBox ID="txtName" Text='<%#Eval("Name")%>' runat="server" /></EditItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="Department"><ItemTemplate><%#Eval("Department")%></ItemTemplate><FooterTemplate><asp:TextBox ID="txtNewDepartment" runat="Server" /></FooterTemplate><EditItemTemplate><asp:TextBox ID="txtDepartment" Text='<%#Eval("Department")%>' runat="server" /></EditItemTemplate></asp:TemplateField><asp:TemplateField HeaderText="E-mail Address"><ItemTemplate><%#Eval("Email")%></ItemTemplate><FooterTemplate><asp:TextBox ID="txtNewEmail" runat="Server" /></FooterTemplate><EditItemTemplate><asp:TextBox ID="txtEmail" Text='<%#Eval("Email")%>' runat="server" /></EditItemTemplate></asp:TemplateField><asp:TemplateField><ItemTemplate><asp:LinkButton ID="btnedit" runat="server" CommandName="Edit" Text="Edit" /></ItemTemplate><EditItemTemplate><asp:LinkButton ID="btnupdate" runat="server" CommandName="Update" Text="Update" /><asp:LinkButton ID="btncancel" runat="server" CommandName="Cancel" Text="Cancel" /><asp:LinkButton ID="btnDelete" runat="server" CommandName="Delete" Text="Delete" /></EditItemTemplate><FooterTemplate><asp:LinkButton ID="btnInsert" runat="Server" Text="Insert" CommandName="Insert" UseSubmitBehavior="False" /></FooterTemplate></asp:TemplateField></Columns></asp:GridView><br /><asp:LinkButton ID="btnAdd" runat="server" Text="Add" OnClick="AddNewRecord" />
Here's my Code Behind in C#:
using System; using System.Data; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.UI; using System.Web.UI.WebControls; using System.Xml; public partial class xmlContact1 : System.Web.UI.Page { protected void Page_Load(object sender, EventArgs e) { if (!Page.IsPostBack) BindData(); } private void BindData() { //bind the gridview gvProducts.DataSource = RetrieveProducts(); gvProducts.DataBind(); } private DataSet RetrieveProducts() { //if the products data is already saved in viewstate //return that data if (ViewState["Contacts"] != null) return ViewState["Contacts"] as DataSet; DataSet dsProducts = new DataSet(); //read the xml schema //dsProducts.ReadXmlSchema(Server.MapPath(@"XML\Co ntacts.xsd")); //read the xml data dsProducts.ReadXml(Server.MapPath(@"XML\Contacts.x ml")); //store the data into viewstate which will be used for //subsequent postbacks ViewState["Contacts"] = dsProducts; return dsProducts; } protected void AddNewRecord(object sender, EventArgs e) { gvProducts.ShowFooter = true; BindData(); } protected void gvProducts_RowCommand(object sender, GridViewCommandEventArgs e) { // If multiple ButtonField column fields are used, use the // CommandName property to determine if "Insert" button was clicked. if (e.CommandName.Equals("Insert")) { //fetch the values of the new product TextBox txtNewFname = gvProducts.FooterRow.FindControl("txtNewFname") as TextBox; TextBox txtNewMname= gvProducts.FooterRow.FindControl("txtNewMname") as TextBox; TextBox txtNewLname= gvProducts.FooterRow.FindControl("txtNewLname") as TextBox; TextBox txtNewAge = gvProducts.FooterRow.FindControl("txtNewAge") as TextBox; TextBox txtNewCity = gvProducts.FooterRow.FindControl("txtNewCity") as TextBox; TextBox txtNewStreet = gvProducts.FooterRow.FindControl("txtNewStreet") as TextBox; TextBox txtNewState = gvProducts.FooterRow.FindControl("txtNewState") as TextBox; TextBox txtNewCountry = gvProducts.FooterRow.FindControl("txtNewCountry") as TextBox; TextBox txtNewMobile = gvProducts.FooterRow.FindControl("txtNewMobile") as TextBox; TextBox txtNewHome = gvProducts.FooterRow.FindControl("txtNewHome") as TextBox; TextBox txtNewOffice = gvProducts.FooterRow.FindControl("txtNewOffice") as TextBox; TextBox txtNewCompany = gvProducts.FooterRow.FindControl("txtNewCompany") as TextBox; TextBox txtNewName = gvProducts.FooterRow.FindControl("txtNewName") as TextBox; TextBox txtNewDepartment = gvProducts.FooterRow.FindControl("txtNewDepartment ") as TextBox; TextBox txtNewEmail = gvProducts.FooterRow.FindControl("txtNewEmail") as TextBox; //insert the new product InsertProduct(txtNewFname.Text, txtNewMname.Text, txtNewLname.Text, txtNewAge.Text, txtNewCity.Text, txtNewStreet.Text, txtNewState.Text, txtNewCountry.Text, txtNewMobile.Text, txtNewHome.Text, txtNewOffice.Text, txtNewCompany.Text, txtNewName.Text, txtNewDepartment.Text, txtNewEmail.Text); //hide the footer gvProducts.ShowFooter = false; //clear the view state so that latest list will be retrieved from file ViewState["Contacts"] = null; // rebind the data BindData(); } } private void InsertProduct(string FirstName, string MidName, string LastName, string Age, string City, string Street, string State, string Country, string Mobile, string Home, string Office, string Company, string Name, string Department, string Email) { //get the product data from viewstate DataSet dsProducts = ViewState["Contacts"] as DataSet; //create a new product row and populate it with user input data DataRow newProductRow = dsProducts.Tables[0].NewRow(); newProductRow["FirstName"] = FirstName; newProductRow["MidName"] = MidName; newProductRow["LastName"] = LastName; newProductRow["Age"] = Age; newProductRow["City"] = City; newProductRow["Street"] = Street; newProductRow["State"] = State; newProductRow["Country"] = Country; newProductRow["Mobile"] = Mobile; newProductRow["Home"] = Home; newProductRow["Office"] = Office; newProductRow["Company"] = Company; newProductRow["Name"] = Name; newProductRow["Department"] = Department; newProductRow["Email"] = Email; dsProducts.Tables[0].Rows.Add(newProductRow); //write the data back to the XML file dsProducts.WriteXml(GetXMLSourcePath()); //store the new product dataset into the viewstate ViewState["Products"] = dsProducts; BindData(); } private string GetXMLSourcePath() { return Server.MapPath(@"XML\Contacts.xml"); } protected void gvProducts_RowEditing(object sender, GridViewEditEventArgs e) { gvProducts.EditIndex = e.NewEditIndex; BindData(); } protected void gvProducts_RowUpdating(Object sender, GridViewUpdateEventArgs e) { // Get the product id of the selected product string ID = gvProducts.DataKeys[e.RowIndex].Value.ToString(); // Get the GridViewRow object that represents the row being edited // from the Rows collection of the GridView control. GridViewRow row = gvProducts.Rows[e.RowIndex]; // Get the controls that contain the updated values. In this example, the updated values are contained //in the TextBox controls declared in the edit item templates of each TemplateField // column fields in the GridView control. TextBox txtFname= (TextBox)row.FindControl("txtFname"); TextBox txtMname= (TextBox)row.FindControl("txtMname"); TextBox txtLname= (TextBox)row.FindControl("txtLname"); TextBox txtAge= (TextBox)row.FindControl("txtAge"); TextBox txtCity = (TextBox)row.FindControl("txtCity"); TextBox txtStreet = (TextBox)row.FindControl("txtStreet"); TextBox txtState = (TextBox)row.FindControl("txtState"); TextBox txtCountry = (TextBox)row.FindControl("txtCountry"); TextBox txtMobile = (TextBox)row.FindControl("txtMobile"); TextBox txtHome = (TextBox)row.FindControl("txtHome"); TextBox txtOffice = (TextBox)row.FindControl("txtOffice"); TextBox txtCompany = (TextBox)row.FindControl("txtCompany"); TextBox txtName = (TextBox)row.FindControl("txtName"); TextBox txtDepartment = (TextBox)row.FindControl("txtDepartment"); TextBox txtEmail = (TextBox)row.FindControl("txtEmail"); //update the product row DataSet dsProducts = ViewState["Contacts"] as DataSet; dsProducts.Tables[0].Rows[e.RowIndex]["FirstName"] = txtFname.Text; dsProducts.Tables[0].Rows[e.RowIndex]["MidName"] = txtMname.Text; dsProducts.Tables[0].Rows[e.RowIndex]["LastName"] = txtLname.Text; dsProducts.Tables[0].Rows[e.RowIndex]["Age"] = txtAge.Text; dsProducts.Tables[0].Rows[e.RowIndex]["City"] = txtCity.Text; dsProducts.Tables[0].Rows[e.RowIndex]["Street"] = txtStreet.Text; dsProducts.Tables[0].Rows[e.RowIndex]["State"] = txtState.Text; dsProducts.Tables[0].Rows[e.RowIndex]["Country"] = txtCountry.Text; dsProducts.Tables[0].Rows[e.RowIndex]["Mobile"] = txtMobile.Text; dsProducts.Tables[0].Rows[e.RowIndex]["Home"] = txtHome.Text; dsProducts.Tables[0].Rows[e.RowIndex]["Office"] = txtOffice.Text; dsProducts.Tables[0].Rows[e.RowIndex]["Company"] = txtCompany.Text; dsProducts.Tables[0].Rows[e.RowIndex]["Name"] = txtName.Text; dsProducts.Tables[0].Rows[e.RowIndex]["Department" ] = txtDepartment.Text; dsProducts.Tables[0].Rows[e.RowIndex]["Email"] = txtEmail.Text; //persiste changes back to the xml data file dsProducts.WriteXml(GetXMLSourcePath()); //store the latest dataset into viewstate ViewState["Contacts"] = dsProducts; //set gridview back to normal mode gvProducts.EditIndex = -1; //rebind gridview with latest data BindData(); } protected void gvProducts_RowDeleting(Object sender, GridViewDeleteEventArgs e) { //get the dataset stored from the viewstate DataSet dsProducts = ViewState["Contacts"] as DataSet; //delete the row from the dataset dsProducts.Tables[0].Rows[e.RowIndex].Delete(); //write those changes to XML data file and save latest dataset to viewstate dsProducts.WriteXml(GetXMLSourcePath()); ViewState["Contacts"] = dsProducts; //change gridview back to normal window gvProducts.EditIndex = -1; //rebind the gridview with latest data BindData(); } protected void gvProducts_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { gvProducts.EditIndex = -1; BindData(); } }
Please help me to get this right
Thank You