Results 1 to 10 of 10

Thread: insert time in vb.net

  1. #1

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Posts
    88

    insert time in vb.net

    i m inserting time from listview into sql database 2005.....but it results inserting both date and time part as well..but i want to insert only the time part...my code is below...thanks...plzz help


    Code:
     Dim conn As New SqlConnection("server=localhost;database=attendance1;uid=sa;password=;")
            Dim i As Integer
    
            For i = 0 To ListView1.Items.Count - 1
                Dim cmd As New SqlCommand
    
    
                cmd.Connection = conn
                cmd.CommandText = "INSERT INTO shift (shift_name,on_duty_time,off_duty_time,beginning_in,ending_in,beginning_out,ending_out) VALUES (@shift_name,@on_duty_time,@off_duty_time,@beginning_in,@ending_in,@beginning_out,@ending_out) "
                cmd.CommandType = CommandType.Text
                cmd.Parameters.AddWithValue("@shift_name", ListView1.Items(i).SubItems(0).Text)
                cmd.Parameters.AddWithValue("@on_duty_time", Convert.ToDateTime(ListView1.Items(i).SubItems(1).Text))
                cmd.Parameters.AddWithValue("@off_duty_time", Convert.ToDateTime(ListView1.Items(i).SubItems(2).Text))
                cmd.Parameters.AddWithValue("@beginning_in", Convert.ToDateTime(ListView1.Items(i).SubItems(3).Text))
                cmd.Parameters.AddWithValue("@ending_in", Convert.ToDateTime(ListView1.Items(i).SubItems(4).Text))
                cmd.Parameters.AddWithValue("@beginning_out", Convert.ToDateTime(ListView1.Items(i).SubItems(5).Text))
    
                cmd.Parameters.AddWithValue("@ending_out", Convert.ToDateTime(ListView1.Items(i).SubItems(6).Text))
    
                conn.Open()
                cmd.ExecuteNonQuery()
                conn.Close()
            Next i
            ListView1.View = View.Details
            MsgBox("ok")

  2. #2
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: insert time in vb.net

    This one ?

    Code:
    cmd.Parameters.AddWithValue("@ending_out", Convert.ToDateTime(ListView1.Items(i).SubItems(6).Text).ToShortTimeString())
    Please mark you thread resolved using the Thread Tools as shown

  3. #3

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Posts
    88

    Re: insert time in vb.net

    i used that and in the date part 1900-01-01 was inserted..still i m getting date part...

  4. #4
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: insert time in vb.net

    SQL Server columns cannot contain the time alone. That's why the column data type is 'datetime': it contains a date and a time. You simply have to ignore the date part when you read the data.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  5. #5
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: insert time in vb.net

    What is Your BackEnd. ? If it is SQL Server You cannot insert only time in if the Column type id DateTime or SmallDateTime. The other thing you can do is the change the column definition to varchar or char datatypes..

    Edit: Little slow in typing
    Please mark you thread resolved using the Thread Tools as shown

  6. #6

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Posts
    88

    Re: insert time in vb.net

    i was thinking abt doing that but if i change datetime to varchar then would i be able to perform the time functions like adding time and all regarding that varchar text that i insert..

  7. #7
    Learning .Net danasegarane's Avatar
    Join Date
    Aug 2004
    Location
    VBForums
    Posts
    5,853

    Re: insert time in vb.net

    Then you have to neglect the Datepart while reading the values in your front end
    Please mark you thread resolved using the Thread Tools as shown

  8. #8

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Posts
    88

    Re: insert time in vb.net

    that's what i want to do.........if i insert both datetime then how can i extract only time part into textbox....

  9. #9
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    111,221

    Re: insert time in vb.net

    Quote Originally Posted by anirai View Post
    that's what i want to do.........if i insert both datetime then how can i extract only time part into textbox....
    You've already asked that question in another thread and I've already provided an answer. I've asked you to post your current code and you have yet to do so. Please don't start asking the same question in multiple threads.
    Why is my data not saved to my database? | MSDN Data Walkthroughs
    VBForums Database Development FAQ
    My CodeBank Submissions: VB | C#
    My Blog: Data Among Multiple Forms (3 parts)
    Beginner Tutorials: VB | C# | SQL

  10. #10

    Thread Starter
    Lively Member
    Join Date
    Jan 2009
    Posts
    88

    Re: insert time in vb.net

    oki....c ya in another thread

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