Linq returning wrong value from DB - asp.net

I have a super simple select in a wcf service like
BusinessModel.Candidate candidateObject
= dcMUPView.Candidates.SingleOrDefault(dev => dev.Username == username);
But when I hit this code, and then check the candidateObject, it's candidateid value is incorrect. In the DB candidateid is the identity column and primary key. In this case it is 2572884 in the DB but when I look in the candidateObject is says the value returned is something like 0x00274254. Anyone know what might cause something like this?

I think your query is returning the correct record, since 0x00274254 (base 16) = 2572884 (base 10).
If you are checking the value using the debugger, make sure you have the Hexadecimal Display option turned off (see here for more info: Visual Studio debugger - Displaying integer values in Hex).

Related

Except string datatype, not getting any columns populated through AIF Service

I have a simple custom table in AX, for which i need to create a Web service so that data could be populated by an external system. I created a Document service with all the Axd* and Ax* objects involved. Next i wrote a simple console application to try populating some dummy data. Interesting thing is i can only get string data type columns populated. Int, Real, Date, Enums etc are not coming through.
In the code below, i am only getting StoreItemId and Barcode columns, as both are strings. Cost is real, ErrorType is Enum, DocketDate is date and none of them get any values. I have discussed this issue with many colleagues and none is aware whats happening. Does anyone know or could point me in some new direction? Thanks a lot.
PS - I have limited experience with AIF and if i am missing something fundamental, please excuse and do let me know. Thanks.
AxdEntity_MMSStagingSalesImport stagingSalesImport = new AxdEntity_MMSStagingSalesImport();
stagingSalesImport.StoreItemId = "9999";
stagingSalesImport.Barcode = "1234546";
stagingSalesImport.Cost = 22;
stagingSalesImport.ErrorType = AxdEnum_MMSImportErrorType.Posting;
stagingSalesImport.DocketDate = new DateTime(2014, 4, 4);
stagingSalesImport.IsDuplicate = AxdEnum_NoYes.Yes;
For some types, you have to specify that you have set the values, so that AX knows the difference between a null value and a value that is set:
stagingSalesImport.Cost = 22;
stagingSalesImport.CostSpecified = true;
stagingSalesImport.ErrorType = AxdEnum_MMSImportErrorType.Posting;
stagingSalesImport.ErrorTypeSpecified = AxdEnum_MMSImportErrorType.Posting;
Thanks for replying Klaas, i like your blog as well.
Should have responded earlier but i fixed the issue.
I didn't try Klaas's option but i had a look at the data policies for the inbound port and found that none of the columns were enabled. I enabled the ones i needed and made most of them required as well. And guess what, that worked. I was expecting that the columns should have been enabled by default.

Using sqlite database with qt

