How to set date to NULL in ODBC - odbc

I am using XOJO and pervasiveDB via ODBC.
There is a form that I need to update and on that form I have a date field that can be deleted on the form. When the date is deleted on the form, I need it deleted in the DB as well.
I am using ODBC, and can't do it for some reason. I have tried sending NULL, nil, 0000-00-00 and it is simply not doing it.
I have even tried not to send it if the date is deleted hoping for the best, but it will simply not go away.
Any ideas?

I think I resolved this issue. This is the solution:
The only working solution, after trying many different things.
dim inTYP as string = "6010-03"
SQLupdateBLIN = "update BLIN set inDAT=NULL WHERE inTYP= '" + inTYP +"'"
If I send the inDAT in following format (send it as variable), then it is not working (no error, just does not work):
dim inDAT as string = "NULL"
It seems ODBC is not liking variables that have value set to null.

Using direct SQL will definitely work.
If you are using a RecordSet, then you want to set the column to NULL using syntax like this:
MyRecordSet.Field("inDAT").Value = Nil
But whether this works is completely dependent on the ODBC driver.

Related

Convert date in textboxes to date in SQL Server

I am working in ASP.NET as front end and SQL Server 2012 as backend. In ASP.NET, the user enters a date in 3 textboxes in the format DD/MM/YYYY.
Now I have concatenated these textbox values into a string. In SQL Server, I want to save this date into column DOB with a data type of DATE.
Below is concatenated string in ASP.net
string strDOB = txtYY.Text + "/" + txtMM.Text + "/" + txtDD.Text;
How can I now save this strDOB in SQL Server?
Stop using strings. Besides the advice in the comments to use a dedicated picker control, the next best advice is to parse those strings into integers (briefly), then construct a DateTime object from that and then do not let it get converted back into a string. It's when people let extra string/date conversions happen that they introduce formatting problems. So get the value:
var year = Int32.Parse(txtYY.Text);
var month = Int32.Parse(txtMM.Text);
var day = Int32.Parse(txtDD.Text);
var dob = new DateTime(year,month,day);
And then pass it to SQL Server:
var cmd = new SqlCommand(...);
cmd.Parameters.Add("#DOB",SqlDbType.Date).Value = dob;
And make sure that your query uses the parameter #DOB wherever you want to use that value.
Try this:
UPDATE [Table] SET [Column] = '' + strDOB + ''
Be careful, though... this hasn't been sanitized to guard against SQL Injection. I've only demonstrated the direct answer to your question, nothing more.
As a best practice, you should never concatenate together unchecked user input and store the result in a column. Techniques for preventing this are outside the scope of this Q&A, but are easily discovered with a minimal amount of searching.
--EDIT--
I defer to Damien's answer on this one. I agree with his syntax suggestions.

SQLite/FoxPro Update can't find record using = but finds it using LIKE

I'm converting a FoxPro database to SQLite, and migrating the instructions to update, where I found a problem.
If inside FoxPro I use Update Fact01 set Motivo = 'asdfgh' where TipoDoc='FV'
the rows are not updated.
But if I use Update Fact01 set Motivo = 'asdfgh' where TipoDoc Like 'FV' the rows are changed.
If I do the first instruction inside the SQLite engine, the rows are also changed. The field type for TipoDoc is NChar(2).
Also, If i do a select * from Fact01 where TipoDoc ='FV' statement inside Foxpro it works OK.
Any idea what's happening here?
I'm not sure if it's due to the fact that Nchar can store unicode data or the way data in general is stored. Wrapping an ALLTRIM around the WHERE clause may correct the problem.
Update Fact01 set Motivo = 'asdfgh' where ALLTRIM(TipoDoc)='FV'
This happened to me because i stored the column value as a BLOB instead of TEXT. Turns out the BLOB value is less than a TEXT value so LIKE found matches but = did not. you might be having the same issue.

XOJO delete a record from SQLite database

