Editable SQL05 XML in ASP.NET

Discussion in 'ASP .Net' started by Greg Collins [Microsoft MVP], Apr 25, 2007.

  1. What do people use when they want to pull XML from a SQL05 XML datatype column, edit it in an ASP.NET web app, and the post the updated XML back to SQL05?

    I've looked into pulling the XML down into an XmlDataSource control, but this doesn't seem to be a very likely option as the bindings are readonly.

    Seems like a common enough scenario--what have you done (those who have done this)?

    --
    Greg Collins [Microsoft MVP]
    Visit Braintrove ( http://www.braintrove.com )
     
    Greg Collins [Microsoft MVP], Apr 25, 2007
    #1
    1. Advertising

  2. Yes, I pull the SQL XML column down and place it in an XmlDocument, and then I put that into an XmlDataSource, but then I can't read it back out from there...

    protected void Button1_Click(object sender, EventArgs e)
    {
    SqlConnection conn = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=SqlXmlTest;Integrated Security=True");
    conn.Open();

    string sql = "SELECT [Data] FROM [Record] WHERE ([UserId] = " + tbUserId.Text + ")";
    SqlCommand comm = new SqlCommand(sql, conn);
    XmlReader reader = comm.ExecuteXmlReader();
    XmlDocument doc = new XmlDocument();
    doc.Load(reader);

    reader.Close();
    conn.Close();

    XmlDataSource1.Data = doc.OuterXml;
    Repeater1.DataSourceID = "XmlDataSource1";
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
    string sql = "UPDATE [Record] SET [Data] = '" + XmlDataSource1.Data +"' WHERE ([UserId] = " + tbUserId.Text + ")";
    Response.Write(sql);
    }

    The Button1 click works fine... but clicking Button2, XmlDataSource1.Data (which is supposed to be read/write) gives me an empty string.

    In the web page, I bound the data to a text box control, so I could try editing it to see what would happen... but I just get a blank trying to read the Data property no matter what.

    Ideas?

    --
    Greg Collins [Microsoft MVP]
    Visit Braintrove ( http://www.braintrove.com )
     
    Greg Collins [Microsoft MVP], Apr 26, 2007
    #2
    1. Advertising

  3. Greg Collins [Microsoft MVP]

    Bruno Piovan Guest

    Hi Greg, I never did that before, but have you tried to put the xml in a
    XmlDocument class?

    Bruno

    "Greg Collins [Microsoft MVP]" <gcollins_AT_msn_DOT_com> wrote in message
    news:...
    What do people use when they want to pull XML from a SQL05 XML datatype
    column, edit it in an ASP.NET web app, and the post the updated XML back to
    SQL05?

    I've looked into pulling the XML down into an XmlDataSource control, but
    this doesn't seem to be a very likely option as the bindings are readonly.

    Seems like a common enough scenario--what have you done (those who have done
    this)?

    --
    Greg Collins [Microsoft MVP]
    Visit Braintrove ( http://www.braintrove.com )
     
    Bruno Piovan, Apr 26, 2007
    #3
  4. I'm still fairly new to a lot of this. I have XML/XSLT/HTML background, and I've got my feet wet in ASP.NET and SQL -- but a lot of these asp:controls are still new to my. Still trying to wrap my head around them as quickly as possible.

    How complex of an XML structure can a dataset handle? If not very, maybe I can do multiple datasets and pull various parts of the document out... I'd have to see if that would work.

    I'm assuming it's fairly easy to drop an XML doc into a dataset? Do you have a small code sample or a pointer to an article?

    Thanks!

    --
    Greg Collins [Microsoft MVP]
    Visit Braintrove ( http://www.braintrove.com )
     
    Greg Collins [Microsoft MVP], Apr 27, 2007
    #4
  5. Greg Collins [Microsoft MVP]

    Bruno Piovan Guest

    Hi greg,
    according to the documentation, the XmlDataSource control is tipically used
    on read-only scenarios. My suggestion would be to load the xml into a
    dataset (assuming you can change the xml accordingly if needed), this will
    create a datatable filled in the dataset, then you use this datatable as the
    datasource of a gridview for example...

    what you think?

    Bruno

    "Greg Collins [Microsoft MVP]" <gcollins_AT_msn_DOT_com> wrote in message
    news:%...
    Yes, I pull the SQL XML column down and place it in an XmlDocument, and then
    I put that into an XmlDataSource, but then I can't read it back out from
    there...

    protected void Button1_Click(object sender, EventArgs e)
    {
    SqlConnection conn = new SqlConnection(@"Data
    Source=.\SQLEXPRESS;Initial Catalog=SqlXmlTest;Integrated Security=True");
    conn.Open();

    string sql = "SELECT [Data] FROM [Record] WHERE ([UserId] = " +
    tbUserId.Text + ")";
    SqlCommand comm = new SqlCommand(sql, conn);
    XmlReader reader = comm.ExecuteXmlReader();
    XmlDocument doc = new XmlDocument();
    doc.Load(reader);

    reader.Close();
    conn.Close();

    XmlDataSource1.Data = doc.OuterXml;
    Repeater1.DataSourceID = "XmlDataSource1";
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
    string sql = "UPDATE [Record] SET [Data] = '" + XmlDataSource1.Data +"'
    WHERE ([UserId] = " + tbUserId.Text + ")";
    Response.Write(sql);
    }

    The Button1 click works fine... but clicking Button2, XmlDataSource1.Data
    (which is supposed to be read/write) gives me an empty string.

    In the web page, I bound the data to a text box control, so I could try
    editing it to see what would happen... but I just get a blank trying to read
    the Data property no matter what.

    Ideas?

    --
    Greg Collins [Microsoft MVP]
    Visit Braintrove ( http://www.braintrove.com )
     
    Bruno Piovan, Apr 27, 2007
    #5
  6. Greg Collins [Microsoft MVP]

    Bruno Piovan Guest

    Hi Greg,
    The underlying data of a DataSet is stored as XML.

    Below is a code that creates a DataSet, a DataTable, DataColumns and then
    add some sample data.

    protected void Button1_Click(object sender, EventArgs e)
    {
    DataSet ds = new DataSet();
    DataTable dt = new DataTable("MyDataTable");

    dt.Columns.Add(new DataColumn("ID", typeof(Int32)));
    dt.Columns.Add(new DataColumn("Name", typeof(string)));

    ds.Tables.Add(dt);

    dt.Rows.Add(new object[] { 1, "Bruno 1" }); /* the values passed
    here are in the order of creation of the datacolumns, "ID" first, "Name"
    second */
    dt.Rows.Add(new object[] { 2, "Bruno 2" });
    dt.Rows.Add(new object[] { 3, "Bruno 3" });

    Response.Write("<pre>" + Server.HtmlEncode(ds.GetXml()) + "</pre>");
    }

    The GetXml method returns the underlying xml... this is to show you how the
    xml structure is, and below is a code that you would use to "bind" this xml
    to a GridView control.

    protected void Button2_Click(object sender, EventArgs e)
    {
    DataSet ds = new DataSet();

    /* fills the dataset with the xml from db */
    ds.ReadXml(command.ExecuteXmlReader());

    GridView1.DataSource = ds.Tables[0];
    GridView1.DataBind();
    }

    I don't know how the scructure of your xml is, but if you can fit it in a
    DataSet it would be a good way to do what you want, try to add more
    DataTables to the DataSet and see the output xml. Check also the
    GetXmlSchema and ReadXmlSchema methods of the DataSet.

    More info on DataSet here
    http://msdn2.microsoft.com/en-us/library/system.data.dataset.aspx

    Bruno
     
    Bruno Piovan, Apr 27, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jack
    Replies:
    1
    Views:
    448
    Rick Spiewak
    Apr 30, 2004
  2. Mike

    ASP.NET Editable DataGrid

    Mike, Jul 11, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    630
    Saravana [MVP]
    Jul 12, 2004
  3. Davide Vernole [MVP]

    Re: ASP.NET Editable Datagrid

    Davide Vernole [MVP], Jul 11, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    397
    Davide Vernole [MVP]
    Jul 11, 2004
  4. =?Utf-8?B?QW1pdC5ORVQ=?=

    Editable Dropdown list control in ASP.NET 2.0

    =?Utf-8?B?QW1pdC5ORVQ=?=, Oct 17, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    4,749
    Eliyahu Goldin
    Oct 17, 2006
  5. DG

    Web service is not releasing SQL05 locks

    DG, Nov 20, 2007, in forum: ASP .Net Web Services
    Replies:
    5
    Views:
    351
Loading...

Share This Page