很抱歉继续发布有关同一问题的信息。我仍然是这种语言的新手。所以这是我的问题。每当我多次更改代码时,我都会遇到此错误。所以这是我的代码
<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>
这是我背后的代码
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
答案 0 :(得分:1)
按如下方式更改服务器端代码..
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
将您的aspx
更改为..
<asp:TextBox ID="txtDate" runat="server"></asp:TextBox>
答案 1 :(得分:0)
正如您所提到的,您的日期格式是(mm / dd / yy)所以在您的情况下,您只需致电
Convert.ToDateTime(txtDate.Text)
答案 2 :(得分:0)
试,
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;
}