Serialize a DataTable

Last post 05-12-2008 5:35 PM by mgodoy_desenv. 9 replies.

Sort Posts:

  • Serialize a DataTable

    05-08-2008, 11:52 AM

    I am using a DataTable in my asmx.cs and would like to know how to serialize the data here.  I really want to use a DataTable on my code behind which is calling this web service, but I also want to leave it open so others can take the serialized data and do with it as they please.  I have listed below what I have which works great for me since I am using a DataTable in my code behind.

    OracleDataAdapter da = new OracleDataAdapter(command);
    DataTable dt = new DataTable("MyResults");
    da.Fill(dt);
    connnection.Close();
    return dt;
     
    "From my point of view, the Jedi are evil!"
  • Re: Serialize a DataTable

    05-08-2008, 12:28 PM
    • Loading...
    • mgodoy_desenv
    • Joined on 02-22-2007, 7:12 PM
    • São Paulo - Brazil
    • Posts 406

    The more simple way is:

    string dataTableSerialized;
    using(StringWriter writer = new StringWriter())
    {
        dt.WriteXml(writer, XmlWriteMode.WriteSchema);
        dataTableSerialized = writer.ToString();
    }

  • Re: Serialize a DataTable

    05-08-2008, 2:00 PM

    This is what I am doing in the code behind.  But when I used a DataSet it would return everything in an XML document will all the values.  Once I switched to the DataTable I lost that.  I Like to use the DataTable for binding to the GridView better so I would like to know exactly how to change this code to work with my GridView or any other object referencing the Web Service. 

    public DataTable GetData(int companyId)
    {
    code.............
    Open Connection
    Parameters
    etc..............

    OracleDataAdapter da = new OracleDataAdapter(command);
    DataTable dt = new DataTable("MyResults");
    da.Fill(dt);
    connection.Close();
    return dt;
    }

     You want me to return a string and not a DataTable?
    "From my point of view, the Jedi are evil!"
  • Re: Serialize a DataTable

    05-08-2008, 4:23 PM
    • Loading...
    • mgodoy_desenv
    • Joined on 02-22-2007, 7:12 PM
    • São Paulo - Brazil
    • Posts 406

    You can keep to use a DataSet. But you will to pass dataSet.Tables[0] to GridView at your Code Behind, or set the DataMember property.

    I had passed a string before cause you had asked how you serialize a DataTable. However, you can return anything in your WebMethod whether it implements Serializable attribute.

  • Re: Serialize a DataTable

    05-09-2008, 10:20 AM

    Forgive my ignorance, but how would that look on my webService .cs and how would it be called and passed to the code behind of the aspx page?

    Thank you!

    "From my point of view, the Jedi are evil!"
  • Re: Serialize a DataTable

    05-09-2008, 12:38 PM
    Answer
    • Loading...
    • mgodoy_desenv
    • Joined on 02-22-2007, 7:12 PM
    • São Paulo - Brazil
    • Posts 406

    Hi, I don´t know how is your Web Service, so wroten a simple (and tested) code to you take a look:

    Web Service
    Created from begin, through of Visual Studio

    using System;
    using System.Web;
    using System.Web.Services;
    using System.Web.Services.Protocols;
    using System.Data;

    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    public class Service : System.Web.Services.WebService
    {
        public Service () {

            //Uncomment the following line if using designed components
            //InitializeComponent();
        }

        [WebMethod]
        public DataSet GetData(int companyId)
        {
            #region Ignore that code. It simulate a data retrieve.

            #region DataSet creation

            DataTable dataTable = new DataTable("newDataTable");

            dataTable.Columns.Add("someField", typeof(string));
            dataTable.Columns.Add("dataCreation", typeof(DateTime));

            DataSet dataSet = new DataSet("myDataSet");
            dataSet.Tables.Add(dataTable);

            #endregion

            #region Filling DataTable

            DataRow dataRow;

            dataRow = dataTable.NewRow();
            dataRow["someField"] = "something1";
            dataRow["dataCreation"] = DateTime.Now;

            for (int i = 0; i < 30; i++)
            {
                CreateDataRow(dataTable);
            }

            #endregion

            #endregion

            return dataSet;
        }

        #region Ignore that code. It simulate a data retrieve.

        private void CreateDataRow(DataTable dataTable)    
        {
            DataRow dataRow;

            dataRow = dataTable.NewRow();
            dataRow["someField"] = String.Format("something{0}", dataTable.Rows.Count);
            dataRow["dataCreation"] = DateTime.Now;

            dataTable.Rows.Add(dataRow);
        }

        #endregion

    }

    Web Site comsuming the WebService
    Web Service reference added by Visual Studio. Proxy named "MyWebService"

    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;

    namespace ConsumerWebServiceTest
    {
        public partial class _Default : System.Web.UI.Page
        {
            protected void Page_Load(object sender, EventArgs e)
            {
            }

            protected void Button1_Click(object sender, EventArgs e)
            {
                MyWebService.Service myWebService = new ConsumerWebServiceTest.MyWebService.Service();
                GridView1.DataSource = myWebService.GetData(1);
                GridView1.DataBind();
            }
        }
    }

  • Re: Serialize a DataTable

    05-12-2008, 12:02 PM

    This part has me confused, do I use the CreateDataRow?  and say my column is UserId  do I replace ""someField" or "something" with that?  Thanks for tolerating my ignorance. Embarrassed

     #region Filling DataTable

            DataRow dataRow;

            dataRow = dataTable.NewRow();
            dataRow["someField"] = "something1";
            dataRow["dataCreation"] = DateTime.Now;

            for (int i = 0; i < 30; i++)
            {
                CreateDataRow(dataTable);
            }

            #endregion

    "From my point of view, the Jedi are evil!"
  • Re: Serialize a DataTable

    05-12-2008, 12:37 PM
    • Loading...
    • mgodoy_desenv
    • Joined on 02-22-2007, 7:12 PM
    • São Paulo - Brazil
    • Posts 406

    All the code inside of "Ignore that code. It simulate a data retrieve." region, that include CreateDataRow method, I created just to fill the DataSet. I haven´t a database, so I had used a DataSet with fictitious datas. In your case, you will remove all that code to place some code to return datas from database. To do that, you will to use a SqlConnection, SqlCommand and SqlDataAdapter classes to fill that DataSet with your datas.

    To turn more easy to understand, I recommend that you create a new project using my code placed before. Follow all the pass that I wroten just to see the datas in GridView. After, replace the "Ignore that code. It simulate a data retrieve." region to code to return your datas.

  • Re: Serialize a DataTable

    05-12-2008, 4:48 PM

    Does it matter that I am using Oracle?

    "From my point of view, the Jedi are evil!"
  • Re: Serialize a DataTable

    05-12-2008, 5:35 PM
    • Loading...
    • mgodoy_desenv
    • Joined on 02-22-2007, 7:12 PM
    • São Paulo - Brazil
    • Posts 406

    In case you will to use Oracle classes: OracleDataAdapter, OracleCommand and OracleConnection.

Page 1 of 1 (10 items)