Application->SP problem

V

Viktor Popov

Hi,

I use ASP.NET/C#/MS SQL and I'm trying to do the following but I find it
very hard!
I have Stored Procedure which saves data from the Application into DataBase.
One of its parameters is @Rooms INT. When A user insert in the WebForm a
value everything is allright because I do this:

cmd.Parameters.Add(new SqlParameter("@ROOMS", SqlDbType.Int));
cmd.Parameters["@ROOMS"].Value = Int32.Parse(RoomsTB.Text.Trim());
where cmd is SqlCommand and RoomsTB is the TextBox from where I take the
value.

When the user doesn't insert value in the TextBox there is an error because
the Stored Procedure doesn't receive value for the Parameter @ROOMS. How
could I send to the SP NULL for this parameter when there isn't value in the
TextBox?

Thank you in advance!

Viktor
 
A

Ann Marinas

Hi, Viktor!

On your stored procedure, try assigning the @Rooms parameter with a 0 value.

For example,
create procedure yourProcedure
(
@rooms = 0
)
....

If in case you execute your procedure, you may or may not provide the value
for the @rooms parameter

- OR -

Try to test the value of RoomsTB.Text.Trim() before you pass this value to
your SqlCommand parameter value.

For example
if (RoomsTB.Text.Trim() != null)
cmd.Parameters["@Rooms"].Value = Int32.Parse(RoomsTB.Text.Trim());
else
cmd.Parameters["@Rooms"].Value = 0;

Hope this helps!

--Ann :)
 
V

Viktor Popov

Thanks for the reply, but I'd like to save in DataBase NULL instead of 0.

Viktor
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top