Reputation: 47
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
Upvotes: 0
Views: 311
Reputation: 2655
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>
Upvotes: 1
Reputation: 4630
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;
}
Upvotes: 0
Reputation: 624
As you mentioned that your date format is (mm/dd/yy) so in your case you can simply call
Convert.ToDateTime(txtDate.Text)
Upvotes: 0