Use session as source of field for dynamic data column - session

I have a Dynamic Data column that I want to be populated by a field from the Session collection. (So that when the user chooses to manipulate data related to that column, where necessary, they will see information based on that field, which is stored in session).
Is there an attribute I need to use, or create?

I have had success creating an extended property in the metadata :
public partial class MyTable
{
[ScaffoldColumn(true)]
public string MyValueFromSession
{
get
{
return Session["theAttribute"].ToString();
}
}
}
then you can specify a new column called MyValueFromSession in the MyTable metadata.
Hope this helps.

Related

Get record real value after overriding it

I've overridden a a record value like the following :
public function getWeightAttribute($weight)
{
if($weight)
{
return $weight;
}
return 70;
}
Now I have a collection of that model and I want to know if the original value was null or not.
I want to do it without connecting to DB again, Actually I want to make sure the user has filled the weight and some other fields while registering.
Some sections are working based on the above override and I don't want to mess them up neither use extra connections to db.
Thanks In Advance,
In order to get the original value, you can use:-
$fetchData->getAttributes()['weight'];

How can I delete all records from a table?

I've been searching for an answer on how to delete ALL records from a table using LINQ method syntax but all answers do it based on an attribute.
I want to delete every single record from the databse.
The table looks like so:
public class Inventory
{
public int InventoryId { get; set; }
public string InventoryName { get; set; }
}
I'm not looking to delete records based on a specific name or id.
I want to delete ALL recods.
LINQ method syntax isn't a must, bt I do prefer it since it's easier to read.
To delete all data from DB table I recommend to use SQL:
Trancate Table <tableName>
Linq is not meant to change the source. There are no LINQ methods to delete or update any element from your input.
The only method to change you input, is to select the (identifiers of the )data that you want to delete in some collection, and then delete the items one by one in a foreach. It might be that your interface with the source collection already has a DeleteRange, in that case you don't have to do the foreach.
Alas you didn't mention what your table was: Is it a System.Data.DataTable? Or maybe an Entity Framework DbSet<...>? Any other commonly used class that represents a Table?
If you table class is a System.Data.DataTable, or implements ICollection, it should have a method Clear.
If your tabls is an entity framework DbSet<...>, then it depends on your Provider (the database management system that you use) whether you can use `Clear'. Usually you need to do the following:
using (var dbContext = new MyDbContext(...))
{
List<...> itemsToDelete = dbContext.MyTable.Where(...).ToList();
dbContext.MyTable.RemoveRange(itemsToDelete);
dbContext.SaveChanges();
}

How to get data from intermediate table Laravel?

How to get data from third table through model?
I have model Prototype and PrototypeField model.
Where Prototype is:
id | name
Where PrototypeField is:
prototype_id | field_id
Also there is table Field that contains name of fields:
id | name
How to get names from table Field throught model 'Prototype' where Prototype relative with PrototypeField by:
Prototype.id = PrototypeField.prototype_id
So, Prototype can has one or many PrototypeField.
Your prototype model should have a relationship inside it which specifies the fields it should be related to for example:
public function fields()
{
return $this->belongsToMany('App\Fields'); // Change to location of fields model
}
Your fields model should also contain a relationship to specify which prototypes it relates to, for example:
public function prototypes()
{
return $this->belongsToMany('App\Prototypes'); // Change to location of prototype model
}
Once you have set these up, you will then be able to select the fields which the prototype belongs to using the following:
Prototype::first()->fields; //This selects the first prototype and gets the associated fields.
The inverse of which would be:
Fields::first()->prototypes //This selects the first field and get associated prototypes.
You can find out lots of information by reading the documentation on Laravel models - https://laravel.com/docs/5.4/eloquent-relationships

eloquent return two columns all records

I'm sure this is very simple - I just need two columns from a country database to populate a <select>
Looking at this URL I came up with the code below, but this seems to return an object for each record
http://laravel.com/api/class-Illuminate.Database.Eloquent.Model.html#_all
class countries extends Eloquent
{
}
public static function getCountrySelect()
{
return countries::all(array("name","iso_3166_2"));
}
If you want to populate custom field from database use this:->
countries::lists('name','iso_3166_2');
i.e
countries::lists('coulmnname1','coulmnname2');
and if you want to get suppose first 10 entries for that model use this:
countries::take(10)->get();

ADO.NET DataRow - check for column existence

How do I check for the existence of a column in a datarow?
I'm building datatables to organize some data that I've already pulled back from the database. Depending on the type of data in each row, I need to create a datatable with different columns. Then, later on, I want to check and see if the datatable I am looking at has a certain column.
I know I can catch the exception and handle it that way, but I'm curious if there is a property or method on the datarow object that will do this for me?
Here's how I can do it by catching the exception:
public static String CheckEmptyDataRowItem(DataRow row, String rowName, String nullValue)
{
try
{
return row[rowName].ToString();
}
catch (System.ArgumentException)
{
return nullValue;
}
}
You can simply check like this:
return row.Table.Columns.Contains(columnName);
DataTables have that schema info, so check if the Row's Table's Columns collection contains the field.

Resources