Thursday 29 November 2012

read and write data into xml using gridview


//creation of gridview

<table width="900px">
                <tr>
                    <td style="background-color: Silver; width: 900">
                        <asp:GridView ID="gvClientTestimonials" runat="server" AutoGenerateColumns="false"
                            OnRowDataBound="gvClientTestimonials_RowDataBound">
                            <Columns>
                                <asp:TemplateField>
                                    <ItemTemplate>
                                        <p>
                                            <img src="images/open-quotes.gif" width="19" height="12" hspace="5" vspace="5" align="bottom"><asp:Label
                                                ID="lblTestimonial" runat="server">
                                            </asp:Label>
                                            <img src="images/close-quotes1.gif" width="19" height="12" hspace="5" vspace="5"
                                                align="middle"></p>
                                        <div align="right">
                                            <strong>--<asp:Label ID="lblClientName" runat="server"></asp:Label></strong></div>
                                        <br />
                                    </ItemTemplate>
                                </asp:TemplateField>
                            </Columns>
                        </asp:GridView>
                    </td>
                </tr>
            </table>

//in.cs file


protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {

                DataSet ds = new DataSet("Testimonials");
                //finding xml file is exist or not

                if (File.Exists(Server.MapPath("Testimonials.xml")) == false)
                {                  
                    ds.Tables.Add("Testimonials");
                    ds.Tables[0].Columns.Add("Id");
                    ds.Tables[0].Columns.Add("Testimonial");
                    ds.Tables[0].Columns.Add("Client");
                    ds.Tables[0].Columns.Add("DateTime");
                   // ds.Tables[0].Rows.Add(txtClientTestimonials.Text, txtClientName.Text, DateTime.Now.ToString());
                    ds.Tables[0].Rows.Add(0, "Working with the some is a great pleasure!", "ram", DateTime.Now.ToString());
                    ds.WriteXml(Server.MapPath("ClientTestimonials/Testimonials.xml"));
                    ds.Clear();
                    ds.ReadXml(Server.MapPath("ClientTestimonials/Testimonials.xml"));
                    gvClientTestimonials.DataSource = ds;
                    gvClientTestimonials.DataBind();
                }
                else
                {
                    ds.ReadXml(Server.MapPath("ClientTestimonials/Testimonials.xml"));
                    gvClientTestimonials.DataSource = ds;
                    gvClientTestimonials.DataBind();
                }
               
            }
           
        }
        protected void gvClientTestimonials_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.DataRow)
            {
                Label lblTestimonial = (Label)e.Row.FindControl("lblTestimonial");
                Label lblClientName = (Label)e.Row.FindControl("lblClientName");
                DataRowView drv = (DataRowView)e.Row.DataItem;
                lblTestimonial.Text = drv["Testimonial"].ToString();
                lblClientName.Text = drv["Client"].ToString();
            }
        }
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            DataSet ds = new DataSet("Testimonials");
            if (File.Exists(Server.MapPath("ClientTestimonials/Testimonials.xml")) == false)
            {
                ds.Tables.Add("Testimonials");
                ds.Tables[0].Columns.Add("Id");
                ds.Tables[0].Columns.Add("Testimonial");
                ds.Tables[0].Columns.Add("Client");
                ds.Tables[0].Columns.Add("DateTime");              
                ds.Tables[0].Rows.Add(0,"Working with the Roopasoft is a great pleasure!","James",DateTime.Now.ToString());
                ds.WriteXml(Server.MapPath("ClientTestimonials/Testimonials.xml"));
            }
            else
            {
                ds.ReadXml(Server.MapPath("ClientTestimonials/Testimonials.xml"));
                ds.Tables[0].Rows.Add(ds.Tables[0].Rows.Count+1,txtClientTestimonials.Text, txtClientName.Text, DateTime.Now.ToString());
                ds.WriteXml(Server.MapPath("ClientTestimonials/Testimonials.xml"));
            }
            gvClientTestimonials.DataSource = ds;
            gvClientTestimonials.DataBind();
        }
        protected void btnCancel_Click(object sender, EventArgs e)
        {
            txtClientName.Text = "";
            txtClientTestimonials.Text = "";

        }

No comments:

Post a Comment