I have 2 textboxes, txtStartDate.Text and txtEndDate.Text, user choose date through calendar date picker.
Now I want to count the days between the two selected date and save the result in the database field totalDay(type integer), the following is my code:
But when I click button and try to save it into database, i receive this error:
The version of SQL Server in use does not support datatype ‘time’.
What should I do to overcome this problem?
Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Threading
Imports System.Globalization
Partial Class addevent
Inherits System.Web.UI.Page
Protected Sub ButtonAddEvent_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles ButtonAddEvent.Click
Dim connString As String = ConfigurationManager.ConnectionStrings("ConnectionString1").ConnectionString
'"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\eventdb.mdf;Integrated Security=True;User Instance=True"
Dim con As SqlConnection = New SqlConnection(connString)
Dim cmdQuery As String = "INSERT INTO eventinfo(venue,totalDay,eventTitle,startDate,endDate,description) VALUES (@venue,@totalDay,@eventTitle,@startDate,@endDate,@description)"
Dim cmd = New SqlCommand(cmdQuery)
cmd.Connection = con
cmd.CommandType = CommandType.Text
con.Open()
cmd.Parameters.AddWithValue("@eventTitle", txtEventTitle.Text)
cmd.Parameters.AddWithValue("@venue", txtEventLocation.Text)
cmd.Parameters.AddWithValue("@startDate", txtStartDate.Text)
cmd.Parameters.AddWithValue("@endDate", txtEndDate.Text)
cmd.Parameters.AddWithValue("@description", txtEventDescription.Text)
Dim fmt As String = "dd/MM/yyyy"
Dim dtStart As DateTime = DateTime.ParseExact(txtStartDate.Text, fmt, Nothing)
Dim dtEnd As DateTime = DateTime.ParseExact(txtEndDate.Text, fmt, Nothing)
Dim ts As TimeSpan = dtEnd - dtStart
cmd.Parameters.AddWithValue("@totalDay", ts.TotalDays)
cmd.ExecuteNonQuery()
cmd.Parameters.Clear()
con.Close()
Response.Redirect("~/addbooth.aspx")
End Sub
End Class
My table structure:

As your field is of type integer, and you only let users input days without a time component, the following could work:
TimeSpan.TotalDays gives you days and fractions of days as a double, TimeSpan.Days only the whole days.