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.)
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