store date value into database (vb.net)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • taufik
    New Member
    • Aug 2007
    • 15

    store date value into database (vb.net)

    Hi,

    I have problem with my simple program. Actually, I try to store date value in form of date format (mm/dd/yyyy). I got message that inform me "number of query values and destination fields are not the same". I provide example coding below to help you all understand. Please help me, it urgent....

    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
    Dim oConn As New OleDbConnection ("Provider=Micr osoft.Jet.OleDb .4.0; Data Source=" & "C:\Documen ts and Settings\Crazy_ Frog\My Documents\Visua l Studio Projects\SportP lanet1\Data\spD ata.mdb")
    Dim oApt As New OleDbDataAdapte r("SELECT [tbl_name] FROM [database_name]", oConn)
    Dim sql As String = String.Empty
    Try
    If _id = 0 Then
    sql = "INSERT INTO [database_name] Values('" & TextBox4.Text & "')"
    End If
    oConn.Open()
    Dim oComm As New OleDbCommand(sq l, oConn)
    oComm.ExecuteNo nQuery()
    oConn.Close()
    MsgBox("Data Save.")
    Catch ex As Exception
    MsgBox(ex.Messa ge)
    End Try

    End Sub
  • SharpDeveloper
    New Member
    • Feb 2008
    • 12

    #2
    Hello
    The SQL Insert Statement Syntax is like this
    Statement I
    INSERT INTO TABLENAME(COLNA ME) VALUES(COL_VALU E)
    Statement II
    INSERT INTO TABLENAME VALUES(COL_VALU E)
    The above query works without column name if you are specifying all the column values in the VALUES.

    Seems in this case
    sql = "INSERT INTO [database_name] Values('" & TextBox4.Text & "')"

    the table database_name has more than one column.
    So use
    sql = "INSERT INTO database_name(C olumn_name) Values('" & TextBox4.Text & "')"

    Hope this helps you.
    Happy Programming!
    Originally posted by taufik
    Hi,

    I have problem with my simple program. Actually, I try to store date value in form of date format (mm/dd/yyyy). I got message that inform me "number of query values and destination fields are not the same". I provide example coding below to help you all understand. Please help me, it urgent....

    Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
    Dim oConn As New OleDbConnection ("Provider=Micr osoft.Jet.OleDb .4.0; Data Source=" & "C:\Documen ts and Settings\Crazy_ Frog\My Documents\Visua l Studio Projects\SportP lanet1\Data\spD ata.mdb")
    Dim oApt As New OleDbDataAdapte r("SELECT [tbl_name] FROM [database_name]", oConn)
    Dim sql As String = String.Empty
    Try
    If _id = 0 Then
    sql = "INSERT INTO [database_name] Values('" & TextBox4.Text & "')"
    End If
    oConn.Open()
    Dim oComm As New OleDbCommand(sq l, oConn)
    oComm.ExecuteNo nQuery()
    oConn.Close()
    MsgBox("Data Save.")
    Catch ex As Exception
    MsgBox(ex.Messa ge)
    End Try

    End Sub

    Comment

    • Plater
      Recognized Expert Expert
      • Apr 2007
      • 7872

      #3
      There are a number of good examples on how to use INSERT.
      This one is pretty good at explaining how to insert data into different columns:
      W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.

      Comment

      Working...