1

I add parameters to Sqldatasource control dynamically from code behind.

sq1.UpdateParameters.Clear();
sq1.UpdateCommand = "sp_upn";
sq1.UpdateCommandType = SqlDataSourceCommandType.StoredProcedure;

is there a better way to add null parameter ?better than code below?(set value to null just when adding, not later)

sq1.UpdateParameters.Add("imgid","");
sq1.UpdateParameters["imgid"].DefaultValue = null;

UPDATED The parameter is not always NULL.It may have Int value based on condition.( if user uploaded an image)

I am using Sqldatasource Control and not a sqlcommand and I can not use:

cmd.Parameters.AddWithValue("imgid", DBNull.Value); 
2
  • Does specifying DbType help? Commented Aug 17, 2018 at 19:29
  • @tia Thank you .yes! sq1.UpdateParameters.Add("imgid", DbType .Int32,null); worked. please add it as answer.I will accept it Commented Aug 17, 2018 at 20:46

2 Answers 2

2

Would suggest adding that default value in your procedure itself and don't pass anything while calling from client code like

create procedure sp_upn
@imgid varchar(30) = null,
//other declarations
Sign up to request clarification or add additional context in comments.

1 Comment

It is not always null. It may be null or may have an int value
1

You can specify DbType when adding parameter. For example:

sq1.UpdateParameters.Add("imgid", DbType.Int32, null);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.