Remove record from Database in Linq - asp.net

My picture is :
I want to remove record from Tbl_Category table.
Tbl_category have childs and its child has child and .... to Tbl_File
How I can remove Tbl_Category records ?
Thank you
In Tbl_user , "username" is Primary key and the session username is :
string u = Session["Username"].ToString();
could you please help me to write the query ?

Try this query to delete category from category table object. categoryID is the id you want to delete from table
Tbl_Category category= dbContext.Tbl_Category.Single(x => x.CategoryID== categoryID);
if(category!=null)
{
dbContext.Tbl_Category.Remove(standard1);
dbContext.SaveChanges();
}
Also check the WillCascadeOnDelete() method on context is turned ON or OFF

Related

How can I use the Doctrine QueryBuilder to select a column using a foreign key?

I have a query to find the sum of points for an individual player in a given tournament. It works well except that the value for the "User" is returned as the foreign key id for that user.
$q = $this->createQueryBuilder('s')
->select('IDENTITY(s.user) as user', 'SUM(s.points) as points')
->groupBy('s.user')
->andWhere('s.tournament = :tournament')
->setParameter('tournament', $tournament)
->orderBy('points', 'DESC')
;
Is there a way that I can select the user's username instead? I'd expect it's possible, instead of having to make another query for every user in the result.
Just join the user table and add username to select query:
$q = $this->createQueryBuilder('s')
->select('IDENTITY(s.user) as user', 's.username', 'SUM(s.points) as points')
->leftJoin('s.user','user')
->groupBy('s.user')
->andWhere('s.tournament = :tournament')
->setParameter('tournament', $tournament)
->orderBy('points', 'DESC');

Filter Product Name in RetailProductDiscount Form productLookup ax 2012

I need to add the field of ProductName filterable in the ProductLookup field in retailPeriodicDiscount form. It is strongly connected to EcoResProduct and I can't use any other lookup field or method.
So, I need to add the product name field to productLookup method. Any suggestions how to do it?
For the product name you have to add the field Name of the table EcoResProductTranslation .Add the table as a joined datasource to the query and filter it by the system language.
sysTableLookup.addLookupfield(fieldNum(EcoResProduct, DisplayProductNumber));
sysTableLookup.addLookupfield(fieldNum(EcoResProduct, SearchName));
sysTableLookup.addLookupfield(fieldNum(EcoResProduct, ProductType));
sysTableLookup.addLookupfield(fieldNum(EcoResProductTranslation, Name));
if (_groupMember.Category)
{
query = RetailGroupMemberLineQueryProvider::containedProductsQuery(_groupMember.Category, true /*includesubcategories*/, _dataAreaId);
}
else
{
query = RetailGroupMemberLineQueryProvider::containedProductsQuery(_groupMember.Category, true /*includesubcategories*/, _dataAreaId);
}
// add datasources and join
qbdsProduct = query.dataSourceTable(tableNum(EcoResProduct));
qbdsProductTranslation = qbdsProduct.addDatasource(tableNum(EcoResProductTranslation));
qbdsProduct.relations(true);
// range for system language
qbdsTranslation.addRange(fieldNum(EcoResProductTranslation, LanguageId)).value(SystemParameters::getSystemLanguageId());
sysTableLookup.parmQuery(query);

How can i insert data from one table to another in symfony2 doctrine2

I have table called
tasks---id ,name,groupid
user----id,username , groupid
Now i have another table
userTasks with fields id,name, taskid , userid
Now i want that when the new user is created or persisted in database then
all the tasks with same groupid asuser should gets inserted in
usertasks table with taskid and userid
HOw can i do that
I am thinking of puting the logic in postFlush() event
But i don't know how doctrine first select and then insert the records in that table
If your do it just after of insert the data, instead inside postFlush function, could be more confortable and easy.
$om->persist($user);
$om->persist($task);
$om->flush();
$usertask = new UserTask($id, $name, $user->getId(), $task->getId());
$om->flush();
$om is the ObjectManager; EntityManager is deprecated in Doctrine last version.
If you want to use postFlush() function, maybe have to declare both id's as private variables to have the info.

matching fields in a database, ASP.net (VB)

In a table I have news posts with these fields:
Title
Content
OwnerID
And a users table
ID
Name
Surname
The OwnerID relates to the ID in the users table, how can I get the name of the user who's ID matches the OwnerID?
I'm writing a website in ASP.net (VB).
You would need to join the two tables together like this:
select users.Name
from news inner join users
on OwnerID = ID;
This query has no where clause to filter the results that are returned so this query would return all users who are associated with a news record. If you wanted to find users associated with a specific news record you would need to filter on the news title or content like this:
select users.Name
from news inner join users
on OwnerID = ID
where Title = 'Some Title';

How to get last inserted row ID from WordPress database?

My WordPress plugin has a table with a AUTO_INCREMENT primary key field called ID. When a new row is inserted into the table, I'd like to get the ID value of the insertion.
The feature is to using AJAX to post data to server to insert into DB. The new row ID is returned in the AJAX response to update client status. It is possible that multiple clients are posting data to server at the same time. So, I have to make sure that each AJAX request get the EXACT new row ID in response.
In PHP, there is a method called mysql_insert_id for this feature.But, it is valid for race condition only if the argument is link_identifier of last operation. My operation with database is on $wpdb. How to extract the link_identifier from $wpdb to make sure mysql_insert_id work? Is there any other way to get the last-inserted-row id from $wpdb?
Thanks.
Straight after the $wpdb->insert() that does the insert, do this:
$lastid = $wpdb->insert_id;
More information about how to do things the WordPress way can be found in the WordPress codex. The details above were found here on the wpdb class page
This is how I did it, in my code
...
global $wpdb;
$query = "INSERT INTO... VALUES(...)" ;
$wpdb->query(
$wpdb->prepare($query)
);
return $wpdb->insert_id;
...
More Class Variables
I needed to get the last id way after inserting it, so
$lastid = $wpdb->insert_id;
Was not an option.
Did the follow:
global $wpdb;
$id = $wpdb->get_var( 'SELECT id FROM ' . $wpdb->prefix . 'table' . ' ORDER BY id DESC LIMIT 1');
Get the last inserted row id in WP like this.
global $wpdb;
$order = ['product_name'=>'Computer', 'os_system'=>'Linux'];
$wpdb->insert('wp_product_orders', $order );
$last_inserted_id = $wpdb->insert_id;
Something like this should do it too :
$last = $wpdb->get_row("SHOW TABLE STATUS LIKE 'table_name'");
$lastid = $last->Auto_increment;
just like this :
global $wpdb;
$table_name='lorem_ipsum';
$results = $wpdb->get_results("SELECT * FROM $table_name ORDER BY ID DESC LIMIT 1");
print_r($results[0]->id);
simply your selecting all the rows then order them DESC by id , and displaying only the first
Putting the call to mysql_insert_id() inside a transaction, should do it:
mysql_query('BEGIN');
// Whatever code that does the insert here.
$id = mysql_insert_id();
mysql_query('COMMIT');
// Stuff with $id.

Resources