Ø
Øyvind Isaksen
I have a page with an optional integer-field, and one asp:calendar control.
I use a stored procedure to save the data in SQL Server.
When all fields contains data, the code works great! But if the user dont
fill in the optional "price-field" (integer value), or if the user dont
choose a date in the asp:calendar control, I get the message "Input string
was not in a correct format".
How do I save "Null" value if the price-field is blank, and how do I save
"Null" value if a date in the calendar is NOT choosen?
----------------------------------
Stored procedure:
---------------------------------
CREATE PROCEDURE spSaveProject
(
@title varchar(512),
@price int,
@date smalldatetime = null,
)
AS
insert into tblProject (title,price,date) values (@title,@price,@date)
GO
-------------------------
Code:
-------------------------
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSave.Click
Dim conn As New SqlConnection(variables.ConnString)
Dim cmd As New SqlCommand("spSaveProject", conn)
cmd.CommandType = CommandType.StoredProcedure
Dim parTitle As New SqlParameter("@title", Data.SqlDbType.VarChar,
512)
parTitle.Value = Me.txtTitle.Text.ToString
cmd.Parameters.Add(parTitle)
cmd.Parameters.Add(New SqlParameter("@price", Data.SqlDbType.Int))
cmd.Parameters("@price").Value = Me.txtPrice.Text
cmd.Parameters.Add(New SqlParameter("@date",
Data.SqlDbType.SmallDateTime))
cmd.Parameters("@date").Value = Me.calDate.SelectedDate
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
cmd.Dispose()
conn.Dispose()
End Sub
THANK YOU!!!!!!
I use a stored procedure to save the data in SQL Server.
When all fields contains data, the code works great! But if the user dont
fill in the optional "price-field" (integer value), or if the user dont
choose a date in the asp:calendar control, I get the message "Input string
was not in a correct format".
How do I save "Null" value if the price-field is blank, and how do I save
"Null" value if a date in the calendar is NOT choosen?
----------------------------------
Stored procedure:
---------------------------------
CREATE PROCEDURE spSaveProject
(
@title varchar(512),
@price int,
@date smalldatetime = null,
)
AS
insert into tblProject (title,price,date) values (@title,@price,@date)
GO
-------------------------
Code:
-------------------------
Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnSave.Click
Dim conn As New SqlConnection(variables.ConnString)
Dim cmd As New SqlCommand("spSaveProject", conn)
cmd.CommandType = CommandType.StoredProcedure
Dim parTitle As New SqlParameter("@title", Data.SqlDbType.VarChar,
512)
parTitle.Value = Me.txtTitle.Text.ToString
cmd.Parameters.Add(parTitle)
cmd.Parameters.Add(New SqlParameter("@price", Data.SqlDbType.Int))
cmd.Parameters("@price").Value = Me.txtPrice.Text
cmd.Parameters.Add(New SqlParameter("@date",
Data.SqlDbType.SmallDateTime))
cmd.Parameters("@date").Value = Me.calDate.SelectedDate
cmd.Connection.Open()
cmd.ExecuteNonQuery()
cmd.Connection.Close()
cmd.Dispose()
conn.Dispose()
End Sub
THANK YOU!!!!!!