1

I am just carrying out a query, and have started using parameters. And all was going great, until a null string is passed. It worked fine before, using "name = " + Name Where name may or maynot be null.

Is there as simple way around this?

Also on the topic, I havent tried it yet, but can I identify parameters by anything other than numbers. It is a potential area to go wrong, having 14 number parameters with no identification to which is which, except hoping they are in the correct order. (What if i come to add another in)

Error message : The parameterized query '(@0 nvarchar(5),@1 nvarchar(4),@2 nvarchar(4000),@3 nvarchar(400' expects the parameter '@2', which was not supplied.

The Parameterised query:

            db.Execute("Update User SET Name = @0 , Address1 = @1 , Address2 = @2 , Address3 = @3, Address4 = @4 , Postcode = @5 , Title = @6, " +
                        " Surname = @7 , Forename = @8 , Tel = @9, Fax = @10 , Mobile = @11 , Email = @12  WHERE UserNo = @13",
                        Name, Address1, Address2, Address3, Address4, Postcode, Title, Surname, Forename, Tel, Fax, Mobile, Email, UserNo);

1 Answer 1

1
db.Execute("Update User SET Name = @0 , Address1 = @1 , Address2 = @2 , Address3 = @3, Address4 = @4 , Postcode = @5 , Title = @6, " +
                        " Surname = @7 , Forename = @8 , Tel = @9, Fax = @10 , Mobile = @11 , Email = @12  WHERE UserNo = @13",
                        (Name==null)?"":Name, (Address1==null)?"":Address1, ...);
Sign up to request clarification or add additional context in comments.

2 Comments

Okay, so thats similar to doing Name ?? "" ? What does the :Name bit do? I was hoping not to do this to every nullable parameter.
condition ? first_expression : second_expression; its conditional operator. In this case yes its same as Name ?? ""

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.