Tuesday, May 22, 2012

PDO get the last ID had inserted

I have a query and i want to get the last ID had inserted , the filed ID is primary key and auto increment



i know that i have to use this statement



LAST_INSERT_ID()


That statement works with me when the query like this



$query = "INSERT INTO `cell-place` (ID) VALUES (LAST_INSERT_ID())";


But if i want to get that ID using this statement



$ID = LAST_INSERT_ID();


I have got this error



Fatal error: Call to undefined function LAST_INSERT_ID()


What am i doing wrong please





A program worked fine on my computer but when I sent it on another one with a Dutch Windows it has problems with the DateTimePicker

The program is with C# WinForms and SQL Server 2008. When I want to enter data that includes the value from a DateTimePicker I can see that the wording is in Dutch and then I get an error about converting of the value. Is there any way to pre-program it to get around this? I've caught the error and here it is.



error



        try
{
SqlConnection connect = new SqlConnection("Data Source=Localhost\\SQLExpress;Initial Catalog=DataBase;Integrated Security=True");
SqlDataAdapter da = new SqlDataAdapter();

/******************** Inserting ********************/
string query = "INSERT INTO spending VALUES (";
query += "'" + date_dateTimePicker.Value + "', "; // Date
query += "'" + Convert.ToDecimal(amount_spent_textBox.Text) + "', "; // Amount spent
query += "'" + spent_on_textBox.Text + "')"; // Spent on
connect.Open();
da.InsertCommand = new SqlCommand(query, connect);
da.InsertCommand.ExecuteNonQuery();

connect.Close();
}

catch (Exception error)
{
MessageBox.Show(error.ToString());
}


Things are getting thick.. I got this error while trying to insert a dateTimePicker value into the database the same way I did with the code above. It worked perfectly fine on my computer but it won't work here. Can someone explain? Here is the error:



error



Code used:



string update = "UPDATE table SET the_date = '" + the_date_dateTimePicker.Value + "' WHERE instance_ID = 1";
connect.Open();
da.InsertCommand = new SqlCommand(update, connect);
da.InsertCommand.ExecuteNonQuery();
connect.Close();




Set focus to UI element using XAMl or MVVM

I want to set focus to the ui elemt when i press any key in the text box. i wnt it purely on XAML sid or by using mvvm.





Inserting a List<> in SQL Table

I have an entity "report" whose values i want to insert in a Database table. Following attributes of the Report have to be inserted -



reportID - int
RoleID - int
Created_BY = SYSTEM(default)
CURRENT_TIMESTAMP


Now the problem is with the 2nd attribute. I have a report with the LIST<ROLES> attributes. ROLES is a well defined entity which has an ID and a NAME. From this list I have to extract every role and insert each role's ID into the table.



So my query presently looks as below :



INSERT INTO REPORT_MARJORIE_ROLE(REPORT_ID, ROLE_ID, CREATED_BY, CREATED)
VALUES({0}, {1}, 'SYSTEM', CURRENT_TIMESTAMP)


The C# code from where I am parsing these values is as follows :



try
{
StringBuilder _objSQL = new StringBuilder();
_objSQL.AppendFormat(Queries.Report.ReportQueries.ADD_NEW_ROLES, report.ID, "report.MarjorieRoles.Add(MarjorieRole"));
_objDBWriteConnection.ExecuteQuery(_objSQL.ToString());
_objDBWriteConnection.Commit();
_IsRolesAdded = true;
}


So please guide me how to add roles from C# function





plupload Codeigniter file upload issue-IO error 2032

i am using plupload script with codeigniter to upload files.I included all required files to make it work but now the issue is if i am trying to upload a image file large than 20 Kb than it will uploaded to the specific folder successfully but if you go and check the image file than only around 25% part of the image is visible other part is shown as a grey color.



and sometimes plupload shows IO error 2032 also.



I check my max post size and file upload size both are large enough to upload a 20 kb image file.



my script is:



    <script type="text/javascript">
// Convert divs to queue widgets when the DOM is ready
$(function() {
var cct = $.cookie("<?php echo $this->config->item("csrf_cookie_name"); ?>");
$("#uploader").pluploadQueue({
// General settings
runtimes : 'gears,flash,silverlight,browserplus,html5',
url : '<?php echo base_url();?>index.php/uploadphoto',
max_file_size : '5mb',
chunk_size : '1mb',
unique_names : true,
multipart:true,
multipart_params:{'<?php echo $this->security->get_csrf_token_name(); ?>': cct},


// Specify what files to browse for
filters : [
{title : "Image files", extensions : "jpg,gif,png,bmp,jpeg"}
],

// Flash settings
flash_swf_url : '<?php echo base_url();?>js/plupload.flash.swf',

// Silverlight settings
silverlight_xap_url : '<?php echo base_url();?>js/plupload.silverlight.xap'
});

});
</script>


and my controller upload photo is like this:



    $upload_path_url = './uploads/';



$config['upload_path'] =$upload_path_url;
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['max_size'] = '5500';

