Øyvind,
Just a quick correction to the length test on the price text box.
I forgot to Trim off spaces in the check but did trim them when adding the
value to the sql parameter:
If txtPrice.Text.Trim.Length > 0 Then
--
Sincerely,
S. Justin Gengo, MCP
Web Developer / Programmer
www.aboutfortunate.com
"Out of chaos comes order."
Nietzsche
"S. Justin Gengo [MCP]" <justin@[no_spam_please]aboutfortunate.com> wrote in
message news:uHe9TXaKGHA.3352@TK2MSFTNGP12.phx.gbl...[color=blue]
> Øyvind,
>
> When using a stored procedure it's as simple as not setting the value. Or
> alternatively you may specifically set the value to NULL:
>
> By the way there is a shorter way to set your parameters instead of:
>
> cmd.Parameters.Add(New SqlParameter("@date",
> Data.SqlDbType.SmallDateTime))
> cmd.Parameters("@date").Value = Me.calDate.SelectedDate
>
> Use:
>
> '---When no date has been selected in the calendar then it returns:
> "1/1/0001 12:00:00 AM"
> If Not Me.calDate.SelectedDate > New Date(1, 1, 1) Then
> cmd.Parameters.Add("@date", Data.SqlDbType.SmallDateTime).Value =
> Me.calDate.SelectedDate
> End If
>
> '---Checking for length of string is more efficient than checking against
> "".
> If Me.txtPrice.Text.Length > 0 Then
> cmd.Parameters.Add("@price", Data.SqlDbType.Int).Value =
> Me.txtPrice.Text.Trim
> End If
>
> '---The alternative specifying the null (but this is not necessary)
> If Me.txtPrice.Text.Length > 0 Then
> cmd.Parameters.Add("@price", Data.SqlDbType.Int).Value =
> Me.txtPrice.Text.Trim
> Else
> cmd.Parameters.Add("@price", Data.SqlDbType.Int).Value = DBNull.Value
> End If
>
>
> --
> Sincerely,
>
> S. Justin Gengo, MCP
> Web Developer / Programmer
>
>
www.aboutfortunate.com
>
> "Out of chaos comes order."
> Nietzsche
> "Øyvind Isaksen" <oyvind@webressurs.no> wrote in message
> news:%23gw%23t3YKGHA.532@TK2MSFTNGP15.phx.gbl...[color=green]
>>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!!!!!!
>>[/color]
>
>[/color]