I' m trying to insert a row in a database residing on SQL Server. I'm using VB .NET and the ADO .NET framework. The structure of the table I want affect is shown below:
CREATE TABLE [dbo].[Users](
[UserName] [nvarchar](80) NOT NULL,
[Description] [nvarchar](600) NULL,
[IDCategory] [int] NULL,
[RegistrationDate] [datetime] NOT NULL,
[CategoryFlag] [bit] NULL,
[TypeFlag] [bit] NULL,
...
I have implemented this, with the following code:
<WebMethod()> _
Sub insertElement()
Dim conn As SqlConnection
Dim sqlcmd As SqlCommand
Dim res As Integer
conn = New SqlConnection(Utilities.ConnectionString)
sqlcmd = New SqlClient.SqlCommand()
sqlcmd.Connection = conn
sqlcmd.CommandType = CommandType.Text
sqlcmd.CommandText = "insert into Users (UserName, RegistrationDate) values (@user, @date)"
Dim param As SqlParameter
param = New SqlParameter("@user", "myself")
sqlcmd.Parameters.Add(param)
param = New SqlParameter("@date", '2012/10/11 20:30:15')
sqlcmd.Parameters.Add(param)
Try
conn.Open()
res = sqlcmd.ExecuteNonQuery()
Catch ex As Exception
Finally
sqlcmd.Dispose()
conn.Close()
End Try
Utilities.ConnectionString is just a class created by me for retuning the ConnectionString associated to my database.
I have tried to do something like this, without affecting any datatime filed and it worked. But with the above code there is probably a mistake in datatime management, because it does not work. Someone can help me?
catch
block and please post the error message. – sloth Aug 29 '12 at 14:00