I have a button in my project that inserts a record into my SQLite database.
but there is an issue cus I have unique index so if the insert is a dup it will error out and quit the function.. how can I prevent from showing an error and quitting?
SQLite error (2067): abort at 11 in [INSERT INTO my_success VALUES('123456')]: column myids is not unique
A first chance exception of type 'System.Data.SQLite.SQLiteException' occurred in System.Data.SQLite.dll
Probably not the best solution, but you should be able to catch the exception:
try
{
// put your SQLite insert code here...
}
catch(System.Exception)
{
}
Related
My senario is: We have a production and an integration database and I wrote a tool to migrate some of the data of the production database to the integration database. For this I use: Entity Framework 6.0, .NET-Framework 4.5.2 and the databases are MS SQL Server Standard (64-bit) 13.0.5102.
My problem: While saving the deletion of all data in the integration database, the SaveChanges method throw an DbUpdateConcurrencyException with the error message
Store update, insert, or delete statement affected an unexpected number of rows (0). Entities may have been modified or deleted since entities were loaded.
The following is the code that through the exception:
using (var prod = new DbProdContext())
{
using (var #int = new DbIntContext())
using (var transaction = #int.Database.BeginTransaction(IsolationLevel.Serializable))
{
try
{
var stack = BuildStack(#int, ...); // buikds a stack of the tables where the latest pushed tables depdend only on already pushed tables
// Deleting everything in db
using (var x = log4net.NDC.Push("Deleting old content"))
{
foreach (var table in stack.Reverse())
{
var destSet = table.DestSet;
destSet.RemoveRange(destSet);
}
log.Info("Saving Changes...");
#int.SaveChanges(); // <-- throw exception
log.Info("Completed Saving Changes");
}
// code to insert the data
transaction.Commit();
}
catch (Exception ex)
{
// log
transaction.Rollback();
}
}
}
Note: The variable stack is a stack of the table in the integration database where for any given table t all table which t depends on are pushed onto the stack before t is pushed onto the stack.
Seoncond note: I tested the code with migration the data from a local test clone of the production database into the integration database which never throw this exception.
Does any body know how to prevent this error or what caused it.
Third note: The integration database it currently note in use to there could not be a concurrent change to the database from a different process.
Should I maybe just create sql drop statements for the tables instead of using destSet.RemoveRange(destSet)
Should I maybe just create sql drop statements for the tables instead of using destSet.RemoveRange(destSet)
Yes. You should do that. But if you don't want to recreate the tables, it should be DELETE or TRUNCATE TABLE instead of DROP TABLE.
Getting error saving the object in the database stage .I'm waiting for a suggestion for a solution from those who have information on the subject.
I checked my connection strings actually dont have much idea to try.
public void SaveNew(Person p)
{
using (var context = new FeatureContext())
{
context.People.Add(p);// error line
context.SaveChanges();
}
}
}
exception
System.Data.DataException: 'An exception occurred while initializing the database. See the InnerException for details.'
SqlException: Cannot attach the file 'C:\Users\ertug.dilek\source\repos\AddFeatures\WebApplication1\App_Data\AddFeatures.Model.FeatureContext.mdf'
as database 'AddFeatures.Model.FeatureContext'.
I am trying to add a data source to a DataGridView in ASP.NET, here is the error I'm getting:
An exception of type 'System.Data.Entity.Core.EntityCommandExecutionException' occurred in EntityFramework.SqlServer.dll but was not handled in user code
The code I'm trying to execute is:
using (MSAAnalyserEntities entities = new MSAAnalyserEntities())
{
UniRecView.DataSource = (from TableRecord in entities.TableRecords where TableRecord.Sector == "University" select TableRecord).ToList();
UniRecView.DataBind();
}
What I'm trying to achieve is that the GridView will display records from my database only if and when a specific column "Sector" has the data "University" in its column.
I can't seem to fix this error, please could you see what I'm doing wrong?
I'm converting a product from System.Data.OracleClient to Oracle.DataAccess.Client, and came across a question Here's a snippet of some code:
try
{
//some db code
}
catch (System.Data.OracleClient.OracleException ex)
{
if (ex.Code == 00904)
{
// log specific error
}
}
The problem is, Oracle.DataAccess.Client.OracleException has no Code property. It has a Number property. Is this the same thing? The docs say this about the property -
This error number can be the topmost level of error generated by
Oracle and can be a provider-specific error number.
The Code property contained the ORA- error code.
Yes, they are the same thing. Oracle.DataAccess.Client.OracleException.Number is the same info as System.Data.OracleClient.OracleException.Code.
What will be proper way to catch exception which throw SQL server when I deleting data with Reference constraint from C# code.
I want to show my users message like:
"I can't delete the data because is used"
, rather than of showing message like this:
The DELETE statement conflicted with the REFERENCE constraint ... The conflict ccurred*in database "rampa", table "dbo.doc", column 'kartica_id'.
Use this:
try
{
//Execute delete statement
}
catch (SqlException sqlEx)
{
//Handle exception
}
finally
{
//Close connection
}
All sql errors are thrown as SqlException and there are no specific errors. To find out what was error exactly, there is property SqlException.Number which is the same as SQL Error Code. You can find the list of codes here.
You can access the ConstraintException and map its fields however you want to your own preferred error output.
using System.Data;
try
{
// code that violates constraint
}
catch (ConstraintException exc)
{
// build output message you wish using exc.Message and other fields,
// return cleanly or rethrow as your own exception
}