Value conversion

Asked

Viewed 92 times

0

I need the string "hql" that would be my select to convert to int so I can compare values in an if.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using BlogWeb.ViewsModels;
using BlogWeb.DAO;
using BlogWeb.Models;
using BlogWeb.Filters;
using BlogWeb.Controllers;
using NHibernate;
using BlogWeb.Infra;

namespace BlogWeb.Controllers
    {
    public class GerenciamentoKM
        {
        private Rota p;
        private ISession session;
        public GerenciamentoKM(ISession session, Rota p, RotaDAO dao)
            {
            this.p = p;
            this.session = session;
            }
        public void soma(Rota post)
            {
            string hql = "SELECT p.Km_Atual FROM Rota WHERE p.Km_Atual as LAST_INSERT_ID(Km_Atual)";
            int km_t = Convert.ToInt16(hql);
            if (km_t <= p.Km_Atual)
                {
                    ITransaction tx = session.BeginTransaction();
                    session.Update(post);
                    tx.Commit();
                    }
            }
        }
    }

This does not work, I tried later running the query before

public void soma(Rota post)
        {
        string hql = "SELECT p.Km_Atual FROM Rota WHERE p.Km_Atual as LAST_INSERT_ID(Km_Atual)";
        IQuery query = session.CreateQuery(hql);
         query.List<Rota>();
        int km_t = Convert.ToInt16(hql);
        if (km_t <= p.Km_Atual)
            {
                ITransaction tx = session.BeginTransaction();
                session.Update(post);
                tx.Commit();
                }


        }

That way it didn’t work either, so I also tried to start her from the outside and pull to the public.

public IList<Rota> Lista()
        {
        string hql = "SELECT p.Km_Atual FROM Rota WHERE p.Km_Atual as LAST_INSERT_ID(Km_Atual)";
        IQuery query = session.CreateQuery(hql);
        return query.List<Rota>();
        }
public void soma(Rota post)
        {
        string hql = "SELECT p.Km_Atual FROM Rota WHERE p.Km_Atual as LAST_INSERT_ID(Km_Atual)";
        if (Lista <= p.Km_Atual)
            {
                ITransaction tx = session.BeginTransaction();
                session.Update(post);
                tx.Commit();
                }


        }
  • Wouldn’t you have to run the query first? And then convert the result?

  • I tried to run with the query but still, it does not convert to integer.

  • You need to run the query before trying to do anything. Your current code is trying to convert the string "SELECT p.Km_Atual ...". Understands?

  • I tried to run the query before trying to convert the data but now it informs that it is part of a group of methods, and if I try to put in the same public void, the void will not stop using the query’s Return.

  • @Guilhermepadovam You can’t understand what you’re talking about. Please try to be clearer. Give more details, [Dit] the question may help.

  • I changed the question to show what I tried to do to run this code.

  • When I try to update for information exchange, this error appears: System.Formatexception was not manipulated by user code Hresult=-2146233033 Message=The input character string was not in a correct format.

  • About your tests as some have already said you should run the query first and then get the result and convert. Check the hints on this link. http://gabsferreira.com/como-fazer-queries-no-nhibernate-criteria-queryover-linq-to-nhibernate-hql-e-sql/

Show 3 more comments

2 answers

2

Try this way:

string hql = "SELECT p.Km_Atual FROM Rota WHERE p.Km_Atual as LAST_INSERT_ID(Km_Atual)";
SqlCommand cmd = new SqlCommand(hql, conn); //conn é a string da conexão.
conn.Open();
km_t = (Int32)cmd.ExecuteScalar();
  • I tried to test, the problem that the connection class that would be "Conn" cannot be imported into class and it is under another name that would be Web.config

0

Use the "top 1" parameter, so your SQL statement will return a single value and not a list of values:

        SELECT top 1 p.Km_Atual FROM Rota WHERE p.Km_Atual as LAST_INSERT_ID(Km_Atual)  

Browser other questions tagged

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