I don’t know how to convert the nvarchar value into an int, but that is the way it needs to be set up. I’ve found other posts about this but I don’t know how to use CAST in the INSERT statement I have. All the posts I could find were about converting to int in SELECT statements. I need this statement to change the field @Duration to an int in the database. @Duration is an amount of time. (01:00, 02:00…) They will all get rounded up to the next int value. So if a user enters 0:45, it should be converted to 1. Can someone help me figure out how to get this INSERT to work?
'SQL Insert: Product table
Dim sqlInsertProduct As String = "INSERT INTO Product (ProductName, Status,
CreateDate, ModifyDate, CreateUser,
ModifyUser, Price, LaunchDate, Duration,
IsPublic, Presenter, Host, Broker)
VALUES (@ProductName, '1',getdate(),
getdate(), @CreateUser, @ModifyUser,
'0', @LaunchDate, @Duration, '1',
@Presenter, @Host, @Broker);
SELECT SCOPE_IDENTITY();"
Using cmd As New SqlCommand(sqlInsertProduct, cn)
Dim ProductID As String
cmd.Parameters.Add(New SqlParameter("@ProductName",
txtNewWebinarName.Text))
cmd.Parameters.Add(New SqlParameter("@CreateUser",
System.Web.HttpContext.Current.User.Identity.Name))
cmd.Parameters.Add(New SqlParameter("@ModifyUser",
System.Web.HttpContext.Current.User.Identity.Name))
cmd.Parameters.Add(New SqlParameter("@LaunchDate", txtDateTime.Text))
cmd.Parameters.Add(New SqlParameter("@Duration", txtDuration.Text))
cmd.Parameters.Add(New SqlParameter("@Presenter", txtPresenter.Text))
cmd.Parameters.Add(New SqlParameter("@Host", txtHost.Text))
cmd.Parameters.Add(New SqlParameter("@Broker", txtBroker.Text))
cn.Open()
ProductID = cmd.ExecuteScalar()
Session("ProductID") = ProductID
Dim Product As String = Session("ProductID")
End Using
cn.Close()
Assuming that you want to convert a
TimeSpaninto an int, also assuming you want it’s TotalHours property:Edit: If this actually is a TimeSpan, your conversion would be wrong, because the String “0:45” represents a TimeSpan of 0,75 hours(45 minutes are 3/4 hours).
This should do the rounding correctly:
http://msdn.microsoft.com/en-us/library/se73z7b9.aspx