Here is my code, there doesn't seem to be anything wrong:
QSqlDatabase db=QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("thedata.sqlite");
db.open();
QSqlQuery query;
query.prepare("SELECT lastname FROM people where firstname='?' ");
query.bindValue(0, lineEdit->text());
bool x = query.exec();
if(x){
lineEdit_2->setText(query.value(0).toString());
}
else {
QSqlError err;
err = query.lastError();
QMessageBox::about(this,"error",err.text() );
}
When the program is working always it gives the error parameter count mismatch I'm using qt 4.8 and its own headers for using sqlite.
I would be very thankful for any advice, though I searched in google i see many posts in this issue but nothing helped me.
Thank you.
You're prepared statement is wrong, it should be:
quary.prepare("SELECT lastname FROM people where firstname=?");
Notice that there are no single quotes (') around the placeholder. If you put the quotes, it gets passed as a literal to the database, leaving you with a parameter-less query and code that passes too many parameters.
(Changing that variable name to query would be a nice touch too.)
Also you need to check the return value if QSqlQuery::prepare, and print out/display the error message you're getting from that if it fails – otherwise QSqlQuery::exec resets the current error and you'll get a pretty meaningless error message if there was a problem parsing the prepared statement.
if(x){
lineEdit_2->setText(quary.value(0).toString());
}
This is incorrect too. The you need to call (and check the return value of) query.next() to position the result set to the first row returned (if there is one). You can't use .value(X) before you've called .next().

How to get inserted row ID with WebMatrix

There is a GetLastInsertId method in WebMatrix. However, using Reflector, I can see that it's implemented like this:
[return: Dynamic]
public object GetLastInsertId()
{
return this.QueryValue("SELECT ##Identity", new object[0]);
}
Which, as far as I can see, is a very bad way of doing it, because ##Identity returns the last insert considering every table and every session. I need this restricted to the scope and session I'm working with, so I was expecting this to use SELECT SCOPE_IDENTITY(), since that also seems to be what is most often used according to my reading.
My questions are:
Do the WebMatrix wrappers do something that makes this ok to use in my case?
If not, is there a simple way to get the inserted ID of an insert query using WebMatrix classes, or should I fall back on stuff like SqlClient for this?
I am interested in answers for SQL Server (not compact) if that makes a difference.
I had the same problem, and I read these answers but still couldn't get the code to work correctly. I possibly was misreading the solutions above, but what seemed to work for me was as follows:
var db=Database.Open(...);
var lastInsertID = db.QueryValue("INSERT name into Names; SELECT SCOPE_IDENTITY()");
To quote David Fowler, one of the devs on the Data APIs in WebMatrix: "[SCOPE_IDENITTY] doesn't work on ce". Since we wanted a data API that would work on both CE and regular SQL Server, we used ##Identity. So if you want to use SCOPE_IDENTITY specifically, you should use db.QueryValue method:
var db = Database.Open(...);
db.Insert(...);
var lastInsertId = db.QueryValue("SELECT SCOPE_IDENTITY()")
here is my solution
var db.Database.open('..');
var lastId = db.GetLastInsertId();

Event log messages get overrriden by another event log

I create event logs for asp.net projects error logging. I do it by adding a key in regedit, and then a sub-key.
Sometimes, I create a new key and sub-key, and instead of getting a new, empty event log, I see in the event viewer that it's showing me the logs from a different project. I'm not able to find a pattern as to when this happens.
Has anyone encountered such a problem? Am I doing something wrong?
You probably want to use the EventLog.CreateEventSource API to do this - it should take care of any details for you.
A quick read thru the docs seems to show that the 1st 8 characters are checked for uniqueness...perhaps that's where your issue is?
Edit: From Reflector, the API does this...
Check for invalid characters ("non printable" based on Unicode category, \, *, ?)
Checks that the created reg key will be <= 254 characters
Checks if the source is already registered
Checks that the log name isn't reserved (AppEvent, SecEvent, SysEvent)
Checks for another log with the same beginning 8 chars
Checks that the log name doesn't exist as a source
Creates the log subkey
Initializes the log subkey with default values (MaxSize = 524288, AutoBackupLogFiles = 9. Retention = 604800, File = %SystemRoot%\System32\config\logName.Substring(0, 8) + ".evt")
If OS is not > Windows NT 5.x (Vista or higher), creates a multi string value on the logkey with logName and source name. Or, if value exists, appends source name to the existing array.
Creates a subkey for source
Initializes the source subkey with default values (EventMessageFile, ParameterMessageFile, CategoryMessageFile, CategoryCount)
It seems that the problem was that we had already created an event log with that name, and even though we deleted it, it didn't help. The solution was to create an event log with a different name.

Does "insert" in SQLite return SQLITE_OK or SQLITE_DONE?

What does statement "insert" in SQLite return in case of success?
I always believed that it should be SQLITE_DONE, but recently in my logs I found out the following string:
sqlite3_step error: 'not an error'
And here is the code that logs the mentioned string:
prepareStatement(addTranslationStmt2, "INSERT INTO translations(lang1_wordid, lang2_wordid) VALUES(?, ?)");
if (!addTranslationStmt2) return -2;
sqlite3_bind_int(addTranslationStmt2, 1, word_id);
sqlite3_bind_int(addTranslationStmt2, 2, translation_id);
if(sqlite3_step(addTranslationStmt2) != SQLITE_DONE)
{
NSLog(#"sqlite3_step error: '%s'", sqlite3_errmsg(database));
sqlite3_reset(addTranslationStmt2);
return -1;
}
sqlite3_reset(addTranslationStmt2);
I am wondering, why does it work in most cases.
Should I change SQLITE_DONE in my code to SQLITE_OK?
Thanks.
SQLITE_DONE
http://www.sqlite.org/c3ref/step.html
You could also try printing out the error code to find out what the problem is.
In cases like these, I like to look at code samples. Here are some good ones:
http://sqlite.phxsoftware.com/forums/p/76/6659.aspx
The SQLite Result Codes Reference lists SQLITE_OK as indicating a successful result. It is also the first error code, having an error code of 0, making it the canonical result (i.e. the result I would expect on a successful operation).
You should put a breakpoint or print statement in your code to find out if it really is returning zero, and check your data to make sure you're getting the result you expect. If that all checks out, I would change your condition to check for SQLITE_OK.
The details of the behavior of the sqlite3_step() interface depend on whether the statement was prepared using the newer "v2" interface sqlite3_prepare_v2() and sqlite3_prepare16_v2() or the older legacy interface sqlite3_prepare() and sqlite3_prepare16().
In the legacy interface, the return value will be either SQLITE_BUSY, SQLITE_DONE, SQLITE_ROW, SQLITE_ERROR, or SQLITE_MISUSE. With the "v2" interface, any of the other result codes or extended result codes might be returned as well.
Switching from "Debug" configuration to "Release" resolved the issue for me.

Resources