Hi all,

I am trying to insert date but not able to, im using SQL server.

This is my code:
Dim str As String
Dim cmd As SqlCommand
Dim con As SqlConnection
Dim sql As String

Function AccessDate(ByVal dateandtime)
Dim myday
Dim mymonth
Dim myyear

myday = Day(dateandtime)
If Len(myday) = 1 Then myday = "0" & myday

mymonth = Month(dateandtime)
If Len(mymonth) = 1 Then mymonth = "0" & mymonth

myyear = Year(dateandtime)
AccessDate = myyear & "-" & mymonth & "-" & myday

End Function


Protected Sub btnadd_Click(ByVal sender As Object, ByVal e As System.EventArgs)
sql = "insert into cashtable(tdate) values(# " & AccessDate(Now()) & " # )"

Try
con = New SqlConnection(str)
cmd = New SqlCommand(sql, con)
con.Open()
cmd.ExecuteNonQuery()
lblinfo.Visible = True
lblinfo.Text = "Record Inserted"

Catch ex As Exception
lblinfo.Visible = True
lblinfo.Text = ex.Message
Finally
con.Close()
End Try

End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

str = ConfigurationManager.ConnectionStrings("ShanthiConnectionString").ConnectionString
lblinfo.Visible = False
GridView1.Visible = False

End Sub

Thanks in advance

Hi all,

I am trying to insert date but not able to, im using SQL server.

This is my code:
Dim str As String
Dim cmd As SqlCommand
Dim con As SqlConnection
Dim sql As String

Function AccessDate(ByVal dateandtime)
Dim myday
Dim mymonth
Dim myyear

myday = Day(dateandtime)
If Len(myday) = 1 Then myday = "0" & myday

mymonth = Month(dateandtime)
If Len(mymonth) = 1 Then mymonth = "0" & mymonth

myyear = Year(dateandtime)
AccessDate = myyear & "-" & mymonth & "-" & myday

End Function


Protected Sub btnadd_Click(ByVal sender As Object, ByVal e As System.EventArgs)
sql = "insert into cashtable(tdate) values(# " & AccessDate(Now()) & " # )"

Try
con = New SqlConnection(str)
cmd = New SqlCommand(sql, con)
con.Open()
cmd.ExecuteNonQuery()
lblinfo.Visible = True
lblinfo.Text = "Record Inserted"

Catch ex As Exception
lblinfo.Visible = True
lblinfo.Text = ex.Message
Finally
con.Close()
End Try

End Sub
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)

str = ConfigurationManager.ConnectionStrings("ShanthiConnectionString").ConnectionString
lblinfo.Visible = False
GridView1.Visible = False

End Sub

Thanks in advance

Try replacing the '#' with one inverted comma like so '
sql = "insert into cashtable(tdate) values(' " & AccessDate(Now()) & " ' )"
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.