I need to add the current date.time in database in format
dd/MM/yyyy HH:mm
This way without formatting works great
Public Sub AddIt(Name As String)
Try
Dim addString As String = "Insert into table (Name, DateAdd) values ('" & Name & "', '" & Date.Now & "')"
sqlcon.Open()
SqlCom = New SqlCommand(addString, sqlcon)
SqlCom.ExecuteNonQuery()
sqlcon.Close()
Catch ex As Exception
End Try
End Sub
Tried with parameters to format
Public Sub Town(Name As String)
Try
Dim time As DateTime = DateTime.Now
Dim format As String = "dd.MM.yyyy HH:mm"
Dim MyCommand As SqlCommand
MyCommand = New SqlCommand("Insert into test (Name, DateAdd) values (@name, @test)", sqlcon)
MyCommand.Parameters.AddWithValue("@name", form2.txtName.Text)
MyCommand.Parameters.AddWithValue("@test", time.ToString(format))
sqlcon.Open()
MyCommand.ExecuteNonQuery()
sqlcon.Close()
Catch ex As Exception
End Try
End Sub
It says connection was not closed. The connection state is open
new SQL Command