简体   繁体   中英

“String was not recognized as a valid DateTime.”

Sorry for keep posting about the same question . Im still newbie in this language . So this is my problem . I keep got this error whenever i change the code many times . so this is my code

<tr>
<td style="width: 160px">
<asp:Label ID="Label2" runat="server">tarikh mula (mm/dd/yy) :</asp:Label>
</td>
<td>
<asp:TextBox ID="txtDate" runat="server" ReadOnly="true"></asp:TextBox>
<cc1:CalendarExtender ID="CalendarExtender1" TargetControlID="txtDate" runat="server">
</cc1:CalendarExtender> 
</td>
</tr>

And this is my code behind

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As      System.EventArgs) Handles btnSubmit.Click
    Dim thisConnection As New SqlConnection(ConfigurationManager.ConnectionStrings("SecurityTutorialsConnectionString").ConnectionString)

    'Create Command object
    Dim nonqueryCommand As SqlCommand = thisConnection.CreateCommand()

    Try
        ' Open Connection
        thisConnection.Open()

        ' Create INSERT statement with named parameters
        nonqueryCommand.CommandText = "INSERT  INTO QuotationBG (TajukSebutHarga, NoRujukan, TarikhMula) VALUES (@TajukSebutHarga, @NoRujukan, @TarikhMula)"

        ' Add Parameters to Command Parameters collection
        nonqueryCommand.Parameters.Add("@TajukSebutHarga", SqlDbType.VarChar, 255)
        nonqueryCommand.Parameters.Add("@NoRujukan", SqlDbType.VarChar, 255)
        nonqueryCommand.Parameters.Add("@TarikhMula", SqlDbType.Date).Value = DateTime.Parse(txtDate.Text)

        nonqueryCommand.Parameters("@TajukSebutHarga").Value = txtTS.Text
        nonqueryCommand.Parameters("@NoRujukan").Value = txtNo1.Text + txtNo2.Text + txtNo3.Text + txtNo4.Text
        nonqueryCommand.ExecuteNonQuery()
    Finally
        ' Close Connection
        thisConnection.Close()
    End Try 
End Sub

Change your server side code as follows..

Protected Sub btnSubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSubmit.Click
    Dim thisConnection As New  SqlConnection(ConfigurationManager.ConnectionStrings("SecurityTutorialsConnectionString").ConnectionString)

    'Create Command object
    Dim nonqueryCommand As SqlCommand = thisConnection.CreateCommand()
    Dim result as Date

    Try
        ' Open Connection
        thisConnection.Open()

        nonqueryCommand.CommandText = _
                    "INSERT  INTO QuotationBG (TajukSebutHarga, NoRujukan, TarikhMula) VALUES (@TajukSebutHarga, @NoRujukan, @TarikhMula)"

        ' Add Parameters to Command Parameters collection
        nonqueryCommand.Parameters.Add("@TajukSebutHarga", SqlDbType.VarChar, 255)
        nonqueryCommand.Parameters.Add("@NoRujukan", SqlDbType.VarChar, 255)

        nonqueryCommand.Parameters("@TajukSebutHarga").Value = txtTS.Text
        nonqueryCommand.Parameters("@NoRujukan").Value = txtNo1.Text + txtNo2.Text + txtNo3.Text + txtNo4.Text


        If DateTime.TryParseExact(Me.txtDate.Text, "M'/'d'/'yyyy", CultureInfo.InvariantCulture, DateTimeStyles.None, result) Then

            nonqueryCommand.Parameters.Add("@TarikhMula", SqlDbType.Date).Value =  result

        else

            nonqueryCommand.Parameters.Add("@TarikhMula", SqlDbType.VarChar).Value =  string.empty

        End If
        nonqueryCommand.ExecuteNonQuery()
    Finally
        ' Close Connection
        thisConnection.Close()
    End Try
End Sub

And change your aspx to..

<asp:TextBox ID="txtDate" runat="server"></asp:TextBox>

正如您提到的,日期格式为(mm / dd / yy),因此您可以简单地致电

Convert.ToDateTime(txtDate.Text)

try,

DateTime? dt=null;

DateTime.TryParse(txtDate.Text,out dt)

if(dt!=null)
{
nonqueryCommand.Parameters.Add("@TarikhMula", SqlDbType.Date).Value =dt;
}
else
{
nonqueryCommand.Parameters.Add("@TarikhMula", SqlDbType.DBNull).Value = null;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM