Friday, 30 August 2013

Database error after DeleteObject(...) / SaveChanges()

Database error after DeleteObject(...) / SaveChanges()

Context: web application / ASP.NET 4.0 / EF 4.0 / MSSQLEXPRESS2012
I'm trying to do a simple delete as follows:
if (someObject != null)
{
if (!context.IsAttached(someObject))
context.SomeObjects.Attach(someObject);
context.DeleteObject(someObject);
context.SaveChanges();
}
The code executes without problem and SaveChanges returns the expected
number of rows. But when I try to read the table afterwards it times out
(this happens in mssms as well as in code).
It also seems to corrupt other processes, returning '8501 MSDTC on server
is unavailable' and "No process is on the other end of the pipe" on
subsequent site access. Which of the three errors happens in the
application seems somewhat random, but the timeout in mssms always
happens.
Inserts and updates on the same table execute without problem.
Apart from reading through a zillion posts, I tried wrapping it in
try/catch and transaction with SaveChanges(false) and manual
AcceptAllChanges; the debugger shows the correct object just before delete
- everything appears normal until table read or accessing another page.
I'm not sure where best to look next - any ideas are greatly appreaciated!

Thursday, 29 August 2013

Why does my function sometimes not execute the document.body.innerHTML command?

Why does my function sometimes not execute the document.body.innerHTML
command?

I've made a function to output data from Google Maps using the Google API.
The function is called to output from an array of places.
The problem is that sometimes it won't output the last
document.body.innerHTML part which outputs the place's URL. If I remove
the for loop which outputs the place's review aspects then the URL is
always shown.
function printme(place, status) {
if (status == google.maps.places.PlacesServiceStatus.OK) {
document.body.innerHTML += ('<br />'+ place.name + ' R: '+ place.rating);
for (var i = 0, reviews; reviews = place.reviews[i]; i++) {
for (var x = 0, aspectr; aspectr = reviews.aspects[x]; x++) {
document.body.innerHTML += ('<br />'+'Aspect: ' + aspectr.type + ' '
+ aspectr.rating);
}
}
document.body.innerHTML += ('<br />'+ '<a href =
'+place.url+'>'+place.url+'</a>'+'<br />');
}
else {
alert(status);
}
}
I can't get my head around why that is.

Jquery code to print object json to normal form

Jquery code to print object json to normal form

USing jQuery code how can i print the json data
while printing the data am getting [object object]
how can i see the actual data using jquery to conver [object object]

Wednesday, 28 August 2013

Selecting for the absence of value in a one-to-many table design

Selecting for the absence of value in a one-to-many table design

I've been working all day on a problem that I expected to be simple, but
is proving incredibly elusive. I suspect I may not be asking the right
questions, so please bear with me.
I have a table with a bunch of newspaper articles for a research project.
The idea is that researchers can tag individual articles. These tags are
stored in a second table. An article can have any number of tags.
I can then select articles with a certain tag by using;
SELECT *,
GROUP_CONCAT(`TAGS`.`TAG`) AS tags
FROM ARTICLES
LEFT JOIN TAGS
ON TAGS.ID = ARTICLES.ID
WHERE TAGS.TAG = 'search term'
GROUP BY ARTICLES.ID;
My problems start when I want to select articles based on the absence of a
particular tag. If an article has only one tag, the result is as expected,
but if there is more then one tag associated with an article, the tag is
simply ommitted.
SELECT *,
GROUP_CONCAT(`TAGS`.`TAG`) AS tags
FROM ARTICLES
LEFT JOIN TAGS
ON TAGS.ID = ARTICLES.ID
WHERE TAGS.TAG != 'search term'
OR TAGS.TAG IS NULL
GROUP BY ARTICLES.ID;
if the original tables where as follows;
ID Name
1 Article #1
2 Article #2
and;
ArticleID Tag
1 New
1 Long
1 Boring
2 Old
2 Long
2 Interesting
Then if I use the above query to select articles where tag != Boring, the
results would be;
ArticleID Name Tags
1 Article #1 New, Long
2 Article #2 Old, Long, Interesting
How can I make it exclude the first article altogether, rather then just
excluding that tag? Keeping in mind that there are over a hundred thousand
articles in the database, what is the most efficient way to do this? I've
looked at dozens of other questions and google searches, but selecting for
the absence of a tag like this is something I could not find advice on.
On a sidenote, I am currently using a one-to-many table, as each tag
appears once for each article it is linked to. I noticed that a lot of
people in similar scenarios use a many-to-many design. Is this that much
faster then having just a foreign key in the tags table referencing the
article table?
Thank you for helping out an SQL noob :).

