The null value automatically becomes 0

Asked

Viewed 759 times

4

I’m doing a vehicle registration, I need it to register put that whenever a vehicle is registered the value zero instead of putting the value null, I tried by default, but it does not record the information.

I am using SQL Server along with C# in visual studio.

Controller class:

public ActionResult Adiciona(VeiculoModel viewModel)
{
  if (ModelState.IsValid)
  {
    Veiculo Veiculo = viewModel.CriaVeiculo();
    dao.Adiciona(Veiculo);
    //return View();
    return RedirectToAction("Form");
  }
  else
  {
    return View("Index", viewModel);
  }
}

DAO class:

public void Adiciona(Veiculo post)
{
  ITransaction tx = session.BeginTransaction();
  session.Save(post);
  tx.Commit();
}
  • How is the insertion being done? Can put the command being used?

  • I haven’t put it in the middle of programming yet, but there are some inserts here that show you how it works.

  • I don’t understand what difference it would make, since when I run select in a numeric variable, if it is null, it will come zeroed. unless you are using a numeric variable that can be null (decimal? , double? , etc...)

  • is a numeric variable, in case it would be an integer, is that I need it to be zero, because in another class, will compare this value with a new one that will overlap

  • 1

    exactly.... if it is an integer, it will never be null, it is always initialized with 0

3 answers

6


Manages a INSERT using COALESCE (or ISNULL) that generates the final query:

INSERT INTO Veiculo(nome, valor)
VALUES('Gol', COALESCE(NULL, 0));

And, of course, change the column to NOT NULL


Or, transform in the application still, preferably in the method CriaVeiculo:

veiculo.valor = this.valor ?? 0;
// ou
veiculo.valor = 0;
  • This would be in case I put the km record in my view, however I will not put a field for the manual record, wanted more than when you insert the other fields, as the numbering, model, these things will be manually typed, already the km not

  • @Guilhermepadovam Can you detail your problem better? If the value will not be reported in the View, then ensure with veiculo.valor = 0 or he doesn’t get into the INSERT/UPDATE

  • Well my view, will have 3 textbox, but in my comic will have four columns, where will be the model,car number, year and km, I want to register the model, car number and year will be manual by the user, after entering these 3 fields the km will be null, I want to be null, it gets 0 so can make the comparison with another class that will call it as foreign key

  • @Andrefigueiredo worked, I only put in the controller, as a variable, everything worked out, thanks

  • @Guilhermepadovam The most correct way would be to create the default in the bank and in the application not allow to be generated a INSERT including the column km worthwhile NULL. The Entity Framework, for example, can be done as described here

  • The person who downvoted for explaining the reason for discussing an improvement?

Show 1 more comment

3

You can also set the value of the KM field to 0, as default in SQL SERVER. To do this, go to the Design of your table, select the KM field and set the Default Value or Binding to 0

inserir a descrição da imagem aqui

1

SQL SERVER:

SELECT ISNULL(CAMPO,'VALOR') FROM TABELA

Browser other questions tagged

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