Copy DataTable to DataTable keeping the PrimaryKey - datatable

I need to copy dt2 into dt1 but I can't use dt1 = dt2 because dt1 has a primary key while dt2 does not. What's the best way to do this?
Is is possible to add a primary key to an existing dataTable which is not empty?
Thanks

I was just searching for how to set primary key - found solution:
dt2.PrimaryKey = new DataColumn[] { dt2.Columns["ColumnName"] };
I needed this for merging datatables, so it might help to copy one into an other.

Related

Find max value of a column in laravel

The problem started because I have a table (Clientes), in which the primary key is not auto-incremental. I want to select the max value stored in a column database.
Like this select, but with eloquent ORM (Laravel):
SELECT MAX(Id) FROM Clientes
How can I do this?
I tried:
Cliente::with('id')->max(id);
Cliente::select('id')->max(id);
I prefer not to make a simple raw SELECT MAX(ID) FROM Clientes
I cannot make it.
Thanks all!
The correct syntax is:
Cliente::max('id')
https://laravel.com/docs/5.5/queries#aggregates
Laravel makes this very easy, in your case you would use
$maxValue = Cliente::max('id');
But you can also retrieve the newest record from the table, which will be the highest value as well
$newestCliente = Cliente::orderBy('id', 'desc')->first(); // gets the whole row
$maxValue = $newestCliente->id;
or for just the value
$maxValue = Cliente::orderBy('id', 'desc')->value('id'); // gets only the id
Or, if you have a created_at column with the date you could get the value like this
$maxValue = Cliente::latest()->value('id');
Relevant Laravel Documentation: https://laravel.com/docs/5.5/queries#aggregates
$maxValue = DB::table('Clientes')->max('id');
Cliente::where('column_name', $your_Valu)->max('id') // You get any max column
We can use the following code :
$min_id = DB::table('table_name')->max('id');
https://laravel.com/docs/8.x/queries#aggregates

GetAllValues from datatview in a webix

how to get all values from dataview in a webix,
this still doesn't work by me, because the table is not to be selected
var data = $$('experienceTable');
console.log(data.getSelectedId())
To get some specific record
console.log(data.getItem(id));
And to get all records
console.log(data.serialize())

How to get the indexvalue of a column with a relation set in QSqlRelationalTableModel?

In QsqlRelationalTableModel foreign keys are resolved to human readable strings, if a relation for the column containing a foreign key is set. In my application stationids are resolved to stationnames.
For some purposes i need the stationid too. QsqlRelationalTableModel.data() or QsqlRelationalTableModel.itemData() only return the displayValue (for displayrole as well as for editrole). How can i get the corresponding foreign key (indexValue)?
QsqlRelationalTableModel.relationModel() returns a QSqlTableModel object for accessing the table for which column is a foreign key.
http://doc.qt.io/qt-5/qsqlrelationaltablemodel.html#relationModel
By the setFilter() method of relationModel() the corresponding id (indexValue) can be found, if the displayValue is unique:
rm = self.relationModel(<index column>)
f = '<columnname> = "{}"'.format(<displayvalue>)
rm.setFilter(f)
id = rm.data(rm.index(0,0))
I mean you can change qsqlrelationaltablemodel.cpp file like this:
QString QSqlRelationalTableModel::selectStatement() const
......
//!!! my
fList.append(QLatin1String(", "));
fList.append(relTableAlias);
fList.append(QLatin1String("."));
fList.append(relation.indexColumn());
fList.append(QLatin1String(" as "));
fList.append(relation.tableName());
fList.append(QLatin1String("_"));
fList.append(relation.indexColumn());
I'v tested it on Qt 4.8.1.
Second variant to get you wish right result is to create own class inherited by QSqlTableModel that make as you need.
First you have to reimplemented selectStatement function and create your own behavier.
Here is a beta version of QSqlRelationalTableModelMod1 class for example:
https://kkmspb.ru/development/Qt/database-sql/Extended-Qt/QSqlRelationalTabelModelMod1/
Unfortunetly by russian only.

DataTable Clone

ds = (DataSet)Session["Details"];
DataTable dt = ds.Tables[0];
DataTable temp = dt.Clone();
dt.Rows.Add(ds.Tables[0].Select("ID =" + ID));
Error message:Unable to cast object of type 'System.Data.DataRow' to type 'System.IConvertible'.Couldn't store <System.Data.DataRow> in Date Column. Expected type is DateTime.
can anybody help me please.
ImportRow is designed for this kind of scenarios only, so check it out. I have used it manytimes for such requirements.
I think it's a fault in the overloads. If you were working in VB, I would tell you to use the row:=ds.Tables[0].Select("ID =" + ID) notation in the parameters, but I don't think C# has this.
I think the Add function assumes you're feeding it the array of values for the new row, instead of a datarow itself.

LINQ not updating on .SubmitChanges()

Is there any reason something like this would not work?
This is the logic I have used many times to update a record in a table with LINQ:
DataClasses1DataContext db = new DataClasses1DataContext();
User updateUser = db.Users.Single(e => e.user == user);
updateUser.InUse = !updateUser.InUse;
db.Log = new System.IO.StreamWriter(#"c:\temp\linq.log") { AutoFlush = true };
db.SubmitChanges();
(updateUser.InUse is a bit field)
For some reason it isn't working. When I check the linq.log it is completely blank.
Could there be a problem with my .dbml? Other tables seem to work fine but I've compared properties in the .dbml and they all match.
It's as if the db.SubmitChanges(); does not detect any updates being required.
The table could not be updated properly because it had no primary key. (Actually it had the column but the constraint was not copied when I did a SELECT INTO my dev table). The DataContext class requires a primary key for updates.
Is the InUse property a "normal" one as far as LINQ is concerned? (e.g. it's not autogenerated or anything funky like that?)
Alternatively, I don't suppose it's a Nullable<bool> is it, with a current value of null? If so, your update line isn't actually doing anything - for nullable Booleans, !null = null.

Resources