$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('file'))
{
$error = array('error' => $this->upload->display_errors());
//$this->load->view('upload', $error);
}
else
{
$data = $this->upload->data();
}


so give me any suggestion to solve this issue.



-Thanks in advance





Error while loading the data from server to core data model using restkit

i am using restkit in my project. i can sucessfully load data using the restkit and can store to data base by manually passing the loaded data to the coredata objects. Later i modified it to the restkit coredata model then i am getting the error like this. when run first time



reason = "The model used to open the store is incompatible with the one used to create the store";


my core data was set correctly and its working fine when i pass the data to keys manually.



i got a solution to delete the app or delete the app data from simulator file,
so i deleated the app from the simulator and again run it.this time i got the this error



 2012-05-18 17:38:45.610 MAW[85942:15203] W restkit.network:RKObjectLoader.m:281 Unable to find parser for MIME Type 'text/html'
2012-05-18 17:38:45.612 MAW[85942:15203] W restkit.network:RKObjectLoader.m:309 Encountered unexpected response with status code: 200 (MIME Type: text/html -> URL: http://api.artworkmanager.com/v1auth?username=anuj.gakhar%40gmail.com&password=1234 -- http://api.artworkmanager.com/v1 -- http://api.artworkmanager.com/v1 -- http://api.artworkmanager.com/v1)
2012-05-18 17:38:45.614 MAW[85942:15203] Encountered an error: Error Domain=org.restkit.RestKit.ErrorDomain Code=4 "The operation couldn't be completed. (org.restkit.RestKit.ErrorDomain error 4.)"


Hear is my code



// Initialize RestKit
RKObjectManager* objectManager = [RKObjectManager objectManagerWithBaseURLString:@"http://api.artworkmanager.com/v1"];
objectManager.client.requestQueue.showsNetworkActivityIndicatorWhenBusy = YES;

RKManagedObjectStore* objectStore = [RKManagedObjectStore objectStoreWithStoreFilename:@"MAW.sqlite"];

RKManagedObjectMapping* customerMapping = [RKManagedObjectMapping mappingForClass:[Customer class]inManagedObjectStore:objectStore];
[customerMapping mapKeyPath:@"phone" toAttribute:@"phone"];
[customerMapping mapKeyPath:@"organization" toAttribute:@"organization"];
[customerMapping mapKeyPath:@"siteAdministrator" toAttribute:@"siteAdministrator"];
[customerMapping mapKeyPath:@"customerType" toAttribute:@"customerType"];
[customerMapping mapKeyPath:@"id" toAttribute:@"id"];

RKManagedObjectMapping* articleMapping = [RKManagedObjectMapping mappingForClass:[User class]inManagedObjectStore:objectStore];
[articleMapping mapKeyPath:@"email" toAttribute:@"email"];
[articleMapping mapKeyPath:@"fullname" toAttribute:@"fullname"];
[articleMapping mapKeyPath:@"id" toAttribute:@"id"];

// relationship mapping
[articleMapping mapKeyPath:@"customer" toRelationship:@"customer" withMapping:customerMapping];

[[RKObjectManager sharedManager].mappingProvider addObjectMapping:articleMapping] ;

[self loadData];


my delegate methods



-(void)loadData{
// fetch your mapping
RKObjectMapping *customerMapping = [[RKObjectManager sharedManager].mappingProvider objectMappingForClass:[Customer class]];
//request data
[[RKObjectManager sharedManager] loadObjectsAtResourcePath:@"auth?password=1234&username=anuj.gakhar@gmail.com/customer" objectMapping:customerMapping delegate:self];
}
- (void)objectLoader:(RKObjectLoader*)objectLoader didLoadObjects:(NSArray*)objects {
RKLogInfo(@"Load collection of: %@", objects);

}
- (void)objectLoader:(RKObjectLoader*)objectLoader didFailWithError:(NSError*)error {
NSLog(@"Encountered an error: %@", error);

}
- (void)request:(RKRequest*)request didLoadResponse:
(RKResponse*)response {
if ([request isGET]) {
if ([response isOK]) {
NSLog(@"Retrieved JSON: %@", [response bodyAsString]);
}
} else if ([request isPOST]) {
if ([response isJSON]) {
NSLog(@"Got a JSON response back from our POST!");
}
}
}


can any one help me how can i solve this. and also suggest me in using restkit. As i am new to iphone feeling little bit difficult to with rest kit. Shall i continue with this or suggest any other alternatives i will go them. if not suggest me a good resorce to follow restkit. now i am going through this restkit in github https://github.com/RestKit/RestKit/blob/master/Docs/Object%20Mapping.md



EDIT
I have tried by running the example twiter code provided in the restkit https://github.com/RestKit/RestKit/tree/master/Examples/RKTwitterCoreData . It is also giving the same error, what might be the problem????(either ios / restkit) i have found an issue here https://github.com/RestKit/RestKit/issues/321. any ideas? thank you...





Spinlock Versus Semaphore!

What is the basic differences between Semaphores & Spinlock? & In what best situations or conditions, we can use these.