MySQL Adapter Fill Exception - c#

I have a MySQL query I'm running from C#. I use the MySqlDataAdapter to fill a DataTable. However I am getting the following exception:
MySql.Data.Types.MySqlConversionException: Unable to convert MySQL date/time value to System.DateTime
I can't really modify the query (it's loaded from an external source). And I'm hoping I don't have to manually fill the data table. Anyone had this before?
I should also add I have tried adding allow zero datetime=no to the connection string with no success.

I think you made a mistake with your addition to the connection string! It should be:
Allow Zero Datetime = true

Related

Error even after changing datatype in both dataset and SQL Server Management Studio

I have a database and then a dataset in C# Visual Studio. In the database there is a table with a column called CardType. This was created by someone before me. And the value in this column is always 40. So he created it of type double.
I was told now that now the value has to be changed to SAR40 and not just 40. So now it is a string. So when I changed the value to SAR40 obviously there will be error when trying to add SAR40 to a column of type double. So first I went to SQL Server Management Studio and changed the type to nvarchar there. Then I come back to Visual Studio and changed the type of datatype of CardType to System.String.
Then when I run the program I still get an error
Parameter value could not be converted from string to double.
FormatException: The input string is in the wrong format.
How the program works is there are a bunch of small sections to fill data and each of the data goes into the database. Data like cardtype, hardwarerevision etc. And from this it will form a workrow.
workRow["CardType"] = type.Text;
workRow["Hardwarerevision"] = hw.Text;
and then finally add to the database using these two lines
//add new data row
ds.UNIO_CPU.Rows.Add(workRow);
//write data to database
DataGridAdapter_CPU.Update(workRow);
So why do I still get an error?
I try to run the program with just 40 and the program is showing no error. It works fine. How is that possible. Because I changed both in the SQL Server Management Studio and dataset in Visual Studio to string type. So here it should show error. But it is not showing error. Whereas I try to enter SAR40 then it is showing error that you cant convert string to double. What could be the error reason? Kindly help me. Thank you.
It is not clear what are the nature of the objects from your post. Also, it is not obvious where exception happens. From your sample, is it OK on string
ds.UNIO_CPU.Rows.Add(workRow);
And fails on
DataGridAdapter_CPU.Update(workRow);
Right?
And can you provide more details about DataGridAdapter_CPU?
In addition, DataSet contains DataTable with its own structure, did you check it?

Changing TableAdapter String

I am using tableadpters and datasets generated by wizards to connect a DataGridView to a source.
I changed databases for the application, but I am getting an error for the stored procedures that still have the "OldDb.dbo.sp_GetWorkbarTypes" on them. I did a search for the "OldDb" and changed everything to "NewDb" but I till get a connection error saying that it cannot find "OldDb.dbo.sp_GetWorkbarTypes".
How do I change the adapters to point to the new Database?
this.sp_GetWorkbarTypesTableAdapter.Fill(this.workBarTypesDataSet.sp_GetWorkbarTypes);
throws a
Invalid object name Invalid object name
where the heck is this getting this string from? I changed them in the find

Updating EF model throws ""Arithmetic overflow error converting expression to data type datetime"

