Results 1 to 4 of 4

Thread: How to save date and time in database

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    67

    How to save date and time in database

    Haii...

    Im trying to save date and time in database. Right now im trying declared time and date, but have problem in assigning it to a variable, where afterwords im try to use it to insert in the database. This is the code that i use:

    Code:
    Dim dt As DateTime = DateTime.Now
    lblTimeNow.Text = dt.ToLocalTime
    
    Dim masa As String
    masa = dt.ToLocalTime.ToString()
    
    Dim DataSource As New SqlConnection("Data Source=ANNAS\SQLEXPRESS; Initial Catalog=DBRMS;Integrated Security=True")
    
    Dim command As New SqlCommand("INSERT INTO Customers(Time) VALUES (masa)", DataSource)
            
    DataSource.Open()
    command.ExecuteNonQuery()
    
    DataSource.Close()
    After been compiled i get this error message

    The name "masa" is not permitted in this context. Valid expressions are constants, constant expressions, and (in some contexts) variables. Column names are not permitted.

    Can,anyone help me with this, thank u

  2. #2
    A SQL Server fool GaryMazzone's Avatar
    Join Date
    Aug 2005
    Location
    Dover,NH
    Posts
    7,493

    Re: How to save date and time in database

    Like this:

    Code:
    Dim command As New SqlCommand("INSERT INTO Customers(Time) VALUES ('" & masa & "')", DataSource)
    Since masa is a var it needs to be outside the string (concat). Since you are storeing a dateTime in SQL Server the var needs to be enclosed in single qoutes.

    edit

    I would also place Time in Square Brackets ([Time])
    Sometimes the Programmer
    Sometimes the DBA

    Mazz1

  3. #3
    Code Monkey wild_bill's Avatar
    Join Date
    Mar 2005
    Location
    Montana
    Posts
    2,993

    Re: How to save date and time in database

    vb Code:
    1. Dim dt As DateTime = DateTime.Now
    2.         lblTimeNow.Text = dt.ToLocalTime
    3.  
    4.         Dim DataSource As New SqlConnection("Data Source=ANNAS\SQLEXPRESS; Initial Catalog=DBRMS;Integrated Security=True")
    5.  
    6.         Dim command As New SqlCommand("INSERT INTO Customers(Time) VALUES (@masa)", DataSource)
    7.         '2003 syntax
    8.         'command.Parameters.Add("@masa", dt.ToLocalTime)
    9.  
    10.         '2005 syntax
    11.         command.Parameters.AddWithValue("@masa", dt.ToLocalTime)
    12.  
    13.         DataSource.Open()
    14.         command.ExecuteNonQuery()
    15.  
    16.         DataSource.Close()

  4. #4

    Thread Starter
    Lively Member
    Join Date
    Aug 2007
    Posts
    67

    Thumbs up How to save date and time in database

    i already try it, and it works perfetly.
    tHanks a lot to GaryMazzone and wild_bill

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width