petko_stankoski
petko_stankoski

Reputation: 10713

Updating in database throws SqlException

Here is my SQL statement:

UPDATE InboxItem 
SET OrderId = @0, Item = @1, Quantity = @2, Price = @3, 
    Memo = "", InboxId = @4, VATId = @5 
WHERE Id = @6

This throws SqlException, saying that

An object or column name is missing or empty. For SELECT INTO statements, verify each column has a name. For other statements, look for empty alias names. Aliases defined as \"\" or [] are not allowed. Change the alias to a valid name.

I think I know what the problem is. If I give value to Memo, everything is ok. However, I sometimes want memo to be empty string. How to do this?

Upvotes: 1

Views: 346

Answers (2)

GrayFox374
GrayFox374

Reputation: 1782

var sb = new StringBuilder();            
sb.Append("UPDATE InboxItem ");
sb.Append("SET OrderId = @0, Item = @1, Quantity = @2, Price = @3, ");
sb.Append("Memo = @7, InboxId = @4, VATId = @5 ");
sb.Append("WHERE Id = @6");
var cm = new SqlCommand(sb.ToString());
cm.CommandType = System.Data.CommandType.Text;
cm.Parameters.AddWithValue("@1", Convert.ToInt16("OrderValue"));
//and so on

Upvotes: 1

qJake
qJake

Reputation: 17139

Use '' instead of "". Also verify that your input is escaped properly (perhaps one of the strings has a comma in it that is unescaped).

Upvotes: 5

Related Questions