Add value from Textbox to xml file - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Add value from Textbox to xml file

luffymcawesomeluffymcawesome Posts: 3Member
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:
[code]


sample
sample
sample
20

sample
sample
sample
sample

1234567
7654321
7894561


sample
IT Technician

xyz@xxx.com


[/code]

but in my output, i'm getting this:

[code]


sample
sample
sample
20
sample
sample
sample
sample
1234567
7894561
1122334
sample
sample
IT Technician
xyz@xxx.com


[/code]

Here's my Interface:

[code]









<%#Eval("FirstName")%>




' runat="server" />




<%#Eval("MidName")%>




' runat="server" />




<%#Eval("LastName")%>

' runat="server" />







<%#Eval("Age")%>


' runat="server" />







<%#Eval("City")%>




' runat="server" />




<%#Eval("Street")%>




' runat="server" />




<%#Eval("State")%>




' runat="server" />




<%#Eval("Country")%>




' runat="server" />




<%#Eval("Mobile")%>




' runat="server" />




<%#Eval("Home")%>




' runat="server" />




<%#Eval("Office")%>




' runat="server" />




<%#Eval("Company")%>




' runat="server" />




<%#Eval("Name")%>




' runat="server" />




<%#Eval("Department")%>




' runat="server" />




<%#Eval("Email")%>




' runat="server" />




















[/code]

Here's my Code Behind in C#:

[code]
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(@"XMLContacts.xsd"));

//read the xml data
dsProducts.ReadXml(Server.MapPath(@"XMLContacts.xml"));

//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(@"XMLContacts.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();
}


}
[/code]

Please help me to get this right
Thank You
Sign In or Register to comment.