I am new to Visual Studio 2010, so I'm not sure if I'm wording this correctly. I am creating a web database to enter / update data in my SQL 2008 R2 database. I am using the MaskedEditExtender and Validator for my date field. I can enter the date in my
web page - no problems, and the MaskEditExtender and Validator are doing what they're supposed to do. I am having a problem when I try to get this date's value to update my SQL database. All other fields add or update perfectly. The field is named DateRequested.
When I look at the data, it's empty even though I added it. I am getting the following error: Conversion from string to type 'Date' is not valid
Can someone help me? I can't figure this out.
Here is my code for this MaskedEditExtender and Validator date field:
aspx
<asp:MaskedEditExtender ID="DateRequested_MaskedEditExtender"
runat="server"
TargetControlID="DateRequested"
Century="2000"
Enabled="true"
Mask="99/99/9999"
MaskType="Date"
MessageValidatorTip="true"
OnFocusCssClass="MaskedEditFocus"
OnInvalidCssClass="MaskedEditError"
AcceptAMPM="True"
AutoComplete="True"
AutoCompleteValue='Date'
UserDateFormat="MonthDayYear" ClearMaskOnLostFocus="False"
ClearTextOnInvalid="True" DisplayMoney="Left" />
<asp:TextBox ID="DateRequested" runat="server"></asp:TextBox>
<asp:MaskedEditValidator ID="MaskedEditValidator1" runat="server"
ControlToValidate="DateRequested"
ControlExtender="DateRequested_MaskedEditExtender"
Display="Dynamic"
IsValidEmpty="false"
Tooltipmessage="Enter a valid date:"
EmptyValueMessage="Date is required."
InvalidValueMessage= "The date is invalid." /></li>
Here is all my vb code. I kept in code I've tried. FYI - I always got the message that Date is blank. All other data is being added to my SQL database.
aspx.vb
Imports System
Imports System.Data
Imports System.Data.OleDb
Imports System.Data.SqlClient
Partial Class TimeOffAdd
Inherits System.Web.UI.Page
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim stempname As String = Request.Form("EmployeeName")
'Dim stdate As String = Request.Form("DateRequested")
Dim stdtreq As Date = Request.Form(CDate("DateRequested"))
Dim streason As String
streason = DropDownList1.SelectedValue
'If (stdate <> "") Then
' stdtreq = DateAndTime.DateValue(stdate)
'Else
'MsgBox("Date is blank")
'End If
Dim objconnection As OleDbConnection = Nothing
Dim objcmd As OleDbCommand = Nothing
Dim strconnection As String, strSQL As String
'connection string
strconnection = "provider=SQLOLEDB;Data Source=janetdev;Initial Catalog=TimeSQL;Persist Security Info=True;User ID=sa;Password=password"
objconnection = New OleDbConnection(strconnection)
objconnection.ConnectionString = strconnection
objconnection.Open()
strSQL = "Insert into tblWorkHours(EmployeeName, DateRequested, WorkCode) values(?,?,?)"
objcmd = New OleDbCommand(strSQL, objconnection)
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@EmployeeName", stempname))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@DateRequested", stdtreq))
objcmd.Parameters.Add(New System.Data.OleDb.OleDbParameter("@WorkCode", streason))
objcmd.ExecuteNonQuery()
'close connection
objconnection.Close()
Response.Write("")
End Sub
I appreciate any help you can give me. Thanks from a newbie!
(FYI - the autocomplete is not working either - but that's for a later post.)
Thanks for your quick response. I tried to change the date format to YYYY/MM/DD. The date is showing up blank either way. I added code to give me a msgbox showing the DateRequested and EmployeeName values. The EmployeeName comes up (so I know it works),
but the DateRequested is still blank.
Maybe I have something wrong with the MaskEditExtender?
'Attempt to get the appropriate Date through the DateTime.Parse() method'
Dim stdtreq As DateTime = DateTime.Parse(Request.Form("DateRequested"))
'Attempt to use the DateTime.ParseExact() method to specify a format (in this case MM/dd/yyyy)'
Dim stdtreq As DateTime = DateTime.ParseExact(Request.Form("DateRequested"),"MM/dd/yyyy", Nothing)
or if you wanted to try to parse specifically using your YYYY/MM/DD format, you would use :
Dim stdtreq As DateTime = DateTime.ParseExact(Request.Form("DateRequested"),"yyyy/MM/dd", Nothing)
Have you tried placing a breakpoint within your existing code to see what the DateRequested value that stored within your Request.Form collection is?
I did a breakpoint to see what the DateRequested value is and it's nothing - "". It's not getting anything from that field. The other fields show data.
I'm thinking that, with the MaskEditExtender, the data is not stored in DateRequested, but something else. I did try to see if it was in DateRequested_MaskedEditExtender, but it's not. Also, one site said to put the <asp:TextBox ID="DateRequested"
runat="server"></asp:TextBox> below the MaskEditExtender - didn't work. I'm typing the date into my webform, and it's disappearing?? Thanks for your help!
That works! And if I change it to MM/dd/yyyy, it works! I had it mm/dd/yyyy, not MM/dd/yyyy. Once I capitalized the M's, it worked! Thanks - I am so happy!
Being a newbie, now I have to find out why the m's needed to be capital M's. hehe
"MM" refers to the month and "mm" refers to the minutes when dealing with formatting strings. You can read more on using custom formatting strings with DateTime objects here.
Member
18 Points
73 Posts
Conversion from string to type 'Date' is not valid.
Nov 07, 2013 10:51 AM|tjkalb|LINK
I am new to Visual Studio 2010, so I'm not sure if I'm wording this correctly. I am creating a web database to enter / update data in my SQL 2008 R2 database. I am using the MaskedEditExtender and Validator for my date field. I can enter the date in my web page - no problems, and the MaskEditExtender and Validator are doing what they're supposed to do. I am having a problem when I try to get this date's value to update my SQL database. All other fields add or update perfectly. The field is named DateRequested.
When I look at the data, it's empty even though I added it. I am getting the following error: Conversion from string to type 'Date' is not valid
Can someone help me? I can't figure this out.
Here is my code for this MaskedEditExtender and Validator date field:
aspx
Here is all my vb code. I kept in code I've tried. FYI - I always got the message that Date is blank. All other data is being added to my SQL database.
aspx.vb
I appreciate any help you can give me. Thanks from a newbie!
(FYI - the autocomplete is not working either - but that's for a later post.)
MaskedEditExtender
Member
113 Points
81 Posts
Re: Conversion from string to type 'Date' is not valid.
Nov 07, 2013 11:10 AM|krishnandu.sarkar|LINK
Though I never worked with MaskEditExtender, but this error means Date format is wrong. The string you are trying to convert to Date via
is not able to identify the string, and that's why it's throwing the error.
Remember that, conversion of Date doesn't recognize all format of dates. Better use YYYY/MM/DD else I always stick to DD-MMM-YYYY.
MaskedEditExtender
Member
18 Points
73 Posts
Re: Conversion from string to type 'Date' is not valid.
Nov 07, 2013 12:14 PM|tjkalb|LINK
Thanks for your quick response. I tried to change the date format to YYYY/MM/DD. The date is showing up blank either way. I added code to give me a msgbox showing the DateRequested and EmployeeName values. The EmployeeName comes up (so I know it works), but the DateRequested is still blank.
Maybe I have something wrong with the MaskEditExtender?
MaskedEditExtender
All-Star
114593 Points
18503 Posts
MVP
Re: Conversion from string to type 'Date' is not valid.
Nov 07, 2013 12:30 PM|Rion Williams|LINK
Have you considered using the DateTime.Parse() or DateTime.ParseExact() methods to read in your Date value from your Request.Form collection as seen below :
or if you wanted to try to parse specifically using your YYYY/MM/DD format, you would use :
Have you tried placing a breakpoint within your existing code to see what the DateRequested value that stored within your Request.Form collection is?
MaskedEditExtender
Member
18 Points
73 Posts
Re: Conversion from string to type 'Date' is not valid.
Nov 07, 2013 12:48 PM|tjkalb|LINK
I did a breakpoint to see what the DateRequested value is and it's nothing - "". It's not getting anything from that field. The other fields show data.
I'm thinking that, with the MaskEditExtender, the data is not stored in DateRequested, but something else. I did try to see if it was in DateRequested_MaskedEditExtender, but it's not. Also, one site said to put the <asp:TextBox ID="DateRequested" runat="server"></asp:TextBox> below the MaskEditExtender - didn't work. I'm typing the date into my webform, and it's disappearing?? Thanks for your help!
MaskedEditExtender
All-Star
114593 Points
18503 Posts
MVP
Re: Conversion from string to type 'Date' is not valid.
Nov 07, 2013 01:05 PM|Rion Williams|LINK
Have you tried to parse the value directly from your Textbox.Text property as seen below to see if that works:
MaskedEditExtender
Member
18 Points
73 Posts
Re: Conversion from string to type 'Date' is not valid.
Nov 07, 2013 02:42 PM|tjkalb|LINK
That works! And if I change it to MM/dd/yyyy, it works! I had it mm/dd/yyyy, not MM/dd/yyyy. Once I capitalized the M's, it worked! Thanks - I am so happy!
Being a newbie, now I have to find out why the m's needed to be capital M's. hehe
MaskedEditExtender
All-Star
114593 Points
18503 Posts
MVP
Re: Conversion from string to type 'Date' is not valid.
Nov 07, 2013 03:36 PM|Rion Williams|LINK
"MM" refers to the month and "mm" refers to the minutes when dealing with formatting strings. You can read more on using custom formatting strings with DateTime objects here.
MaskedEditExtender
Member
18 Points
73 Posts
Re: Conversion from string to type 'Date' is not valid.
Nov 07, 2013 03:51 PM|tjkalb|LINK
Thanks! Thanks! Thanks!
MaskedEditExtender