I have created windows service for data-insertion.Time interval is one min.After one min, data insert into table.Data get inserted into table at multiple time.I don't want to that,only one time.How to do that?May I need to check in database wether entry is there or nor if not add.
You can use this query before inserting the data.
IF EXISTS(SELECT * FROM dbo.YourTable WHERE Name = #Name)
RETURN
-- here, after the check, do the INSERT
You might also want to create a UNIQUE INDEX on your Name column to make sure no two rows with the same value exist:
CREATE UNIQUE NONCLUSTERED INDEX UIX_Name
ON dbo.YourTable(Name)
Hope this help you.
//You can do like this in ur code
if (ChkRecordExist() == true)
{
//Do nothing
}
else
{
// insert operation
}
protected bool ChkRecordExist()
{
//here logic for record exist or not.
//if record is exist return true else return false
}
Related
I have a table the only has an Id column in SQL. It is an Auto-Increment column. It is used to keep track of a booking number sequence to be sent to an outside system. In SQL I use this to insert a new record:
insert into bookingnumbers default values
I would like to use entity framework and get the Next available Id. I have tried this:
private async Task<long> GetNextBookingNumberAsync()
{
BookingNumbers bookingNumber = default;
GhanemContext.BookingNumbers.Add(bookingNumber);
await GhanemContext.SaveChangesAsync();
return bookingNumber.Id;
}
However, booking number is just null and I get:
ArgumentNullException: Value cannot be null. Parameter name: entity
Any help would be greatly appreciated!
I need to run two statements like so:
Select amount from db where ID=5
DELETE from db where ID=5
Currently I prepare and run two different statements. I wonder if there is a way to combine it in one statement.
Basically all I need to do is to get an amount column from the row before it is deleted.
SQLite does not support this extension to standard SQL -- you do have to use both statements, SELECT first, DELETE next. You can wrap them in a transaction, of course, (BEGIN and COMMIT statements before and after will guarantee that), to guarantee atomicity and consistency.
If you just want to select rows and delete them in one pass, you can use the returning clause in the delete statement.
For instance:
delete from myTable returning *
The delete statement has all select functionalities possible such as with and where that permits to select rows with any logic.
Assuming that your calling thread/process has a unique identifier (e.g. thread_id), I think a viable approach would be to add a flag (say, "handlerid") to your table, which is set to null on insert, and then do:
update db set handlerid = <my_thread_id> where handlerid is null;
select * from db where handlerid is not null and handlerid = <my_thread_id>;
delete from db where handlerid is not null and handlerid = <my_thread_id>;
Not sure how it would perform vs a transaction but can't think of any reason it would be materially worse (might even be better), and using this approach the code seems about as straightforward as you can get. unlike a transaction, it won't require you to loop in the case that a transaction fails, in order to be sure that all outstanding elements that were in the table at the time of the most recent SELECT got processed.
It's late but for future visitor. In my case I did like below
I want to delete that ID where amount = 5 so I did this
public void selectAndDelete() {
try {
SQLiteDatabase db = this.getWritableDatabase();
int i=db.delete(table_name, "ID = (select ID from table_name where amount = 5)", null);
if(i>0)
{
// I'm inserting here new record
}
} catch (SQLException e) {
}
}
So, In your case you need to modify where condition like you want
You can do this by separating the two statements with a semicolon, e.g. (using the .NET port of SQLite):
using (SQLiteConnection conn = new SQLiteConnection("Data Source=fie.db3"))
{
conn.Open();
using (var cmd = conn.CreateCommand())
{
cmd.CommandText = "SELECT id FROM fies; DELETE FROM fies WHERE id = 5;";
using (var reader = cmd.ExecuteReader())
{
while (reader.Read())
{
Console.WriteLine(reader[0]);
}
}
}
}
I'm working around File Exchange (Export) using Data Import Export Framework (DIXF) , i want to add generate method to Find LineAmount Purchline associated with the receiving line VendPackingSlipTrans from PurchLine table.I create the following script but i need a help :
[DMFTargetTransformationAttribute(true),DMFTargetTransformationDescAttribute("Function that generate LineAmount"),
DMFTargetTransformationSequenceAttribute(11),
DMFTargetTransFieldListAttribute([fieldStr(DMFVendPackingSlipTransEntity,LineAmount)])
]
public container GenerateLineAmount(boolean _stagingToTarget = true)
{
container res;
PurchLine purchLine;
VendPackingSlipTrans vendPackingSlipTrans;
if (_stagingToTarget)
{
select firstOnly purchLine
where purchLine.LineAmount == entity.LineAmount &&
vendPackingSlipTrans.OrigPurchid == purchLine.PurchId &&
vendPackingSlipTrans.PurchaseLineLineNumber == purchLine.LineNumber;
if ( ! purchLine )
{
entity.LineAmount = purchLine.LineAmount ;
entity.insert();
}
}
res = [entity.LineAmount];
return res;
}
I have to export data from ax to file using DMF,so for that i have some field existing in VendPackingSlipTrans so added this fields in staging table but others field exist in other table like LineAmount.I don't know how to add this others fields in staging table. for that in myEnityclass i create generat method to associate field in source table. to staging table
So it seems you want to export VendPackingSlipTrans records with additional information from PurchLine records using a custom entity of the data import/export-Framework (DIXF). If that is correct, there are several problems in your implementation:
logic in if (_stagingToTarget) branch: since the framework can be used for both import and export, _stagingToTarget is used to distinguish between the two. If _stagingToTarget is true, data is imported from the staging table to the Dynamics AX target table. So you need to put the logic in the else branch.
selection of PurchLine record: the current implementation will never select a PurchLine record because values of an uninstantiated VendPackingSlipTrans table variable are used as criteria in the select statement. Also the chosen criteria are wrong, take a look at method purchLine of table VendPackingSlipTrans to see how to get the PurchLine record for a VendPackingSlipTrans record and use the target variable to instantiate the VendPackingSlipTrans table variable.
check if (! purchLine): This check means that if NO PurchLine record could be found with the previous select statement, the LineAmount of this empty record will be used for the staging record. This is wrong, instead you want to use the LineAmount of a record that has been found.
entity.insert(): as I mentioned in the comments, the entity record should not be inserted in a generate method; the framework will take care of the insert
A possible fix of these problems could look like this:
[
DMFTargetTransformationAttribute(true),
DMFTargetTransformationDescAttribute('function that determines LineAmount for export'),
DMFTargetTransformationSequenceAttribute(11),
DMFTargetTransFieldListAttribute([fieldStr(DMFVendPackingSlipTransEntity, LineAmount)])
]
public container GenerateLineAmount(boolean _stagingToTarget = true)
{
container res;
PurchLine purchLine;
VendPackingSlipTrans vendPackingSlipTrans;
if (_stagingToTarget)
{
// this will be executed during import
res = [0.0];
}
else
{
// this will be executed during export
// the target variable contains the VendPackingSlipTrans that is exported
vendPackingSlipTrans = target;
purchLine = vendPackingSlipTrans.purchLine();
if (purchLine)
{
res = [purchLine.LineAmount];
}
}
return res;
}
I have a table in that I have some duplicate record now I am getting the record without excluding duplicate records. I want to get all record excluding duplicate, and from duplicate data I want only one record. like if I have three same records i want only one record.
my query is like,this giving null pointer.
Cursor dCursor=database.rawQuery( "SELECT FROM note_image GROUP BY IDMATCHER",null,null);
and if I want to get this using query() method how can I do that. I am getting all records by using this method. but I want all records only once. without occurrence two time a record.
public Cursor fetchImage(){
String[] columns = new String[]{DatabaseHelper.NOTE_IMG_ID,DatabaseHelper.NOTE_HD_IMG_PATH,DatabaseHelper.NOTE_IMG_PATH_THUMB,DatabaseHelper.TEMP_IMG_ID};
Cursor cursor=database.query(DatabaseHelper.TABLE_NOTE_IMAGE,columns,null,null,null,null,null);
if (cursor!=null){
cursor.moveToNext();
}
return cursor;
}
I done using this query. taken help from above commented link. ` public Cursor fetchImage() {
Cursor icursor = database.query(true, DatabaseHelper.TABLE_NOTE_IMAGE, new String[]
{DatabaseHelper.TEMP_IMG_ID, DatabaseHelper.NOTE_IMG_PATH_THUMB},
null, null, DatabaseHelper.TEMP_IMG_ID, null, null, null);
if (icursor != null) {
icursor.moveToNext();
}
return icursor;
}`
How can i know specific value on a form using request.form?
I am trying it long but no success.
i want to check something like this
if (request.form.contains("text_check")) //But it doesn't work
{
go in;
}
else{
here we go;
}
i want to know specific value from AllKeys, and total count of all keys too.
To check if a key exists in the form data, you can simply compare the value to null:
if (Request.Form["text_check"] != null) {
If the key exists, you always get a string value back, even if the value is empty.
If you want to check if there is a non-empty value, you can use the String.IsNullOrEmpty method:
if (!String.IsNullOrEmpty(Request.Form["text_check"])) {
If you want to check if a certain key exists in the Request.Form collection you can do so like this:
if(Request.Form.AllKeys.Any(k => k == "text_check")) { ... }
and to then get it's value:
if(Request.Form.AllKeys.Any(k => k == "text_check"))
{
var textCheckValue = Request.Form["text_check"];
}
To get the total number of keys then:
var count = Request.Form.AllKeys.Count();
If you are using server side controls, you can use Request.Form.Contains(text_check.UniqueId) to make sure form is having that value during postback.