I an create a new record with no issues, but whenever I update the model, even when the date isn't being changed, I get the "Arithmetic overflow error converting expression to data type datetime" error.
I have confirmed that the dates in the existing db record to be updated are valid.
The below code snippet, will fail even though no date field is being updated.
var dbModule = db.Modules.FirstOrDefault(v => v.ModuleId == module.ModuleId);
dbModule.Name = module.Name;
//dbModule.Password = module.Password;
//dbModule.Colour = module.Colour;
//dbModule.Modified = module.Modified;
db.SaveChanges();
If I look at the trace, I see the command below, so am certain that no date fields are being updated.
ADO.NET:Execute NonQuery "update [dbo].[Modules]
set [Name] = #0
where ([ModuleId] = #1)"
For clarity, the operating environment is:
ASP.NET MVC 3
.NET 4.5
SQL Server 2012 (Express)
Models generated by database first approach, with corresponding edmx.
DateTime model fields are of type DateTime in SQL Server
I have tried to use SQL Server Profiler, but it does't output the update statement to see the exact SQL, so am relying on the intellitrace provided above.
Any help would be greatly appreciated.
Aughhhhh! After days and days.
Using SQL Server Profiler, I found that I had to log both
RPC:Completed
SQL:BatchCompleted
That showed all the SQL statements, which allowed me to track it down to the fact that I had a trigger firing on the database, setting the Modified field, and using an incorrectly formatted string date.
BlinkyBill, check if the datetime field on your entity/db is nullable, and if you are using the correct mapping.
Also, you could try to explicity set this field on your update method.
Hope it helps!

How to fix "Not a legal OleAut date." when reading an Excel file in C#?

I have been working with excel spreadsheets and so far I never had any problems with them.. But this error,"Not a legal OleAut date.", showed up out of the blue when I tried to read an excel file. Does anyone know how I can fix this. Here is the code I use to read the excel and put the data into a dataset. It has worked fine previously but after I made some changes (which doesn't involve dates) to the data source this error showed up.
var fileName = string.Format("C:\\Drafts\\Excel 97-2003 formats\\All Data 09 26 2012_Edited.xls");
var connectionString = string.Format("Provider=Microsoft.Jet.OLEDB.4.0; data source={0}; Extended Properties=Excel 8.0;", fileName);
var adapter = new OleDbDataAdapter("SELECT * FROM [Sheet1$]", connectionString);
DataSet Originalds = new DataSet();
adapter.Fill(Originalds, "Employees"); // this is where the error shows up
I sort of figured out a work around to this problem I changed the connection string to the latest oleDB provider.
var connectionString = string.Format("Provider=Microsoft.ACE.OLEDB.12.0;Data Source={0};Extended Properties='Excel 12.0 Xml;HDR=YES;'", fileName);
I also made sure that the empty space after the last row of my excel data is not being read as a value.
Look for different date formats, in my case one of the column was in dd/mm/yyyy format and the other was in mm/dd/yyyy. Once the date formats were rectified data import worked.
In my case, I had a date in my table inserted manually with the wrong format, it was 01/01/0019 instead of 01/01/2019.
I have this issue while reading excel in oledb.
I found that in excel many date columns are empty in the beginning records. i had 6000 records in excel and one of the datecolumn has around first 4000 empty cells so excel is not able to understand what to convert the cell so i added one dummy record fill with date and process my file. Or you can move few records which has date value in it at top
How can you fix this? Hi!jack your data with a dummy row at row 1 and force the column(s) in question into a string (in this case only - it is a data type error, so apply the fix according to the type).
It is necessary to understand what the data adaptor does, which interprets the data type of each column by examining, by default, the first 8 rows of data (sans header if HDR=Yes in connect string) and deciding on a data type (it can be over-ridden -yes there is an override - in the connection string to 16 rows - almost never very helpful).
Data adaptors can do other nasty things, like skip strings in columns of mixed data types, like string/double (which is really just a string column, but not to the adaptor if the first rows are all double). It won't even give you the courtesy of an error in this example.
This often occurs in data coming from ERP sources that contains "Free Form" columns. User defined columns are the usual suspects. It can be very difficult to find in other data type issues. I once spent quite a bit of time resolving an issue with a column that typed as a string with a max length of 255 chars. Deep in the data there were cells that exceeded that length and threw errors.
If you don't want to advance to the level of "Genetic Engineering" in working with data adaptors, the fastest way to resolve an issue like this is to hi!jack your data and force the column(s) in question to the correct type (or incorrect, which you can then correct in your own code if need be). Plan B is to give the data back to the customer/user and tell them to correct it. Good luck with Plan B. There is a reason it isn't Plan A.
More on manipulating via the connection string and similar issues with the adaptor - but be wary, results are not going to be 100% fool proof. I've tested changing IMEX and HDR settings extensively. If you want to get through the project quickly, hi!jack the data. OleDB & mixed Excel datatypes : missing data
Here is another posting similar in context, note all of the possible time consuming solutions. I have yet to be convinced there is a better solution, and it simply defies the logic a programmer brings to the keyboard every morning. Too bad, you have a job to do, sometimes you have to be a hack. DateTime format mismatch on importing from Excel Sheet

knowing if a string will be truncated when updating database

I'm working on a software that takes a csv file and put the data in a sqlserver. i'm testing it with bad data now and when i make a data string to long (in a line) to be imported in the database i got the error : String or binary data would be truncated the statement has been terminate. that's normal and that's what i should expect. Now i wanna detecte those error before the update to the database. Is there any clever way to detecte this?
The way my software work is that i importe every line in a dataset then show the user the data that will be imported. Then he can click a button to do the actual update. i then do a dataAdapter.Update( Dataset, "something" ) to make the update to the database.
The probleme is that the error row terminate all the update and report the error. So i want to detect the error before i do the update to the server so the other rows will be inserted.
thanks
You will have to check the columns of each row. See if one exceeds the maximum specified in the database, and if yes, exclude it from being inserted.
A different solution would be to explicitly truncate the data and insert the truncated content, which could be done by using SubString.
The only way that I know of is to pre-check the information schema for the character limit:
Select
Column_Name,
Character_Maximum_Length
From
Information_Schema.Columns
Where
Table_Name = 'YourTableName'
What you need is the column metadata.
MSDN: SqlConnection.GetSchema Method
Or, if you have opened a recordset on your database, another solution would be to browse the field object to use its length to truncate the string. For example, with ADO recordset/VB code, you could have some code like this one:
myRecordset.fields(myField) = left(myString, myRecordset.fields(myField).DefinedSize)

Categories