Tagged: ado dataservices Toggle Comment Threads | Keyboard Shortcuts

  • montelof 3:36 pm on September 30, 2010 Permalink | Reply
    Tags: ado dataservices, , development, , , xml   

    Error processing response stream. The XML element contains mixed content. 

    Ok, in my last post I explained how to pass an array of values to the Webget method of an Ado DataService as a primitive value (csv), in this post Im going to show how to retrieve a primitive value from a Webget method avoiding the error message above.

    Lets start with the Dataservice:

    [WebGet] //we are going to return an int

    public IQueryable<int> DWTNewDownTime(int metricArrayID, string metricDate)

    {  //create the command as we used to do in 1.1

        DbCommand cmd = this.CurrentDataSource.Connection.CreateCommand();

        cmd.CommandType = CommandType.StoredProcedure;

        //add the parameters needed for the stored procedure

        EntityParameter MetricArrayID = new EntityParameter("MetricArrayID", DbType.Int16);

        MetricArrayID.Value = metricArrayID;

        cmd.Parameters.Add(MetricArrayID);

        EntityParameter MetricDate = new EntityParameter("MetricDate", DbType.String, 50);

        MetricDate.Value = metricDate;

        cmd.Parameters.Add(MetricDate);      

        //add an output parameter, this parameter should be returned as a SELECT not return from the stored procedure

        EntityParameter Res = new EntityParameter("res", DbType.Int16, 20);

        Res.Direction = ParameterDirection.Output;

        cmd.Parameters.Add(Res);

        //constructs the command text

        cmd.CommandText = string.Format(@"{0}.{1}", this.CurrentDataSource.DefaultContainerName, "DWTGetMetricValuesNewDownTimeActions");

        try

        {//open connection and execute an scalar

            cmd.Connection.Open();

            cmd.ExecuteScalar();

        }

        catch (System.Exception ex)

        {

            throw new System.Exception(ex.InnerException.Message);

        }

        finally

        {   //always close the connection

            cmd.Connection.Close();

        }//return the result output value.

        List<int> _list = new List<int> { Convert.ToInt32(Res.Value) };

        return _list.AsQueryable();

    }

     

    As I mention above the stored procedure should return the output parameter as SELECT.

    now lets see the ModelView:

     

    public void DWTGetMetricValuesNewDownTimeActions(int metricArrayID, string metricDate)

    {   //create a DataserviceQuery as we normally do for other webget methods

        QryInsertMetric = this.context.CreateQuery<int>("DWTNewDownTime")

            .AddQueryOption("MetricArrayID", metricArrayID)

            .AddQueryOption("MetricDate", "’" + metricDate + "’");

        //then create a webclient that will be used to execute the above query

        WebClient wc = new WebClient();

        wc.DownloadStringAsync(QryInsertMetric.RequestUri);

        wc.DownloadStringCompleted += (s, e) =>

        {   //we have to parse the returning XML

            XDocument xdoc = XDocument.Parse(e.Result);

            MetricValueID = Convert.ToInt32(xdoc.Root.Descendants(((XNamespace)@"http://schemas.microsoft.com/ado/2007/08/dataservices&quot;) + "element")

                .Select(xe => xe.Value).ToList().Single());

            //fire the property so you can catch it at your silverlight client

            FirePropertyChanged("MetricValueID");

        };

    }

     

    Finally just catch the property change at your client:

    void Downtimemodel_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs e)

    {

        if (e.PropertyName == "MetricValueID")

        {

            Console.WriteLine(Downtimemodel.MetricValueID.ToString());

        }   

    }

    and that’s it, I hope this help someone get home early.

    some help from Invoking a WebGet throws an exception @ msdn

     
  • montelof 3:53 pm on September 9, 2010 Permalink | Reply
    Tags: ado dataservices, ,   

    Only primitive types are supported as parameters 

    This is the error message i got when tried to pass an array or List to an Ado Dataservice. It looks like you can only use int, string etc, when passing parameters to the webget method, this is a limitation on the Dataservice it self, not the Datacontext, you can easily workaround this by passing the comma separated values to the Dataservice then split the values before calling your DataContext.

    To convert from the string array to the comma separated use this on your silverlight code:

    string _csvproducts = string.Join(“,”, productList);


    you webget method on the Dataservice should look like this:

    [WebGet]

    public IQueryable<ModelWorkcenter> FilterByModelList(string models)

    {

    string[] _modelsqry = models.Split(‘,’).Select(sValue => sValue.Trim()).ToArray();

    var result = this.CurrentDataSource.GetModelsByList(_modelsqry);

    return result;

    }

    what im doing is to split again the values and passing them to the Datacontext method GetModelsByList this method is expecting a List<string> parameter. your Context class should look like this:

    public IQueryable<ModelWorkcenter> GetModelsByList(IList<string> modelList)

    {

    var result = (from c in Scope.Extent<ModelWorkcenter>() where modelList.Contains(c.TopMaterial) select c);

    return result;

    }

    This way your Linq to SQL will generate good SQL code using the IN clause something like

    Select Model from ITEM_MASTER where Model in (‘model1’,’model2’,’model3’,’model4’)
    To call the webget method use the CreateQuery method of your proxy class so you can add the parameter using AddQueryOption

    public void CustomFilterModels(string models)

    {

    QryModelWorkcenter = Entities.CreateQuery<ModelWorkcenter>(“FilterByModelList”)

    .AddQueryOption(“models”, “‘” + models + “‘”);

    QryModelWorkcenter.BeginExecute(this.GetModelWorkcenterCallback, this.Entities);

    }

    Also note that I’m adding single quotes to the string, without quotes the url cant be parsed correctly and you will get syntax error. this is the generated Url:

    http://localhost:3122/workcenters_service.svc/FilterByModelList()?models=’PPT2633-ZRIY0Y03,PPT2637-TRIZ0Y03,PPT2637-ZRIY0Y00′

    I have read some post about very complex ways to do the same thing, others build the e-sql from scratch, I liked like this because seems more clear and easy to understand.

    Please leave your comments.

    source:http://stackoverflow.com

     
c
Compose new post
j
Next post/Next comment
k
Previous post/Previous comment
r
Reply
e
Edit
o
Show/Hide comments
t
Go to top
l
Go to login
h
Show/Hide help
shift + esc
Cancel