Changing Color array to produce Red Tint in OpenGL

Changing Color array to produce Red Tint in OpenGL

I have run across some open source code for a screensaver and would like
to understand how I could modify the color array of a Color Verticies to
produce a RED tint instead of a GREEN tent
Here is the code that the author uses to set the green color:
// Compute the new state of the color vertices
- (void) computeColorVertices
{
int i,maxi,c;
GLfloat g, gstep, cursorglow;
c = 0; // To suppress spurious warning
gstep = stripParams.colorCycleSpeed;
// First, run down the strip cycling colors to bright then back to dark
g = startColor;
maxi = cursorDrawing ? cursorPos : stripSize;
for (i=0; i < maxi; i++) {
for (c = 0; c < 4; c++) {
// Some shade of green if cell is not empty
colorArray[16*i + 4*c + 1] = (cellState[i] == 0) ? 0.0 : g;
// Cells which are very bright are slightly whitened
colorArray[16*i + 4*c + 0] = ((g > 0.7) && (cellState[i] != 0)) ?
(g - 0.6) : 0.0;
colorArray[16*i + 4*c + 2] = ((g > 0.7) && (cellState[i] != 0)) ?
(g - 0.6) : 0.0;
// Transparent if cell is empty, otherwise opaque
colorArray[16*i + 4*c + 3] = (cellState[i] == 0) ? 0.0 : 1.0;
}
g += gstep;
if (g > 1.0) {
g = 0.2;
}
}
// Cycle the start color used above, to make the colors appear to fall
startColor -= stripParams.colorFallSpeed;
if (startColor < 0.2) {
startColor = 1.0;
}
// If the cursor's drawing, work up from its position making sure the
cells aren't too dark
if (cursorDrawing) {
maxi = cursorPos - 1;
cursorglow = 0.8;
for (i = maxi; i >= 0 && cursorglow > 0.2; i--) {
// If there's some cursor-imparted glow left, use it
if (colorArray[16*i + 4*c + 1] < cursorglow) {
for (c = 0; c < 4; c++) {
// Some shade of green if cell is not empty
colorArray[16*i + 4*c + 1] = (cellState[i] == 0) ? 0.0 :
cursorglow;
// Cells which are very bright are slightly whitened
colorArray[16*i + 4*c + 0] = ((cursorglow > 0.7) &&
(cellState[i] != 0)) ? (cursorglow - 0.6) : 0.0;
colorArray[16*i + 4*c + 2] = ((cursorglow > 0.7) &&
(cellState[i] != 0)) ? (cursorglow - 0.6) : 0.0;
// Transparent if cell is empty, otherwise opaque
colorArray[16*i + 4*c + 3] = (cellState[i] == 0) ? 0.0 : 1.0;
}
}
cursorglow -= gstep;
}
}
}

Complicated class member construction

Complicated class member construction

