I'm using an SQL Server 2008 database which contains an Geometry column and I'm now having problems loading and manipulating that data in CSharp when it hasn't been a problem before.
I have an object which is of the Microsoft.SqlServer.Types.SqlGeometry type and I need to get the STNumGeometries i.e.:
var numberOfGeometries = tmpDelytas[i].Delyta.DelytaGrans.STNumGeometries();
but it is causing an error:
ArgumentException 24144: This operation cannot be completed because the instance is not valid. Use MakeValid to convert the instance to a valid instance. Note that MakeValid may cause the points of a geometry instance to shift slightly.
I checked the Geometry value in SQLServer and STIsValid there reports that is is valid. (Unsurprising as the code has previously worked).
The geometry is valid according to STIsValid both in CSharp and in SQLServer, and STNumGeometries works in the database and if I put a breakpoint there then the value of STNumGeometries is reported as 1 in the watch. But still the program crashes with this error if I step forward.
Why might my code be suddenly unwilling to process the line of code?
Update
I've found a "solution", but would still like to understand the problem because this stinks...
var geomText = tmpDelytas[0].Delyta.DelytaGrans.ToString();
var geom = SqlGeometry.STGeomFromText(new SqlChars(geomText),0);
var numberOfGeometries = geom.STNumGeometries();
And this works.
Related
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?
I have a simple Linq query that is failing suddenly. This worked up until yesterday and I have not been able to determine the problem.
The query is simply getting a complete view from SQL Server
using(JobEntities JE = new JobEntities())
{
var BatchData = (from x in JE.vwBatchObjectActiveBatches select x).ToList();
}
Starting yesterday this line gets a
NullReferenceException (Object reference not set to an instance of an object)
My suspicion was that a user put in bad data causing the view to fail on SQL Server, but I have checked SQL Server itself and the view runs fine and populates with data.
This query was running in the middle of a larger function loading data from many places, so I have created a test case where I simply load the main window and run this query directly in the code behind to make sure that nothing else is affecting it, and the query still fails. All other Linq queries that I run in this project work still, only this one fails. The app is not under any production right now, and has been static for several months at least.
When I look at the JE in the watch window I can see the vwBatchObjectActiveBatches and it lists 164 records in the Local section -- this matches the view results on SQL Server. Expanding the Results View shows the null error again.
How can I find and fix whatever is causing this query to fail? Why does the results view show an error but the local Line shows the data that I am attempting to get?
It looks like your database returns NULL where Entity Framework does not expect/allow it. Data returned should be in accordance with the definition of its datamodel objects.
Solution: either 'fix' the data, or fix the query that produces it, or change the definition of your datamodel objects to allow NULL for the conflicting field(s).
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
I have a very strage problem with the SqlQuery.ExecuteAsCollection<T>() method and I am not sure whether this is related to SubSonic, my database or .NET.
When the following line gets executed:
FlowerCollection myCollection = sqlQuery.ExecuteAsCollection<FlowerCollection>();
my application crashes with the IndexOutOfRangeException that occured in System.Data.dll and the Additional Information containing the name of a column in the Flower table (which does exist both in the database itself and in the generated SubSonic class). However when I execute the same line in the Command Widnow or in the Watch window I get the result I expect without any errors.
I tried to load the debug symbols for .NET but in this case there seem to be no source code available so I am not able to debug it like that.
Have you got any ideas what else I can try to find the bug?
EDIT: I just added a try/catch around this code block and it throws exeptions for each column in the Flower table.
I found a workaround...
my original SqlQuery was constructed with an argument limiting the columns in the result:
SqlQuery sqlQuery = new Select(new String[] { Flower.Columns.Name }).From(Tables.Flower)
after I removed the argument from the Select constructor it runs without throwing any exception:
SqlQuery sqlQuery = new Select().From(Tables.Flower)
Why it worked in the Command & Watch in spite of this remains a mistery...
I am updating an object of type X and its children Y using LINQ to SQL and then submitting changes and getting this error
Example Code
X objX = _context.X.ToList().Where(x => x.DeletedOn == null).First();
objX.DeletedOn = DateTime.Now;
EntitySet<Y> objYs = objX.Ys;
Y objY = objYs[0];
objY.DeletedOn = DateTime.Now;
_context.SubmitChanges();
On SubmitChanges() I get an exception "1 of 2 Updates failed", no other information as to why that happened. Any ideas?
Also the exception type is
ChangeConflictException
Sooo what was the cause of the problem
- A trigger
I did a sql profiler and saw that
When ObjY's DeletedOn property got updated a trigger updated
ObjX's property (value in table) called CountOfX
which led to an error as the SQL created by LINQ to SQL had the old CountOfX value in it.
Hence the conflict.
If you ever get this error - SQL profiler is the best place to start your investigation
ALSO NOT RELATED TO THE QUESTION
I am testing LINQ to SQL and ADO.net Framework, weirdly this error happened in LINQ to SQL but not in ADO.net framework. But I like LINQ to SQL for its Lazy Loading. Waiting for EF to get outta beta
I'm not sure what the cause of the error may be exactly, but there seem to be a number of problems with the example you've provided.
Using ToList() before the Where() method would cause your context to read the entire table from the DB into memory, convert it to an array; and then in the same line you immediately call Where which will discard the rows you've loaded, but don't need. Why not just:
_context.X.Where(...
The Where method will return multiple items, but the second line in the example doesn't appear to be iterating through each item individually. It appears to be setting the DeletedOn property for the collection itself, but the collection wouldn't have such a property. It should fail right there.
You are using DateTime.Now twice in the code. Not a problem, except that this will produce ever so slightly different date values each time it is called. You should call DateTime.Now once and assign the result to a variable so that everything you use it on gets identical values.
At the point where you have "Y objY = objYs[0]" it will fail if there are no items in the Y collection for any given X. You'd get an index out of bounds exception on the array.
So given this example, I'm not sure if anyone could speculate as to why code modeled after this example might be breaking.
In LINQ2SQL Data Context diagram select the Entity and the field where the count is stored. (A denormalized figure)
Now set the UpdateCheck = Never.
I had this kind of issue. I was debugging running single lines at a time. It turned out another process was modifying this record.
My manual debugging process was slowing down the normal speed of the function. When I ran it all the way to a line after the SubmitChanges method, it succeeded.
My scenario would be less common, but the nature of this error relates to the record becoming superceded by another function/process. In my case it was another process.