how to insert date to OleDb DataBase(VB.Net windows Application)


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 5 of 5

Thread: how to insert date to OleDb DataBase(VB.Net windows Application)

Hybrid View

  1. #1
    Join Date
    Nov 2006
    Location
    India, Kerala
    Posts
    19

    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

  2. #2
    Join Date
    Dec 2004
    Posts
    163
    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

  3. #3
    Join Date
    Jul 2006
    Posts
    14
    Quote 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.

  4. #4
    Join Date
    Dec 2003
    Posts
    2,750
    As Kerry mentioned, DATE is an ODBC reserved word. You should rename that column.
    Paul
    ~~~~
    Microsoft MVP (Visual Basic)

  5. #5
    Join Date
    Nov 2006
    Location
    India, Kerala
    Posts
    19
    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

  1. vb.net windows application help?
    By sandhya.o.k in forum .NET
    Replies: 1
    Last Post: 12-06-2006, 12:32 PM
  2. Replies: 4
    Last Post: 07-30-2006, 10:50 PM
  3. Replies: 1
    Last Post: 01-26-2006, 10:54 PM
  4. Sendkeys to any windows application
    By Go_Diamond in forum VB Classic
    Replies: 12
    Last Post: 03-30-2005, 01:59 AM
  5. ASP Vs Windows application
    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
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center