Read column values - C#

Asked

Viewed 212 times

1

I need to read the values of a column called Code_article and place them, only when the column is still empty, I execute and immediately gives the error:

The object reference was not defined as an instance of a object.

Here is my code:

conn.Open();
SqlCommand cmd6 = new SqlCommand("SELECT Code_Article FROM ArticleBarCode", conn);
Int32 codeexistente = (Int32)cmd6.ExecuteScalar();
conn.Close();

What I can change in the code for when the column is empty, do not give error, but return for example 0 ?

  • Do you still have the problem in your code? Because SQL returns a list or no value, what is the purpose you need to do in your code?

2 answers

1

First: check that cmd6 is not null, this is a possibility.

For your conversion, it’s not a good idea to do this cast as the return of ExecuteScalar() can be null and this can also cause a mistake.

A good idea is to use the Convert.ToInt32, if you are sure that values can only be null or a whole:

Int32 codeexistente = Convert.ToInt32(cmd6.ExecuteScalar());

If that cannot be guaranteed, the best thing to do is to receive this result in a int? and validate it

int? code = cmd6.ExecuteScalar() as int?;
Int32 codeexistente = code == null ? 0 : code;

What I would simplify to

Int32 codeexistente = (cmd6.ExecuteScalar() as int?) ?? 0;

And it is also possible to do query, using the IsNull of SQL Server

SqlCommand cmd6 = new SqlCommand("SELECT IsNull(Code_Article, 0) FROM ArticleBarCode", conn);
  • Still not working with this method, gives the same error

  • So the problem is elsewhere =D

  • I don’t know how to fix

  • Well, I know. I just need to know where the bug is. You have how to see which line bursts the bug?

  • Error bursts on line: Int32 coded = (Int32)cmd6.Executescalar();

  • cmd6 is void at time of error?

  • Yes, this is inside a foreach and the first time foreach runs, cmd6 is empty, because only then will I add values to that column

  • If cmd6 is null, that is the cause of the error.

  • I know that, but my question is "how can I solve this?". Because in the first foreach the column is empty, then add, then in the next foreach, it is no longer empty, it will work. I just needed to figure out that part when the column is empty at the beginning

  • You’re not talking about the same thing I am. I’m talking about object cmd6, that is to say, the object of the command that will be executed.

  • The object cmd6 will bring the value of the column, right ?

  • Wrong. It is the object of the command, the return is "brought" by the method ExecuteScalar()

  • Got it. So you know how to tell me ?

  • Just put the really relevant code in the question (this should have been done at the beginning already).

  • But this is the error code, this is where I create the cmd6, it doesn’t appear anywhere else !

  • But here in the comments you speak in foreach, where is this foreach?

Show 12 more comments

1

This error happens due to return being nullo and you try to record in a whole, so returns that error.

To solve can use the ISNULL sql server, thus:

SELECT ISNULL(Code_Article,0) FROM ArticleBarCode

Thus, if the Code_Article either empty or nullo will return 0 to your application.

  • I tried it and it doesn’t work anyway, it makes the same mistake

Browser other questions tagged

You are not signed in. Login or sign up in order to post.