I'm starting out with Entity Framewrok and I wrote the following simple sample code in a console application:
var post1 = new Post
{
PostTitle = "Post Title 1",
PostBody = "post Body 1",
Comments = new List<Comment>
{
new Comment{CommentAuthor="aravind",CommentBody="my first comment on post 1"},
new Comment{CommentAuthor="dimpu",CommentBody="my second Commment on post1"}
}
};
using (BlogEntites be = new BlogEntites())
{
foreach(Post post in be.Posts)
{
Console.WriteLine(post.PostTitle);
foreach (Comment comment in post.Comments)
{
Console.WriteLine("\t" + comment.CommentBody);
}
}
be.Posts.Add(post1); // post1 added in memory
be.SaveChanges(); // post1 added to DB
}
Console.WriteLine("press any key to continue...");
Console.ReadKey();
At first, I only had foreach only on the posts and it worked fins and I had an output of all my posts.
Then I wanted to add the comments for each post so I've added another inner foreach - which if I was working with regular object (which to my understanding what ORM should be all about) that what I would have done....
But when running this code I'm getting inner exception:
There is already an open DataReader associated with this Command which must be closed first.
What am I not understanding here?
Thanks
What you are not understanding is that you have two foreach statements, each of them is attempting to execute a query from the same data reader. The problem is that you already have an open data read with your first foreach.
There are two ways to solve this. You can either Execute the query into a collection, such as a List by calling foreach(Post post in be.Posts.ToList()) or you can define your connection string with MultipleActiveRecordSets=True (aka MARS support).
Related
I am using VSTS with git.
I have test cases with many bug work items being linked to them.
I want to specifically retrieve all bug work items linked to a particular test case using .NET Client Library or VSTS REST API. The version of the REST API can be 4.0 or later.
I could not find info pertaining to retrieving bug work items linked to a particular test case, though there is info related to retrieving all bug work items.
Here is the code I tried :
static List GetLinkedWorkItems()
{
int[] workitemIds = new int[] { 12697 };
//VssConnection connection = Context.Connection;
VssConnection connection = new VssConnection(new Uri(vstsCollectionUrl), new VssClientCredentials());
WorkItemTrackingHttpClient workItemTrackingClient = connection.GetClient<WorkItemTrackingHttpClient>();
List<Microsoft.TeamFoundation.WorkItemTracking.WebApi.Models.WorkItem> workitems = workItemTrackingClient.GetWorkItemsAsync(workitemIds, expand: WorkItemExpand.Links | WorkItemExpand.Relations).Result;
foreach (var workitem in workitems)
{
Console.WriteLine("Work item {0}", workitem.Id);
if (workitem.Links != null)
{
foreach (var link in workitem.Links.Links)
{
Console.WriteLine(" {0} {1}", link.Key, link.Value);
}
}
}
return workitems;
}
Note that there's no connectivity issues to VSTS. Also, I tried with query based approach as given below, but no use :
VssConnection connection = new VssConnection(new Uri(vstsCollectionUrl), new VssClientCredentials());
//create http client and query for resutls
WorkItemTrackingHttpClient witClient = connection.GetClient<WorkItemTrackingHttpClient>();
Wiql query = new Wiql() { Query = "SELECT [Id], [Title], [State] FROM workitems WHERE [Work Item Type] = 'Test Case'" };
WorkItemQueryResult queryResults = witClient.QueryByWiqlAsync(query).Result;
//Display reults in console
var l = queryResults.WorkItemRelations;
var t = queryResults.WorkItems.Skip(0).Take(100);
if (queryResults == null || queryResults.WorkItems.Count() == 0)
{
Console.WriteLine("Query did not find any results");
}
else
{
foreach (var item in queryResults.WorkItems)
{
Console.WriteLine(item.Id);
Console.WriteLine(item.Url);
}
}
So what you need to do is updating the test case by removing it’s relations for bug work items. And you can achieve it by the update work item REST API. Details as below:
1. Get the relation work items for the test case
Use the Get work item REST API:
GET https://account.visualstudio.com/project/_apis/wit/workitems/workitemID?$expand=all&api-version=4.1
2. Get the count of the related bug work items
Get the count of related work items from the response of step1. If the linked WIT is bug, then the count of relations is the count for linked bugs. Assume there are three bugs only link with the test case.
3. Update test case by removing the related bugs
The path for remove the related work items as /relations/index, and the index is the based on the count if the linked work items which start with 0. So if there are only three bugs linked to the test case, you can remove the paths with /relations/0, /relations/1 and /relations/2.
The Update work item REST API as below:
PATCH https://account.visualstudio.com/project/_apis/wit/workitems/workitemID?api-version=4.1
application/json-patch+json:
[
{
"op": "remove",
"path": "/relations/0"
},
{
"op": "remove",
"path": "/relations/1"
}
{
"op": "remove",
"path": "/relations/2"
}
]
I'm quite the noob using Ionic or Angular for that matter. So as a cheat sheet I'm using the ionic-super-starter template (link below).
I am trying to make a get request to my API and it works just find if I'm doing it like this:
this.api.get('user/'+this.user.userId+'/entries?include=stuff&access_token=TOKEN');
but when I put the url params into an object it stops working:
let options = {
'include':'stuff',
'access_token':'TOKEN'
}
this.api.get('user/'+this.user.userId+'/entries', options);
The only error I get is "Unauthorized Request" since the options object including the access token was not appended to the url.
In the ionic-super-starter template the providers/api/api.ts calls .set() for each key in my params object:
if (params) {
reqOpts.params = new HttpParams();
for (let k in params) {
reqOpts.params.set(k, params[k]);
}
}
but according to Angular University this is not possible since "HTTPParams is immutable".
If it really was wrong to do this, I don't believe it would be in the ionic template. Nor would I believe that I would be the first person to come across this issue.
However, I am stuck here so any help would be appreciated.
Link to Angular University:
https://blog.angular-university.io/angular-http/#httprequestparameters
Link to ionic-super-starter:
https://github.com/ionic-team/starters/tree/master/ionic-angular/official/super
I think I figured it out myself:
if I write (in my src/providers/api/api.ts)
reqOpts.params = reqOpts.params.append(k, params[k]);
instead of
reqOpts.params.set(k, params[k]);
it works.
if you are using a loopback API as I am you might have nested objects like:
let options = {
"filter": {
"order": "date DESC"
},
"access_token":this.user._accessToken
};
this won’t work. try instead:
let options = {
"filter": '{"order":"date DESC"}',
"access_token":this.user._accessToken
};
I am trying to creating a app which can send posts to tumblr using tumblr.js api.
I could send single image using the createPhotoPost method, but I have to send multiple image in a single post via this method.
From the documentation it says that createPhotoPost method has a "data" parameter which can be an array with "URL-encoded binary contents"
When ever I try to send something in the data Array it returns "[Error: form-data: Arrays are not supported.]".
Can someone please help to solve this issue and please explain what should we pass in the array (Really I am not getting what is URL-encoded binary contents)?
Thanks in advance
There is an error in tumblr.js
https://tumblr.github.io/tumblr.js/tumblr.js.html#line504
The documentation at https://www.tumblr.com/docs/en/api/v2#posting is correct.
Basically the issue is that its not supposed to be
data = [ one , two ] its litterally params['data[0]'] = one ; params['data[1]'] = two; (?A PHP Convention)
var request = require('request')// already a tumblr.js dependency
var currentRequest = request({
url:'https://api.tumblr.com/v2/blog/someblog.tumblr.com/post',
oauth:keys,
},function(err,response,body){
currentRequest//
debugger
cb()
})
var params = {
'type' :'photo',
'caption':'Click To Verify You Are A Robot',
}
var params_images = [
fs.createReadStream('image'),
fs.createReadStream('image')
]
// Sign it with the non-data parameters
currentRequest.form(params)
currentRequest.oauth(keys);
// Clear the side effects from form(param)
delete currentRequest.headers['content-type'];
delete currentRequest.body;
// And then add the full body
var form = currentRequest.form();
//###add params_images to params keys 'data[0]' 'data[1]' 'data[2]' ....
params_images.forEach(function(image,index){
params['data['+index+']']
})
for (var key in params) {
form.append(key, params[key]);
}
// Add the form header back
var form_headers = form.getHeaders()
for(var key in form_headers){
currentRequest[key] = form_headers[key]
}
I am working on a code where I need Multiple tables as result of a stored procedure. I am using Entity Framework repository pattern. It returns and bind an IEnumerable object, but I need to bind it with multiple IEnumerables at the same time.
Can anybody help?
This is the code I am using :
db.Database.SqlQuery("procReturnsMultipleResuiltSets")
the ways to achieve your goal are disclosed in this article.
From related article the most common way is:
using (var db = new BloggingContext())
{
// If using Code First we need to make sure the model is built before we open the connection
// This isn't required for models created with the EF Designer
db.Database.Initialize(force: false);
// Create a SQL command to execute the sproc
var cmd = db.Database.Connection.CreateCommand();
cmd.CommandText = "[dbo].[GetAllBlogsAndPosts]";
try
{
db.Database.Connection.Open();
// Run the sproc
var reader = cmd.ExecuteReader();
// Read Blogs from the first result set
var blogs = ((IObjectContextAdapter)db)
.ObjectContext
.Translate<Blog>(reader, "Blogs", MergeOption.AppendOnly);
foreach (var item in blogs)
{
Console.WriteLine(item.Name);
}
// Move to second result set and read Posts
reader.NextResult();
var posts = ((IObjectContextAdapter)db)
.ObjectContext
.Translate<Post>(reader, "Posts", MergeOption.AppendOnly);
foreach (var item in posts)
{
Console.WriteLine(item.Title);
}
}
finally
{
db.Database.Connection.Close();
}
}
please note the important remark: The first result set must be consumed before moving to the next result set.
I've setup a form using googledocs. I just want to have the actual data entered into the form emailed to me, as opposed to the generic response advising that the form has been completed.
I have no skill or experience with code etc, but was sure i could get this sorted. I've spent hours+hours and haven't had any luck.
My form is really basic.it has 5 fields. 4 of which are just text responses, and one multiple choice.
I found this tute online (http://www.labnol.org/internet/google-docs-email-form/20884/) which i think sums up what i'm trying to do, but have not been able to get it to work.
from this site i entered the following code:
function sendFormByEmail(e)
{
var email = "reports.mckeir#gmail.com";
var subject = "Google Docs Form Submitted";
var s = SpreadsheetApp.getActiveSheet();
var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];
var message = "";
for(var i in headers)
message += headers[i] + ' = '+ e.namedValues[headers[i]].toString() + "\n\n";
MailApp.sendEmail(email, subject, message);
}
To this, i get the following response: ->
Your script, Contact Us Form Mailer, has recently failed to finish successfully. A summary of the failure(s) is shown below. To configure the triggers for this script, or change your setting for receiving future failure notifications, click here.
The script is used by the document 100% Club.
Details:
Start Function Error Message Trigger End
12/3/12 11:06 PM sendFormByEmail TypeError: Cannot call method "toString" of undefined. (line 12) formSubmit 12/3/12 11:06 PM
Is anyone able to help shed some light on this for me? I'm guessing i'm not including some data neeeded, but i honestly have no clue.
Workaround http://www.labnol.org/internet/google-docs-email-form/20884/
You have to setup app script to forward the data as email.
I'll point to the comment above that solved it for me: https://stackoverflow.com/a/14576983/134335
I took that post a step further:
I removed the normal notification. The app script makes that generic text redundant and useless now
I modified the script to actually parse the results and build the response accordingly.
function sendFormByEmail(e)
{
var toEmail = "changeme";
var name = "";
var email = "";
// Optional but change the following variable
// to have a custom subject for Google Docs emails
var subject = "Google Docs Form Submitted";
var message = "";
// The variable e holds all the form values in an array.
// Loop through the array and append values to the body.
var s = SpreadsheetApp.getActiveSheet();
var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0];
// Credit to Henrique Abreu for fixing the sort order
for(var i in headers) {
if (headers[i] = "Name") {
name = e.namedValues[headers[i]].toString();
}
if (headers[i] = "Email") {
email = e.namedValues[headers[i]].toString();
}
if (headers[i] = "Subject") {
subject = e.namedValues[headers[i]].toString();
}
if (headers[i] = "Message") {
message = e.namedValues[headers[i]].toString();
}
}
// See https://developers.google.com/apps-script/reference/mail/mail-app#sendEmail(String,String,String,Object)
var mailOptions = {
name: name,
replyTo: email,
};
// This is the MailApp service of Google Apps Script
// that sends the email. You can also use GmailApp here.
MailApp.sendEmail(toEmail, subject, message, mailOptions);
// Watch the following video for details
// http://youtu.be/z6klwUxRwQI
// By Amit Agarwal - www.labnol.org
}
The script utilized in the example is extremely generic but very resilient to change because the message is built as a key/value pair of the form fields submitted.
If you use my script you'll have to tweak the for loop if statements to match your fields verbatim. You'll also want to edit the toEmail variable.
Thanks again for the question and answers. I was about to ditch Google Forms as the generic response was never enough for what I was trying to do.
Lastly, in response to the actual problem above "toString of undefined" specifically means one of the form fields was submitted as blank. If I had to guess, I would say the author only used this for forms where all the fields were required or a quick undefined check would've been put in place.
Something like the following would work:
for(var i in headers) {
var formValue = e.namedValues[headers[i]];
var formValueText = "";
if (typeof(formValue) != "undefined") {
formValueText = formValue.toString();
}
message += headers[i] + ' = '+ formvalueText + "\n\n";
}
I haven't tested this precisely but it's a pretty standard way of making sure the object is defined before trying methods like toString() that clearly won't work.
This would also explain Jon Fila's answer. The script blindly assumes all of the header rows in the response are sent by the form. If any of the fields aren't required or the spreadsheet has fields that are no longer in the form, you'll get a lot of undefined objects.
The script could've been coded better but I won't fault the author as it was clearly meant to be a proof of concept only. The fact that they mention the replyTo correction but don't give any examples on implementing it made it perfectly clear.
If this is a Google Form, do you have any extra columns in your spreadsheet that are not on the form? If you delete those extra columns then it started working for me.
You don't need to use a script. Simply go to Tools >> Notification Rules on your Google Spreadsheet. There you can change the settings to receive an email with your desired information every time the document is changed.