I am getting an cast exception when my formview attempts an insert of a record that has a string date that needs to go into a datetime column in SQL Server. The field is bound to the table column using the Bind method, and is a part of the InsertItemTemplate of the FormView. I am not sure how to convert the string to datetime during the insert. I thought maybe using the Insert Command to Convert(datetime, @PDate, 101)? That did not take care of the problem.
Please tell me what you think. I appreciate any help and suggestions.
*EDIT *
This is the code for setting the current date
Protected Sub FormView1_DataBound(sender As Object, e As System.EventArgs) Handles FormView1.DataBound
If FormView1.CurrentMode = FormViewMode.Insert Then
Dim tb As New TextBox
tb = FormView1.FindControl("textPDate")
tb.Text = FormatDateTime(DateTime.Now, DateFormat.ShortDate).ToString
End If
End Sub
This is the markup for the textbox
<asp:TextBox ID="textPDate" runat="server" Style="position:absolute; top: 140px; left:181px; width: 200px;"
Text='<%# Bind("PDate") %>'></asp:TextBox>
This is some other calc I do during for inserting
Protected Sub FormView1_ItemInserting(sender As Object, e As System.Web.UI.WebControls.FormViewInsertEventArgs) Handles FormView1.ItemInserting
Dim paper As New DropDownList
Dim cylinder As New DropDownList
Dim blockcode As New TextBox
paper = FormView1.FindControl("dropdownPaperItem")
cylinder = FormView1.FindControl("dropdownCylinderNumber")
blockcode = FormView1.FindControl("textBlockCode")
Dim c As New Common
blockcode.Text = c.CalcBlockCode(paper.Text, cylinder.Text)
End Sub
I thought maybe I could cast the string to datetime in the ItemInserting event but I am not sure.
DateTimeobject and then attach it as parameter to your Insert query