0

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
Chris Dunaway
  • 10,974
  • 4
  • 36
  • 48
xay
  • 47
  • 7
  • Use DateTime.ParseExact. Please refer this http://stackoverflow.com/questions/2193012/string-was-not-recognized-as-a-valid-datetime-format-dd-mm-yyyy/2193037#2193037 – Sain Pradeep Sep 17 '15 at 05:43
  • You have other example for vb.net ? that link is c# – xay Sep 17 '15 at 06:47

3 Answers3

1

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>
Amnesh Goel
  • 2,617
  • 3
  • 28
  • 47
  • Its give me this error Error 10 'null' is not declared. 'Null' constant is no longer supported; use 'System.DBNull' instead. C:\Users\ITTS\Documents\membership tuto\14\ASPNET_Security_Tutorial_14_VB\test.aspx.vb 51 133 C:\...\ASPNET_Security_Tutorial_14_VB\ – xay Sep 17 '15 at 06:46
  • @xay This is because you dont have any value in this text box and you are trying to convert a empty value.. and this error is expected .. So you may need `TryParseExact` instead of `ParseExact` – Amnesh Goel Sep 17 '15 at 07:01
  • 1
    @xay probably this is your friend.. http://stackoverflow.com/questions/32622433/string-was-not-recognized-as-a-valid-datetime-in-vb-net/32622898#32622898 – Amnesh Goel Sep 17 '15 at 07:02
  • can teach me how to convert that code suitable for my calendar extender ? i really new in this language – xay Sep 17 '15 at 07:10
  • but then i still got error like the first comment . null in not declared – xay Sep 17 '15 at 07:28
  • @xay it seems you are copying everything what I wrote.. hold on a sec .. let m write you whole code – Amnesh Goel Sep 17 '15 at 07:30
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/89881/discussion-between-amnesh-goel-and-xay). – Amnesh Goel Sep 17 '15 at 07:30
0

As you mentioned that your date format is (mm/dd/yy) so in your case you can simply call

Convert.ToDateTime(txtDate.Text)
Govind Kalyankar
  • 574
  • 1
  • 4
  • 17
0

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;
}
A_Sk
  • 4,532
  • 3
  • 27
  • 51