I need to convert a string to int within an hql

Asked

Viewed 100 times

0

I need to change the string to int, however it is located within an hql, I probably have to finish the query, but it error to enter the database, my code is a c# with Asp.net, mvc, nhibernate.

public void soma(Rota post)
        {
        //* Tentativa de criação de query ja foi efetuada e não deu certo.
        string hql = "SELECT Km_Atual FROM Rota WHERE Id=LAST_INSERT_ID()";
        //* Tentar converter o hql para int, para fazer comparação em if.
        int Km_Ultima = Convert.ToInt16(hql); 
        if (Km_Ultima <= p.Km_Atual)
  • You’ve asked this before, right?

  • What mistake friend

  • At one point yes, but it was the whole code and it was another question

  • The error that appears is that "The input string was not in a correct format." when I call this term

  • @Hermepads It’s because you’re trying to convert a string "SELECT Km_Atual FROM Rota WHERE Id=LAST_INSERT_ID()" in short. That doesn’t make sense. You shouldn’t run the query before?

  • I’ll try, but last time I said it was a bunch of methods and I couldn’t convert

Show 1 more comment

1 answer

0

As LINQ said, you are trying to convert the string "SELECT Km_Atual FROM Rota WHERE Id=LAST_INSERT_ID()" instead of the value the database will return.

To do this you will need to make a connection to the database and run the command, so you convert the returned value and store it in a variable.

Example:

string hql = "SELECT Km_Atual FROM Rota WHERE Id=LAST_INSERT_ID()";
IQuery query = applicationSession.CreateQuery(hql).UniqueResult();
int Km_Ultima = int.Parse(query.ToString());

if (Km_Ultima <= p.Km_Atual)
  • The applicationSession will do what in the code, is that it is showing error and I want to know what will result.

  • @Guillhermepadovam what error?

  • applicationSession is the session with your ue database. I put this example name only. To instantiate: using (ISession applicationSession = MySQLSessionFactory.StartSession())

Browser other questions tagged

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