|
Hi,
I have always used the following coding when formatting dates for use with SQL:
bookingMadeDateTime.ToString("yyyy-MM-dd hh:mm:ss tt") <-- or very similar
however I have now been advised by a prospective user that they are using the following formats and the sql insert is failing:
Date: dd.MM.yy
Long date: dddd, dd' de 'MMMM' de 'yyyy
Time: hh:mm tt
The error is:
There was a syntax error in the date format. [ Expression = 2007-10-29 12:32:33 p.m. ]
The dates are entered using a datetime picker and I thought that by formatting the way I have, internationalization would be an issue. Also, the actual expression looks fine to me.
Can someone help me with this please.
Thanks.
Glen Harvy
|
|
|
|
|
Glen Harvy wrote: I have always used the following coding when formatting dates for use with SQL:
bookingMadeDateTime.ToString("yyyy-MM-dd hh:mm:ss tt") <-- or very similar
however I have now been advised by a prospective user that they are using the following formats and the sql insert is failing
Why would you be formatting dates for SQL Statements. Surely you would be passing this as a parameter? If you didn't use parameters your code would be highly susceptable to SQL Injection attacks, which would not be good.
Please read SQL Injection Attacks and Tips on How to Prevent Them[^] to secure your application. Incidentally it will also solve your current date formatting problem.
|
|
|
|
|
Colin Angus Mackay wrote: Why would you be formatting dates for SQL Statements. Surely you would be passing this as a parameter? If you didn't use parameters your code would be highly susceptable to SQL Injection attacks, which would not be good.
As the input is from a datetime picker and the user has no option but to select a date, I wasn't concerned with injection attacks.
I am happy to rewrite the code as you suggest ie paramaterized however I don't see this as solving the problem at hand ie the user hast his computer to use non-US English.
Does .Net automatically tanslate date formats?
Glen Harvy
|
|
|
|
|
Glen Harvy wrote: I am happy to rewrite the code as you suggest ie paramaterized however I don't see this as solving the problem at hand ie the user hast his computer to use non-US English.
Does .Net automatically tanslate date formats?
My PC is set to use Standard English rather than US English and I have no problems. If you use parameters then you don't have to worry about date formats, you just pass a DateTime object in to the parameter and set the SQL type of the parameter to DATETIME or SMALLDATETIME and that's all you need to do.
|
|
|
|
|
Thanks for this confirmation.
I have made some changes to the program as you suggest and it certainly seems to be working OK.
Glen Harvy
|
|
|
|
|
Further to your suggestion regarding parameters, how do I add this in an Insert Statement. I currently have the following Stingbuilder:
....
commandRecDetails.Append(" VALUES ( '");
commandRecDetails.Append(bookingMadeDateTime.ToString("yyyy-MM-dd hh:mm:ss tt") + "',");
commandRecDetails.Append(" " + courtNumber + ", ");
commandRecDetails.Append(" " + thisBookingNumber.ToString() + ", '");
etc etc....
recordBookingDetails.CommandText = commandRecDetails.ToString();
I can define the paramater
recordBookingDetails.Parameters.Add("DateMade", bookingMadeDateTime);
but how do I include it to replace the DateTime value in the string above?
Thanks.
Glen.
Glen Harvy
|
|
|
|
|
Your SQL String will look like this:
... VALUES(@dateMade, @courtNumber, @thisBookingNumber)
everything beginning with an @ is a parameter name.
Then add it with
recordBookingDetails.Parameters.AddWithValue("@dateMade", bookingMadeDateTime);
or
recordBookingDetails.Parameters.Add("@dateMade", SqlDbType.DateTime).Value = bookingMadeDateTime;
|
|
|
|
|
It doesn't get any better than this ...
Thanks again.
Glen Harvy
|
|
|
|
|
Always insist on ISO 8601 compliant date and time formats.
|
|
|
|
|
Within the multithreading enviroments, there is an exclusive lock provided by the .Net, which will block all other threads trying to enter the critical section if currently there is a threading holding this critical section.
But I'm trying to find a unblocking lock whick is exclusive but won't block other threads, just let them return and inform them the critical section is not available for them to access. Anyone knows if there is this kind of lock?
Thanks a lot;)
|
|
|
|
|
lock (obj) {
}
is just a syntax sugar for:
System.Threading.Monitor.Enter(obj);
try {
} finally {
System.Threading.Monitor.Exit(obj);
}
You should take a look at the other Monitor methods, there's a method called "TryEnter".
|
|
|
|
|
Thanks very much, it's really helpful!
|
|
|
|
|
Hi guys
Does anyone know how to cast the text of a textBox into a integer in C#?
Thanks and best wishes
|
|
|
|
|
You can't cast it. Use Int32.TryParse method.
[ My Blog] "Visual studio desperately needs some performance improvements. It is sometimes almost as slow as eclipse." - Rüdiger Klaehn "Real men use mspaint for writing code and notepad for designing graphics." - Anna-Jayne Metcalfe
|
|
|
|
|
Look at the Convert class. Type "Convert." and see what comes up in IntelliSense. You'll find Convert.ToInt32() is probably what you want. While it is not as save as Int32.TryParse(), it is quick and dirty.
Hogan
|
|
|
|
|
Most native numeric types in c# 2.0 have a "TryParse" method attached. An example method that would return the integer from a TextBox.Text property would be something like this:
<br />
public static int GetIntFromTextBox(TextBox textbox)<br />
{<br />
if(textbox != null && !string.IsNullOrEmpty(textbox.Text))<br />
{<br />
int myInt;<br />
<br />
string myNumberString = textbox.Text;<br />
int.TryParse(myNumberString, out myInt);<br />
<br />
return myInt;<br />
}<br />
return 0;<br />
}<br />
This will work for floats, doubles, etc as well. Just change the "datatype" in the TryParse line (double.TryParse() instead of int for instance). In the cases where the string is not a number, I believe it assigns zero (0), so the evaluation may not be neccessary.
-------------------------
Spiffdog Design
It's ok.. he's no ordinary dog...
|
|
|
|
|
In a C# project in VS 2005, when I add a reference to a DLL in the Solution Explorer (using the Browse Tab in the Add Reference dialog) I have to specifiy the full path. That means that if the DLL is a Debug DLL bin\Debug\ appears in the path. So how do I reference the Release DLL for the Release configuration?
Thanks in advance
-- modified at 5:52 Tuesday 30th October, 2007
|
|
|
|
|
You don't set a reference to the .DLL, you set it to the project that builds that .DLL, so long as the project is part of the same solution.
|
|
|
|
|
I'm afraid the project isn't part of the solution.
-- modified at 5:53 Tuesday 30th October, 2007
|
|
|
|
|
Then you have to add the reference to the Release version manually. There is no way to automatically switch between the two.
|
|
|
|
|
Hi ,
I'm having some trouble. My winform app is running SQL Queries , and I would like some sort of a progressbar to be displayed while the query is running (just a progress bar that constantly moves back & forth - I'm using the Circular Progress bar - http://www.codeproject.com/vb/net/sql2005circularprogress.asp[^]
Anyhow - It seems that I need to create a new thread to run the progress bar - If anyone would be so kind and explain me the basics on how to implement this - I would really appreciate it.
Thanks again.
|
|
|
|
|
The progress bar is UI. I would imagine you need a new thread to run the query. System.Threading.ThreadPool.QueueUserWorkItem and IsInvokeRequired will get you on the right path.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
Thanks. I'd appreciate more information on how to use the IsInvokeRequired exactly ? Sorry but I'm not so familier with threading.
Plus - how do I prevent my app from continuing while waiting for the Query running thread to finish ?
|
|
|
|
|
I will have to recommend looking through WinForms threading articles for that answer as it begins to be a deep topic. The Methods I gave you (and maybe it is InvokeRequired instead) should allow you to find the exact articles that will suit your needs.
Need a C# Consultant? I'm available.
Happiness in intelligent people is the rarest thing I know. -- Ernest Hemingway
|
|
|
|
|
Hello. I'm attaching a delegate which throws an exception to the BackgroundWorker.DoWork event, but the exception isn't caught by the worker and passed to RunWorkerCompleted event. Apparently it has something to do with asynchronous calling of delegate, since if I make the equivalent named function and attach that to DoWork, the exception is caught as expected.
(My goal is to make a function "RunInBackgroundWithProgressDialog" so that I can launch long functions like:
<br />
RunInBackgroundWithProgressDialog(delegate(){<br />
}); )
Offending code:
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
BackgroundWorker w = new BackgroundWorker();
w.DoWork += delegate(object o, DoWorkEventArgs e)
{
Console.WriteLine("sleeping 1000");
System.Threading.Thread.Sleep(1000);
throw new Exception("thrown");
};
w.RunWorkerCompleted += delegate(object o, RunWorkerCompletedEventArgs e)
{
if (e.Error != null)
{
Console.WriteLine("should be handled here, but isn't");
}
};
w.RunWorkerAsync();
Console.Read();
}
}
}
|
|
|
|