Since I know that it is generally much nicer to initialize all members in
the initializer list of the constructor, I would like to know if it is
also possible to do some more complicated construction in c++ inside the
initializer list. In my program I would like to construct a class that
initializes two of it's member vectors. Since I'm using them a Lot I would
like to cache the contents, therefore I want to create the vectors at
construction.
edit The main reason I want to cache these values that I can generate the
the x and y coordinates from a circle with any radius without the need to
recompute the sin and cosine values. So I'm using n (n_samples) for the
granularity of sinx and cosy vector, which I use as lookup table
internally.
So is it possible to do the initialization inside the initializer list, in
such way that the constructor only needs to know how many samples it
should create?
Please note: While writing a short self contained question I wrote sinx
and cosy for the vectors with respectively x and y coordinates of a
circle. I could change this but then I would invalidate answers below. The
sinus of angle gives a y-coordinate and the cosine will give a x value
typically.
#include <vector>
#include <iostream>
#include <cmath>
class circular {
public:
circular( unsigned n = 20 );
/* In my actual program I do much more
* complicated stuff. And I don't want
* the user of this class to be bothered
* with polar coordinates.
*/
void print_coordinates( std::ostream& stream, double radius );
private:
unsigned number_of_samples;
std::vector<double> sinx;
std::vector<double> cosy;
};
circular::circular( unsigned n )
:
number_of_samples(n) //I would like to initialize sinx cosy here.
{
for ( unsigned i = 0; i < number_of_samples; ++i ){
sinx.push_back( std::sin( 2*M_PI / number_of_samples*i ) );
cosy.push_back( std::cos( 2*M_PI / number_of_samples*i ) );
}
}
void
circular::print_coordinates( std::ostream& stream, double r)
{
for ( unsigned i = 0; i < sinx.size(); ++i ) {
stream << "{ " <<
sinx[i] * r <<
" , " <<
cosy[i] * r <<
" } " <<
std::endl;
}
}
int main () {
circular c(20);
c.print_coordinates(std::cout, 4);
c.print_coordinates(std::cout, 5);
return 0;
}
many thanks for your effort.
Heteperfan

ALAssetslibrary - Not able to return to the method the list of informations about images

ALAssetslibrary - Not able to return to the method the list of
informations about images

I have implemented a method that find the images inside the device. For do
this i'm using the ALAssetslibrary framework. I want return an NSString
object with the list of the informations about the images. I have already
found them, but unfortunately the ALAssetslibrary seems that works
asynchronously. This cause a problem. In fact, when i write "return
xmlList" it result null because probably the ALAssetslibrary doesn't have
finish yet to perform the search. How can i resolve this problem? Here
there is my working code.
-(NSString *) getPhotos{
NSMutableArray *idList = [[NSMutableArray alloc] init];
__block XMLWriter* xmlWriter = [[XMLWriter alloc]init];
__block NSString *xmlList;
__block ALAssetsLibrary *library = [[ALAssetsLibrary alloc] init];
__block NSString *description = [[NSString alloc] init];
[library enumerateGroupsWithTypes:ALAssetsGroupAll
usingBlock:^(ALAssetsGroup *group, BOOL *stop) {
if (group) {
[group setAssetsFilter:[ALAssetsFilter allPhotos]];
[group enumerateAssetsUsingBlock:^(ALAsset *asset, NSUInteger index,
BOOL *stop){
if (asset){
NSString *description = [asset description];
NSRange first = [description rangeOfString:@"URLs:"];
NSRange second = [description rangeOfString:@"?id="];
NSString *path = [description substringWithRange:
NSMakeRange(first.location + first.length, second.location -
(first.location + first.length))];
[idList addObject:path];
}
}];
}
if (group == nil){
[xmlWriter writeStartDocumentWithEncodingAndVersion:@"UTF-8"
version:@"1.0"];
[xmlWriter writeStartElement:@"Data"];
int i = 0;
for(i = 0; i<=[idList count]-1; i++){
[xmlWriter writeStartElement:@"Photo"];
[xmlWriter writeAttribute:@"Id" value:[idList objectAtIndex:i]];
[xmlWriter writeEndElement:@"Photo"];
}
[xmlWriter writeEndElement:@"Data"];
[xmlWriter writeEndDocument];
xmlList = [xmlWriter toString];
NSLog(@"xml list: %@", xmlList);
}
} failureBlock:^(NSError *error) {
NSLog(@"error enumerating AssetLibrary groups %@\n", error);
}];
NSLog(@"xml list2 %@", xmlList);
return xmlList;
}
In my code you can see the line "NSLog(@"xml list: %@", xmlList);" inside
the ""if (group == nil)" block. This return the correct informations in
the log. But the last NSLog "NSLog(@"xml list2 %@", xmlList);" return
null, so it is empty. What can i do? Is there a way for add for example a
timer or something? I don't know how proceed. Thanks