Converting DateTimePicker control value to sql database smalldatetime

samir_gambler

Member
Joined
Jul 12, 2012
Messages
6
Programming Experience
1-3
I have a form containing a DateTime picker control and I have a table in an SQL database containing a field of type smalldatetime. I want to store the date from the form into the table.

I tried something like
VB.NET:
Me.EmployeeTableAdapter1.InsertQuery(Me.EmployeeIDTextBox.Text, Me.LastNameTextBox.Text, Me.FirstNameTextBox.Text, Me.TextBox4.Text, Me.AddressTextBox.Text, Convert.ToInt32(Me.TelephoneTextBox.Text), Me.CivilStatusTextBox.Text, Convert.ToDecimal(Me.DailyRateTextBox.Text), [COLOR=#ff0000]Me.DateHiredDateTimePicker.Value[/COLOR], Convert.ToInt32(Me.TinTextBox.Text), 0)

but I am getting an exception

System.FormatException was unhandled
Message="Input string was not in a correct format."
Source="mscorlib"
StackTrace:
at System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
at System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt)
at System.Decimal.Parse(String s, IFormatProvider provider)
at System.Convert.ToDecimal(String value)
at StudyCase.frmAddEmployee.Button1_Click(Object sender, EventArgs e) in E:\work\POS_ronmon2210138262011\StudyCase\StudyCase\frmAddEmployee.vb:line 42
at System.Windows.Forms.Control.OnClick(EventArgs e)
at System.Windows.Forms.Button.OnClick(EventArgs e)
at System.Windows.Forms.Button.PerformClick()
at System.Windows.Forms.Form.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.Control.ProcessDialogKey(Keys keyData)
at System.Windows.Forms.Control.PreProcessMessage(Message& msg)
at System.Windows.Forms.Control.PreProcessControlMessageInternal(Control target, Message& msg)
at System.Windows.Forms.Application.ThreadContext.PreTranslateMessage(MSG& msg)
at System.Windows.Forms.Application.ThreadContext.System.Windows.Forms.UnsafeNativeMethods.IMsoComponent.FPreTranslateMessage(MSG& msg)
at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
at System.Windows.Forms.Application.Run(ApplicationContext context)
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[] commandLine)
at StudyCase.My.MyApplication.Main(String[] Args) in 17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()


Please clarify

Thanks
 
When an exception occurs you need to read all the information provided. This issue has nothing whatsoever to do with the date. Look at the call stack, which is a list of methods that were called leading to the exception:
at System.Convert.ToDecimal(String value)
at StudyCase.frmAddEmployee.Button1_Click(Object sender, EventArgs e) in E:\work\POS_ronmon2210138262011\StudyCase\StudyCas e\frmAddEmployee.vb:line 42
Why would Convert.ToDecimal be called if it had anything to do with a date? On the other hand, I can see a call to Convert.ToDecimal in your code and it is being passed a String, so logically it is telling you that that String is not in a valid format to be converted to a Decimal. It is never safe to assume that free text input by the user is valid. You must always validate it yourself.
 
Back
Top