I am using Xojo 2013 Version 1. I am trying to delete a record from a SQLite database. But I am failing miserably. Instead of deleting the record, it duplicates it for some reason.
Here is the code that I use:
command = "DELETE * from names where ID = 10"
namesDB.SQLExecute(command)
I am dynamically generating command. but however I change it it always does the same. Same result with or without quotes.
Any ideas?
The very first thing I would do is check to see if there is an error being generated.
if namesDB.Error then
dim s as string = namesDB.errorMessage
msgbox s
return
end
It will tell you if there's a database error and what the error is. If there's no error then the problem lies elsewhere.
FWIW, always, always, always check the error bit after every db operation. Unlike other languages, Xojo does NOT generate/throw an exception if there's a database error so it's up to you to check it.
Try calling Commit().
I just made a sample SQLite database with a "names" table, and this code worked fine:
db.SQLExecute("Delete from names where ID=2")
db.Commit
I have done a lot of work with XOJO and SQLite, and they work well together. I have never seen a record duplicated erroneously as you report. That is very weird. If this doesn't help, post more of your code. For example, I assume your "command" variable is a String, but maybe it's a Variant, etc.
I think on SQLite you don't need the * between the DELETE and the FROM.

Classic ASP: RecordSet Field giving no value

I'm not really an ASP developer, so a little bit lost.
I have the following data access code:
sSQL = "SELECT answer_id, company_name, old_access_company_name, answer, flag_asker_notified FROM Q01_ask_sheiiba_answer_company2 WHERE question_id="& sQuestion_id &" ORDER BY answer_id"
rs.open sSQL, conn
DO WHILE NOT rs.EOF
Response.Write(rs.Fields("answer"))
rs.MoveNext
LOOP
I have tested that the sql query is built properly by outputting it to the response before it is called. It produces the following query:
SELECT answer_id, company_name, old_access_company_name, answer, flag_asker_notified
FROM Q01_ask_sheiiba_answer_company2
WHERE question_id=988
ORDER BY answer_id
When I copy that exact query to sql management studio and run it I get the expected results of 5 rows and each row containing data in every cell, BUT, when I run it through the above code, I get the same 5 rows with the same cell data, EXCEPT for the answer column, which is empty!
What am I missing?
Thanks in advance
There are two things you might want to try:
Put your text field at the end of the query. For example:
SELECT answer_id, company_name, old_access_company_name, flag_asker_notified, answer
If this doesn't give you the results, you might want to try:
WHILE NOT rs.EOF
theanswer=rs("answer")
Response.Write(theanswer)
rs.movenext
wend
Text and Memo fields can play a little havoc with ASP.
EDIT: Another thing that you can try is:
rs.CursorLocation = adUseClient
or
rs.CursorLocation = 3
The problem is that the ODBC driver can't access large text blobs as strings; you need to access them as chunked BLOB data.
I advise instead to dump the ODBC connection and connect using the OLE-DB driver directly. This will let you access that column as if it was just another varchar column.
I just had a similar problem (I think). I converted a varchar field to text. When I did so, I found that the text field seemed to "disappear" from my selected record set. I my case, I discovered that you can reference the text field ONLY ONCE. After that, it seems to disappear. Accordingly, for the text field, I now simply move it into a string variable and then operate on the string. That solved my problem.
John
Had the same problem and found the solution here https://web.archive.org/web/20170224013842/http://www.4guysfromrolla.com/aspfaqs/ShowFAQ.asp?FAQID=80
Basically when you open the recordset (not connection.execute) use the options adOpenKeyset (val 1) and adUseClient (val 3), and the text filed should be the last in your field list in strSql
example: rs.Open strSql, dbConn, adOpenKeyset, adUseClient

ASP.NET saving empty date value in SQL Server database with parameters

I have a web forms application which allows to update database columns. One of the database columns is Finish Date which is of date datatype in the SQL Server database.
I use SQL query with parameters and textbox to provide the new value for the finish date.
This is a code which gets me the value of the date from a text box within the grid:
TextBox tFD = (TextBox)grdProjectUpdate.Rows[e.RowIndex].Cells[11].FindControl("proFDTextBox");
then I use a parameter:
cmd.Parameters.Add("#proFD", SqlDbType.Date).Value = tFD.Text.Trim();
to update the value using this SQL statement:
UPDATE PMSprojects SET proFD = #proFD ,...
This solution works fine whenever there is an actual date provided. However, it does not save nulls. If I provide an empty string, it is being converted into 1900-01-01 date, even though the column in the database allows nulls.
How could I solve this issue and save null to the database?
You have to write code around this to handle, and pass DbNull.Value when the value is null.
cmd.Parameters.Add("#proFD", SqlDbType.Date).Value = (tFD.Text.Trim().Length > 0) ? (object)tFD.Text.Trim() : DbNull.Value;

Resources