Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
583 views
in Technique[技术] by (71.8m points)

c# - ExecuteScalar returns null or DBNull (development or production server)

I'm trying to add a column to an existing DataRow in C#. Afterwards the column will be filled with a single value from my database.

DataRow dr already exists and column "COLNAME" also exists.
comTBP is my SqlCommand.

dr["COLNAME"] = Convert.ToInt32(comTBP.ExecuteScalar());

This all works fine if there is a value in my database and ExecuteScalar() can get that value. If I test this code on my development server (local) it also works if ExecuteScalar() return null or DBNull and the value of my new column is 0. But the problem appears if I deploy my code to the production server. If I do everything the same, with the same database it throws an Exception with a message that it can't convert DBNull to Int32.
My question is why does this error appear on the production server and not on my local development server?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

ExecuteScalar returns DBNull for null value from query and null for no result. Maybe on your development server it never occured (null result from query).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...