I want to insert the Date into SQL that is entered in textbox, When i enter 18/07/2011 it is successfully converted to DateTime object ( WORKING CODE - BELOW) , but when I enter 18/7/2011 it is not converted to DateTime object. What should I do?? if the user enters the date in the format 18/7/2011??
Code:Imports System.Globalization Imports System.Data.SqlClient Imports System.Data Partial Class _Default Inherits System.Web.UI.Page Dim con As New SqlConnection("Data source = SONIA-PC;Initial Catalog=master;Integrated Security=true") Dim cmd As New SqlCommand Dim ds As DataSet Dim da As SqlDataAdapter Dim query As String Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click Try query = "Insert into Practise2 values(@DOB)" cmd = New SqlCommand(query, con) Dim dt As DateTime If DateTime.TryParseExact(txtDateofBirth.Text, "dd/MM/yyyy", CultureInfo.InstalledUICulture, DateTimeStyles.None, dt) Then MsgBox(dt.ToString) MsgBox(dt.ToString(" yyyy-MM-dd")) cmd.Parameters.AddWithValue("@DOB", dt.ToString((" yyyy-MM-dd"))) End If If con.State = ConnectionState.Closed Then con.Open() End If cmd.ExecuteNonQuery() Catch ex As Exception MsgBox(ex.Message) Finally If con.State = ConnectionState.Open Then con.Close() End If End Try End Sub End Class




Reply With Quote