-
how to insert date to OleDb DataBase(VB.Net windows Application)
i am trying to insert a date to OleDb data base. its Field Type is DateTime.
what is wrong with my insert query? by avoiding the date field using this query
ssql = "INSERT INTO mytable (Address,Name) VALUES (@address,@name)"
i can insert Address and name to database.
something is wrong in the syntax for Date Field. pls help me.This is my code.it shows Syntax error in INSERT INTO Statement
Dim insertOk As Boolean = True
Dim sqlcon As OleDbConnection = New OleDbConnection("Data Source=C:\Debug\Data.mdb; Provider=Microsoft.Jet.OLEDB.4.0; User ID=Admin")
Try
sqlcon.Open()
ssql = "INSERT INTO mytable (Address,Name,Date) VALUES (@address,@name,@dt)"
sqlcmd = New OleDbCommand(ssql, sqlcon)
sqlcmd.Parameters.Add("@address", TextBox2.Text)
sqlcmd.Parameters.Add("@name", TextBox1.Text)
sqlcmd.Parameters.Add("@dt", Convert.ToDateTime(DateTimePicker1.Value.ToString) )
sqlcmd.ExecuteNonQuery()
Catch ex As OleDbException
MsgBox(ex.Message.ToString)
insertOk = False
Finally
sqlcon.Close()
End Try
If insertOk Then
MsgBox("Details Submitted Successfully!")
Else : MsgBox("There was an error saving your data!")
End If
-
sandhya.o.k,
Since you are getting a syntax error in the Insert statement, perhaps Date is a reserved keyword.
In that case you will need to enclose it in square brackets:
ssql = "INSERT INTO mytable (Address,Name,[Date]) VALUES (@address,@name,@dt)"
Kerry Moorman
-
 Originally Posted by sandhya.o.k
sqlcmd.Parameters.Add("@dt", Convert.ToDateTime(DateTimePicker1.Value.ToString()) )
I'm pretty sure that .ToString needs () at the end.
-
As Kerry mentioned, DATE is an ODBC reserved word. You should rename that column.
Paul
~~~~
Microsoft MVP (Visual Basic)
-
Thank u for all..... my problem got solved. syntax error was due to Date Field. [ ] solved my problem.
working query is
ssql = "INSERT INTO mytable (Address,Name,[Date]) VALUES (@address,@name,@dt)"
okey.... thank u sooo much.
Similar Threads
-
By sandhya.o.k in forum .NET
Replies: 1
Last Post: 12-06-2006, 12:32 PM
-
By Urbaud in forum VB Classic
Replies: 4
Last Post: 07-30-2006, 10:50 PM
-
By lew26 in forum Database
Replies: 1
Last Post: 01-26-2006, 10:54 PM
-
By Go_Diamond in forum VB Classic
Replies: 12
Last Post: 03-30-2005, 01:59 AM
-
By Shailaza in forum ASP.NET
Replies: 0
Last Post: 06-15-2000, 07:08 AM
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
Forum Rules
|
Top DevX Stories
Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL
|
Bookmarks