Hey guys I am almost done with my CRUD project for my Database Project. I am just trying to finish up and complete the Delete Functionality.
query = string.Format("DELETE FROM customers WHERE `cid`= {0};", mDeleteTextBox);
My variable mDeleteTextBox is filled with the value I want.
What is wrong with my query?
ERROR MESSAGE
An unhandled exception of type 'MySql.Data.MySqlClient.MySqlException' occurred in MySql.Data.dll
Additional information: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '.TextBox, Text: 6' at line 1
Your additional information says it all: you're trying to pass mTextBox as a parameter for your query, but in order to access the content of the textbox itself (which is the data you want to use to complete your query), you should access the Text property of the textbox.
So, your code:
query = string.Format("DELETE FROM customers WHERE `cid`= {0};", mDeleteTextBox);
became
query = string.Format("DELETE FROM customers WHERE `cid`= {0};", mDeleteTextBox.Text);
Related
In my Mongo db collection have 2 date fields TimeStamp, OpEndTime, I need to take order by OpEndTime-TimeStamp. It is working with a small amount of data. For huge data throw exception out of ram space. So need to create the index with difference of OpEndTime and TimeStamp. I don't know how to create it. I am using C# mongo db driver
I tried like below
DBContext.ClientDb.Repository(collection).Indexes.CreateOne(Builders.IndexKeys.Ascending(i =>i.OpEndTime- i.TimeStamp));
but am getting error like below
An exception of type 'System.InvalidOperationException' occurred in MongoDB.Driver.dll but was not handled in user code
Additional information: Unable to determine the serialization information for i => Convert((i.OpEndTime - i.TimeStamp)).
I have a little problem with Entity Framework 6.
My application is a WPF C# application, I use SQL SERVER 2012 Express.
I try to insert data into my Person table.
It was working for a long time. Today I had an error : receiving an invalid column length from the client 46.
I searched and found some articles, they are talking about column sizes etc but in my case, tis is not the problem.
This code was working : dc.BulkInsert(listToInsert, options);
**using EntityFramework.BulkInsert.Extensions;**
//I have a list of person object to insert.
var listToInsert = PersonList.Where(ro => !ExistingPerson.Contains(ro.Pers_Code.ToLower())).ToList();
using(MyEntities dc = new MyEntities())
{
*//If I add items one by one, it works*
foreach (var item in listToInsert)
{
dc.Person.Add(item);
}
dc.SaveChanges(); //Success.
//But If I use Bulkinsert, I have an error message
BulkInsertOptions options = new BulkInsertOptions();
options.BatchSize = 1000;
dc.BulkInsert<Person>(listToInsert, options); // at this moment I have this error message : receiving an invalid column length from the client 46.
dc.SaveChanges();
}
I checked the data length of items, I didn't see any problem.
Does anyone have an idea ?
Thanks.
The SaveChanges use a SqlCommand. If the name is longer than the database limit, it will silently be truncated, so no error will be thrown.
The BulkInsert use a SqlBulkCopy, if the name is longer than the database limit, an error will be thrown.
That explain why you get an error in one case and none in the other case.
SqlBulkCopy doesn't raise this error for fun, so I would double check your length with your column size.
Perhaps the type is char(xyz) and there is space at the end?
Perhaps there is some space at the start?
etc.
.NET Fiddle support Entity Framework and NuGet packages. So, if you could reproduce it online, it could be possible to tell exactly why that happens.
Example: https://dotnetfiddle.net/35mQ0W
We are using an embedded Firebird V3.0.2 for our MVC5 Project
Problem: When we update the software, we want to find out if a table exists before we create the new table. Therefor we simply make a select on the desired table and if an exception with sql error code -204 occurs, then the table does not exist. But how can I resolve the error code -204?
Code:
var lFbCommand = new FbCommand();
lFbCommand.Connection = "MyConnectionstring";
lFbCommand.Connection.Open();
// check if table exists by an select query
lFbCommand.CommandText = string.Format("SELECT FIRST 1 * FROM {0}", "MyTableName");
try
{
pFbCommand.ExecuteNonQuery();
}
catch (FirebirdSql.Data.FirebirdClient.FbException e)
{
if(e.? == -204) // -204 seem to be the errorcode for "Table does not exists."
{
// table does not exist.
}
else
{
throw;
}
}
Note: We want to prevent parsing the message text in the exception. We want to get a clear flag for this error (e.g. the error code via any property).
You shouldn't look at the (so-called) SQLcode. SQLcodes are very unspecific, for example SQLcode -204 covers a broad range of errors (maybe dozens if not hundreds of sub-errors). Not just "table unknown", but also "trigger unknown", "Datatype unknown", etc.
You'd need to look at the error code (FbException.ErrorCode) (which is not the same as the SQLcode), although that is still tricky, because sometimes error codes are nested from unspecific (covering a similar range as the associated SQLcode) to more specific. And that is the case for "table unknown" (isc_dsql_relation_err or 335544580), which is nested in the group isc_dsql_error (335544569). The isc_dsql_error - IIRC - maps almost one-to-one to SQLcode -204 (which could be a few dozen to a few hundred errors).
As far as I know - but I don't usually program in C# - you will need to look at FbException.Errors (which returns a FbErrorCollection) and check if it contains the right error(s).
Please any one help me:
I run my application was created in C# for export data source t XML document, I also use try catch method for correct an error of database access (adapter.Fill(ds)). The problem is show below.
n_org:SELECTs_department.did,s_department.name,s_department.fk_deptFROMs_department
Index #0
Message: Incorrect syntax near ','.
LineNumber: 1
Source: .Net SqlClient Data Provider
Procedure:
Please give a suggestion or any correct code is ok.
Thanks
Your SELECT statement is all wrong - no spaces anywhere!
Instead of what you have right now:
SELECTs_department.did,s_department.name,s_department.fk_deptFROMs_department
use a statement that has the necessary spaces - like this:
SELECT s_department.did, s_department.name, s_department.fk_dept FROM s_department
I am getting a strange exception when trying to update a DataTable after flagging a row for deletion. It looks like it's validating the data in the row I'm trying to delete. I was able to save the same row successfully, why won't it let me delete it?
Using C# in VS 2008 and SQL Server CE.
The essential bits (I hope) of my code:
var listDataTable = new booksDataSet.ListDataTable();
this.tableAdapterManager1.ListTableAdapter.Fill(listDataTable);
listDataTable.Rows[0].Delete();
this.tableAdapterManager1.ListTableAdapter.Update(listDataTable);
booksDataSet1.List.AcceptChanges(); // exception here
The exception is:
System.FormatException was unhandled
Message="#p2 : foo - Input string was not in a correct format."
Source="System.Data"
I've reduced this to a very simple table:
Id (PK, int, not null)
Name (nvarchar(100), not null)
Notes (nvarchar(4000), null)
The data in the row I'm trying to delete (first row) is Name = "foo", so it is the correct row, but why does it care what the data is before deleting the row?
You need to do a trace on the app (try running it on your local copy of SQL Server and run the 'Profiler'); my guess is when the SQL query is hitting your SQL Server, part of it isn't in the correct format. I bet you'll see the problem when you run a trace.
I get the same response. If I bind with {0:c} it will fail on delete every time. Thinking it was the $ I tried {0:N2} same response. Remove it, deletes just fine.