I have an application which consists of simple schema of DB:
Networks 1-->* Shops
i use entityframework (Default EntityObject Code Generator) with winforms,
i use DataBinding to a grid to CUD these entities,
i have :
DbObjectModelContainer _context = new DbObjectModelContainer();
_context.ContextOptions.LazyLoadingEnabled = true;
NetworkBindingSource.DataSource = _context.Networks;
ShopsBindingSource.DataSource = NetworkBindingSource;
ShopsBindingSource.DataMember = "Shops";
NetworkBindingNavigator.BindingSource = NetworkBindingSource;
ShopBindingNavigator.BindingSource = ShopsBindingSource;
NetworkDataGridView.DataSource = NetworkBindingSource;
ShopDataGridView.DataSource = ShopsBindingSource;
all databinding is working good and synchronized, i can CUD on both grids on the Form and go to _context.SaveChanges() with no problem.
First Scenario
a simple scenario of Pressing "+"(add) on the NetworkBindingNavigator and right afterwards "X"(delete) on this empty line on the grid and finally i go to context.SaveChanges()
succeed without a problem.
Second Scenario
when i press "+"(add) on the ShopBindingNavigatorand then right afterwards i press "X"(delete) on this empty line on the grid and finally i go to _context.SaveChanges() i get :
System.Data.UpdateException: An error occurred while updating the entries. See the inner exception for details. ---> System.Data.SqlClient.SqlException: Cannot insert the value NULL into column 'Location', table 'MyDB.dbo.Shops'; column does not allow nulls
my question is why didnt it happen in the first scenario as well (i dont allow NULL in Networks table as well) ?
Thanks.
Cannot insert the value NULL into column 'Location', table 'MyDB.dbo.Shops'; column does not allow nulls means you table does not accept NULL.
so can you check in the table if the column is nullable or not. If it is not nullable then when you say it has succeeded in your first scenario have a look at you DB. Somehow you must be inputting some default values into the tables.
Related
I started working on a project in my work that doesn't have any documentation, and the person who developed the project in the first place isn't avalaible anymore.
There is this piece of code for doing and update to the database
_report = db.Report.Where(x => x.IdReport == ReportId).FirstOrDefault();
db.Report.Attach(_report);
_report.attr1 = reportmodel.attr1;
_report.attr2 = reportmodel.attr2;
_report.attr3 = reportmodel.attr3;
if (db.SaveChanges() != 0)
{
return View(reportmodel)
}
Looks fine and indeed does the update to the database in the table "Report", but additionally it is being inserted in another table "ReportLog" the detail of the change (orginal value, new value), I believe this is being done somehow in the SaveChanges().
So my question is where can I find where those insertions to the log table are being executed?
I have checked in the model if the table "Report" has some stored procedure mapped in the update action, checked for triggers and stored procedures in the database and used Find(Ctrl+f) to check for "ReportLog" in the entire solution, but I couldn't find where the insertion is being executed.
And something really weird is that this happens for the "Report" table only, using SaveChanges() for other updates in other tables does only what is expected
I found a trigger on the Report table that was doing the inserts
Situation:
I'm writing a Winforms app using C# in VS2013 with .NET4.0.
I have a datagridview bound to a dataadapter that loads data from a MySQL table. dataadapter SQL command have been built manually as some reformatting is needed to accord with date and boolean handling on the database. This is a multi user environment so data contention is possible. I therefore handle DBConcurrency exceptions when carrying out dataadapter.Update().
For testing the adapter is filled, bound to the dgv and then I use MySQLWorkbench to delete one of the displayed rows. If I then try to update that row in the dgv the DBConcurrency exception is thrown correctly.
Issue:
If however I only amend the row with MySQLWorkbench, then update through the dgv the DBConcurrency exception is not thrown and the update is successful. Hence any update that another user might have made gets lost. (NB I am ensuring that the MySQLWorkbench change is properly committed first!)
Question:
How do I get the DBConcurrency exception thrown when there is amend contention?
EDIT
This is actually a tabbed application with a number of datagridviews each linked to a MySQL table. All have the issue. A really simple example is a "maker" table consisting of:
maker_id int(11) not nullable auto-increment
maker_name varchar(127) not nullable
The SQL is generated through code and populated via parameters. Through the debugger just before datadapter.Update I see:
UPDATE maker SET maker_id = #maker_id, maker_name = #maker_name WHERE maker_id = #maker_id
I wrote an application which is working with strongly typed datasets and adapters.
First it reads data from database and store it to the typed datatable.Second this datatable processed by this application (add, delete or update rows) and last writes updated data back to the database.
On the step of updating sometimes application causes the exception of
Violation of PRIMARY KEY constraint 'PK_GPS_Events_History'. Cannot insert duplicate key in object 'dbo.GPS_Events_History'. The duplicate key value is (16805552).
The statement has been terminated."} System.Exception {System.Data.SqlClient.SqlException}
Table GPS_Events_History at the moment of exception contains about ~30000 rows.
How to filter data in datatable to look at this single row with ID=16805552? I tried to filter it by following code:
dataTable.where(row => row.ID == 16805552)
but VS2010 didn't allow me to enter any LINQ expression in debugger variables window.
Make the result you want to debug save to a variable, then inspect the variable:
var testData = dataTable.Where(row => row.ID == 16805552);
//once the line above has executed, quickwatch the "testData" variable
You can't put lambdas in a quickwatch window, so this is the next best thing.
I typed following command in quick watch window and it solved the problem:
Select("ID = 16805552")
i want to set a property value in a table to default auto increment,but their are no options to do so in lightswitch2012 to my knowledge which is given that i recently started learning lightswitch,very light.
ok heres the real problem,this is the table
[customer][id,customer_id,name]
i want to set customer_id by default to id unless it is manually changed to different value.
how to acheive this?
In the Entity designer make your Customer_ID not required.
Write Code for Customers_Inserted.
Then, check to see if the Customer_ID is null. If it is, copy the ID field to it.
private void Customers_Inserted(Customer entity)
{
if (entity.Customer_ID == null) {
entity.Customer_ID = entity.ID;
}
}
You're right, there is no "auto-increment" data type available in LightSwitch. The ID property auto-increments, but that's a special case, handled by LightSwitch.
If you were attaching to an external SQL database, if you added a column that was an Integer Identity column, although it'll just appear as an Integer property in LightSwitch, it would still auto-increment because that's actually done in the SQL database itself.
The problem with all auto-increment properties is that you won't get the actual value until the record is saved.
Can I ask why you need an auto-increment property?
I may be misunderstanding what you are trying to achieve, but if you are using either a table or a grid, and you want to set the values for various entities for each new row your user adds (like customer_id = id, etc.), you can use the _Changed method and Add event to programmatically set any of the new row entities.
If this is along the lines of what you're looking for, watch Beth Massi's video How Do I: Copy Data from One Row into a New Row? You should be able to adapt her code to accomplish what you have in mind I think.
How to make all columns allow null before adding a new row to the datatable .
dt.Rows.Add(dt.NewRow());
This line throws an exception
Column XXX does not allow nulls
How to fix this problem.
You don't add the row until it's filled and ready to be saved.
DataRow row = dt.NewRow();
...fill row with values...
dt.Rows.Add(row);
Usually the database designer specifies that a column can't be null for a reason. For example, it might be a primary key or a foreign key, or is otherwise mandatory information.
If you are sure that it is OK to provide no data for this column for this particular record, try passing an empty string.
It is usually better to initialize the whole row in memory before sending it to the database (instead of filling it field-by-field when it is already there).
However, if you absolutely must do that, and if your DBMS supports it, you can declare your NOT NULL constraints as deferred, so they are not checked until the transaction commits. Here is an Oracle example.
It's rarely good solution. Start from redesigning your database.
Consider removing NOT NULL constraints from all not necessary fields.
Also if any fields is obligatory, but you still do not want to fill it during row creation, set default value either in database or middle layer (ORM or whatever)
edit:
however, in this case it looks like you're just trying to pass an empty row to db, before initializing it with data. That will never work ;-)