How do you pass null values to a command object parameter???

C

C Newby

I'm sure the answer to this is simple...I hope anyway...

Given the following (C#):

sqlCommand command = new sqlCommand();
command.Connection = someOpenConnectionObject;
command.CommandText = "INSERT INTO myTable ( myTextField ) VALUES (
@myTextField )";

SqlParameter parameter = new SqlParameter();
parameter.ParameterName = "@myTextField";
parameter.Size = 8000;
parameter.DbType = System.Data.DbType.String;
parameter.Value = ARatherBigString;
parameter.IsNullable = true;

command.paramaters.add( parameter );
command.ExecuteNonQuery();


If ARatherBigString contains text, all is well and a new record is inserted
into myTable. However, if ARatherBigString is null, i get a "Prepared
statement ... expects parameter @myTextField " exception from SQL Server.
But if ARatherBigString is null, i *would* like to insert a record with a
null value.

So my question is, what is the best way to do this?

TIA//
 

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