0

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.

1
  • What is the format of your string ? Parse your string to DateTime object and then attach it as parameter to your Insert query Commented Oct 28, 2013 at 17:52

2 Answers 2

1

If you know the exact format (dd/MM/yyyy in this example) you want to support, then use TryParseExact, like this:

Dim dt As DateTime

DateTime.TryParseExact(textBox.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, dt)

Then you can check to see if dt is null or not after the TryParseExact() call, like this:

If Not dt Is Nothing
    ' Text box text is a valid date, pass the date to the database
End If

You can use this logic inside of the form view's item inserting event, like this:

Sub FormView1_ItemInserting(ByVal sender As Object, ByVal e As FormViewInsertEventArgs)
    Dim dt As DateTime

    DateTime.TryParseExact(textBox.Text, "dd/MM/yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, dt)

    If dt Is Nothing
        ' Use the Cancel property to cancel the insert operation.
        e.Cancel = True
    End If
End Sub
Sign up to request clarification or add additional context in comments.

6 Comments

I understand. Looks good. What event then would I intercept to parse and pass to the database?
@Ryan - I would assume this logic is tied to a button on the UI, right? If so, then put this logic into the button click event handler, see updated answer.
Basically, when a user opens the web page and the Formview loads for an insert, I set the current date in an asp textbox. When the user submits the form is when I get the cast exception. Would I try and set the sqldatasource insert parameter value? I have some other code that runs in the FormView ItemInserting event. Would that be the place for it?
@Ryan - posted update to answer, just realized you are using VB.NET so I converted my entire answer to use VB.NET instead of C#.
Still getting this: The conversion of a nvarchar data type to a datetime data type resulted in an out-of-range value. The statement has been terminated.
|
1
DateTime dateTime;

DateTime.TryParseExact(textBox1.Text, 
                       "dd/MM/yyyy", //you can specify any desired format here
                       CultureInfo.InvariantCulture, 
                       DateTimeStyles.None, 
                       out dateTime);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.