|
Robert Kamarowski wrote: What am I doing wrong?
Well, the most obvious thing is that you're writing code which is vulnerable to SQL Injection[^].
The ID should be passed as a parameter. Table and column names can't be passed as parameters, so you should verify that the name is one of the expected values, and cannot be modified by the user. Alternatively, since you only seem to have four tables, use a specific query for each table.
DataTable s can accept null values in columns of any type. You shouldn't get an error when you try to load a null date into a DataTable , so I'm not sure that your FillError handler is actually doing anything.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
The FillError handler is definitely working. I've walked through the code and it works. The error is happening during the Fill() method.
I don't think Injection is the problem here. The code works fine, except for when there is a date. If I run the search against SQLiteManager there are no errors. This is a self-contained application and does not go over the internet.
VS 2015
|
|
|
|
|
Robert Kamarowski wrote: I don't think Injection is the problem here
He's not saying that it is: he's giving you a serious warning that your code is wide open to accidental or deliberate damage (or deletion) of your database, and that it needs to be fixed throughout the whole of your application as a matter of extreme priority. It doesn't matter if the app is internet or local to "just your PC" - SQL injection is a real risk. Google "Bobby Tables" one day.
Your truncated data is exactly what you should get: the fill error terminates the fill operation.
I would change the DB design to not use null dates, but use the minimum sensible value instead.
Failing that, I'd use a query that didn't return null values, replacing them at the SQL end with a "sensible" default.
Once I'd fixed my code to avoid SQL Injection, of course.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
Robert Kamarowski wrote: This is a self-contained application and does not go over the internet.
That's nice. Most damage to databases occurs from inside the company, not outside, for a multitude of reasons. Don't get into the habit of writing bad code because you think "nothing's going to happen". You don't know the future of your code or application and you could end up putting this code into an environment where the risks are increased.
|
|
|
|
|
Robert Kamarowski wrote: The error is happening during the Fill() method.
OK, what's the error?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Error:
String was not recognized as a valid DateTime
|
|
|
|
|
That doesn't sound like a SQL error. It sounds more like an error from your C# code, trying to convert an invalid string value to a DateTime value.
Having said that, I don't know what SQLite will do if you pass a null or empty string to the datetime function, or if you pass in a string that's not in the correct format.
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
You are storing your dates in the DB as strings as well?
That's something else you need to change. And it's going to take longer because you have existing data in there that isn't a valid datetime value.
So create a new table using DATETIME values this time, and a program to transfer each row one-by-one after they have been converted, and log the rows that fail so you can manually work out which ones you need to fix. Fix 'em, and then delete the old table, and rename the new. Then change your app to insert DateTime values directly via parameterised queries.
Bad command or file name. Bad, bad command! Sit! Stay! Staaaay...
|
|
|
|
|
The dates are stored as DATE type.
|
|
|
|
|
The date are of Date type, and NULL is allowed. However, from a previous application/language, many of the Date fields are set to an empty string, and not null. This application is under development, so I have options Is there a way to identify an empty string during the Fill() method? I haven't found anything in my online searches, so I'm hoping someone has a trick up their sleeve...
|
|
|
|
|
Is there a DATE type in SQLite now?
SQLite does not have a storage class set aside for storing dates and/or times. Instead, the built-in Date And Time Functions of SQLite are capable of storing dates and times as TEXT, REAL, or INTEGER values.
And if you're not using SQLite, why are you using the SQLiteConnection , SQLiteCommand and SQLiteDataAdapter classes?
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Yes I'm using SQLite. SQLiteManager has a DATE type.
|
|
|
|
|
I'm trying to code this: (Sorry, formatting is all screwey)
public Task<Dictionary<int, string>> GetAllNamesAsync()
{
Dictionary<int, string> dict = new Dictionary<int, string>();
<pre>
Task<IList<InternalProviderViewDto>> task = Client.GetAsync(new InternalProviderSearch());
Task x = task.ContinueWith(t =>
{
IList<InternalProviderViewDto> results = t.Result;
foreach (var result in results)
{
dict.Add(result.Id, result.Name.ToString());
}
});
return //<=== WHAT GOES HERE???
}
I want to return the dictionary that I populated, but I'm not sure how to code this right.
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
Try this:
public Task<Dictionary<int, string>> GetAllNamesAsync()
{
Task<IList<InternalProviderViewDto>> task = Client.GetAsync(new InternalProviderSearch());
return task.ContinueWith(t => t.Result.ToDictionary(i => i.Id, i => i.Name));
}
If you're using .NET 4.5, you could use async and await instead:
public async Task<Dictionary<int, string>> GetAllNamesAsync()
{
IList<InternalProviderViewDto> results = await Client.GetAsync(new InternalProviderSearch());
return results.ToDictionary(i => i.Id, i => i.Name));
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|
|
Damn my slow sickly fingers today! You beat me by 3 minutes.
|
|
|
|
|
That did it. Thanks
If it's not broken, fix it until it is
|
|
|
|
|
Well, you're saying that GetAllNamesAsync() should be returning a Task, which returns a Dictionary<>. With the code, as written, there's nothing you can put in there to fix this. The whole method has to be rewritten a bit.
The second task you create, with ContinueWith doesn't specify a return type (void), but it looks like you're trying to return the Dictionary<> from that task, so it needs to be changed to specify that return type. I think you're looking for something more like this:
public Task<Dictionary<int, string>> GetAllNamesAsync()
{
Task<IList<InternalProviderViewDto>> task = Client.GetAsync(new InternalProviderSearch());
Task<Dictionary<int, string>> x = task.ContinueWith<IDictionary<int, string>>(t =>
{
IList<InternalProviderViewDto> results = t.Result;
Dictionary<int, string> dict = new Dictionary<int, string>();
foreach (var result in results)
{
dict.Add(result.Id, result.Name);
}
return dict;
});
return x;
}
|
|
|
|
|
I went with Richards answer as it's shorter, but your also works.
Thanks
If it's not broken, fix it until it is
|
|
|
|
|
His is definitely shorter and better. I just kept my example in the context of the code you already wrote.
|
|
|
|
|
Message Removed
modified 8-Dec-15 2:03am.
|
|
|
|
|
Message Removed
modified 8-Dec-15 2:03am.
|
|
|
|
|
Message Removed
modified 8-Dec-15 2:03am.
|
|
|
|
|
Message Removed
modified 8-Dec-15 2:03am.
|
|
|
|
|
Hello, how do I use Dispatcher.BeginInvoke() to work with fuction that doesn't returns and doesn't get any parameters ?
lets say :
"public void AddInk()
{
}"
I am inside a class and I am trying to use threading from a main window constructed on WPF in order to make the AddInk() work on the background.
from the main window I called :
Thread addI = new Thread(MyClass.ThreadInk);
public void ThreadInk()
{
//checks the CheckAcess() and using Dispatcher to call AddInk if CheckAcess() is false else: just call AddInk normally
}
I understand I should work with delegate on the Dispatcher,can someone give me an example how exactly and if I am doing it ok ?
B.when can I use Thread.Sleep() (I mean in which part of the program that I have described ?)
modified 6-Dec-15 12:34pm.
|
|
|
|
|
Try:
if (Dispatcher.CheckAccess())
{
AddInk();
}
else
{
Dispatcher.BeginInvoke((Action)AddInk);
}
"These people looked deep within my soul and assigned me a number based on the order in which I joined."
- Homer
|
|
|
|