Click here to Skip to main content
15,949,741 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
hi....


I developed an application with gridview control and one xml file.

I bind xml data to gridview.Its working fine.But at the time of editing it shows an error like this
Unable to cast object of type 'System.Data.DataTable' to type 'System.Data.DataSet'.

My design is:

ASP.NET
<asp:GridView ID="grdv" runat="server" AutoGenerateColumns="False" AutoGenerateEditButton="True" Height="67px" Width="214px" onrowcancelingedit="grdv_RowCancelingEdit" onrowediting="grdv_RowEditing" onrowupdating="grdv_RowUpdating"  >
      <columns>
       <asp:TemplateField             HeaderText="CompanyName">
<itemtemplate>
<%# Eval("CompanyName") %>

                        </itemtemplate>

<edititemtemplate>
<asp:TextBox ID="txtJobTitle1" runat="Server" Text='<%#Eval("CompanyName")%>'Columns="30">

        </edititemtemplate>
       
  <asp:TemplateField HeaderText="Description">
<itemtemplate>
<%# Eval("Description")%>
</itemtemplate>
      <edititemtemplate>
<asp:TextBox ID="txtDescription1" runat="Server" Text='<%# Eval("Description")%>'Columns="30">
 </edititemtemplate>
  
                           
</columns>




My CS flle is:

C#
public partial class Admin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        //txtJobTitle.Focus();
        //txtDescription.Focus();
        GetData();

    }
protected void grdv_RowEditing(object sender, GridViewEditEventArgs e)
    {
        grdv.EditIndex = e.NewEditIndex;
        GetData();
       
    }
    protected void grdv_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        //GridViewRow row = (GridViewRow)grdv.Rows[e.RowIndex];
        int i = grdv.Rows[e.RowIndex].DataItemIndex;
        //TextBox txtName = (TextBox)GridView1.Rows[e.RowIndex].FindControl("txtName");
      TextBox CompanyName = ((TextBox)grdv.Rows[e.RowIndex].FindControl("txtJobTitle1"));
      TextBox Description = ((TextBox)grdv.Rows[e.RowIndex].FindControl("txtDescription1"));

        //int i = grdv.Rows[e.RowIndex].DataItemIndex;
        //string CompanyName = ((TextBox)grdv.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
        //string Description = ((TextBox)grdv.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
        grdv.EditIndex = -1;
        GetData();
        DataSet dss =(DataSet)grdv.DataSource;

        dss.Tables[0].Rows[i]["CompanyName"] = CompanyName;
        dss.Tables[0].Rows[i]["Description"] = Description;
        dss.WriteXml(Server.MapPath("XMLJobFile.xml"));
        GetData();

    }
    protected void grdv_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        grdv.EditIndex = -1;
        GetData();

    }
    protected void GetData()
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("XMLJobFile.xml"));
        grdv.DataSource = ds.Tables[0];
        grdv.DataBind();
    }
    
}


And my Xml file is:

XML
<<pre lang="xml">?xml version="1.0" standalone="yes"?>
<JOBS>
  <Fresher>
    <CompanyName>
         Rayars Group: </CompanyName>
    <Description>
      We are looking for Graduates (B.sc/B.com and other non-engineering streams) who have decent knowledge about webdesigning
       </Description>
  </Fresher>
</JOBS>



have to update the row with new value and the changes are reflected to xml file.
Please give any solution
Posted
Updated 2-May-12 21:17pm
v4

C#
DataSet dss =(DataSet)grdv.DataSource;


---Problematic Line


Do This

C#
DataTable dt = (DataTable)gridSlips.DataSource; ;
       DataSet dss=new DataSet();
       dss.Tables[0]=dt ;
       dss.Tables[0].Rows[i]["CompanyName"] = CompanyName;
       dss.Tables[0].Rows[i]["Description"] = Description;
       dss.WriteXml(Server.MapPath("XMLJobFile.xml"));
       GetData();
 
Share this answer
 
Comments
Hi,
i am getting error in this line.
dss.Tables[0]=dt ;
error:property or indexer 'System.Data.DataTable collection.this[int]' cannot be assigned to -- it is read only.

thanks,
swathi
Swati Sorry For That Line Actually That was untested and Done By logic Only

Now use
C#
DataTable dt = (DataTable)gridSlips.DataSource; ;
DataSet dss=new DataSet();

dss.Tables.Remove();
dss.Tables.Add(dt);
dss.Tables[0].Rows[i]["CompanyName"] = CompanyName;
dss.Tables[0].Rows[i]["Description"] = Description;
dss.WriteXml(Server.MapPath("XMLJobFile.xml"));
GetData();
This i have Tested and Implemented This All
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900