Sebastián
Sebastián

Reputation: 49

Convert dataset into integer

look i have this code:

private void obtengoUltimoComienzoColocado()
    {
        ManejoUsuarios lAdm = new ManejoUsuarios();
        lblTodoOK.Text = lAdm.LevantoUltimoIDBarco().ToString();

    }

public int LevantoUltimoIDBarco()
        {
            ConexionBD lP = new ConexionBD();
            try
            {
                string lQuery = "Select Max(idBarco) as UltimoID From Comienzos;";
                Convert.ToInt32(lP.ObtenerRegistro(lQuery));
                return 1;
            }
            catch (Exception ex)
            {
                return 0;
            }

        }

public DataSet ObtenerRegistro(string SqlQuery)
    {
        DataSet lResult;
        SqlConnection lSqlConnection = new SqlConnection("Data Source=SEBA-PC\\sqlexpress;Initial Catalog=Batalla_Naval;Integrated Security=True");
        SqlCommand lSqlCommand = null;
        try
        {
            lSqlCommand = new SqlCommand();
            lSqlCommand.CommandText = SqlQuery;
            lSqlCommand.CommandType = CommandType.Text;

            SqlDataAdapter lAdapter = new SqlDataAdapter(lSqlCommand);
            lResult = new DataSet();
            lSqlConnection.Open();
            lSqlCommand.Connection = lSqlConnection;
            lAdapter.Fill(lResult);
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            lSqlConnection.Close();
        }
        return lResult;
    }

As you can see i use three functions to go to the database and get the max ID from the table Comienzos, but when i want to convert the data set to int32 the function LevantoUltimoIDBarco returns me 0,

Upvotes: 2

Views: 7424

Answers (3)

Steve
Steve

Reputation: 216293

Looking at how you retrieve your data, then the correct code to read that integer (MAX()) is the following

string lQuery = "Select Max(idBarco) as UltimoID From Comienzos;";
DataSet ds  = lP.ObtenerRegistro(lQuery);
return Convert.ToInt32(ds.Tables[0].Rows[0][0] == DBNull.Value ? 0 : ds.Tables[0].Rows[0][0]);

However, if your table has no record the MAX(idBarco) will return DBNull as result

Upvotes: 0

sm_
sm_

Reputation: 2602

My friend you should use ExecuteScalar to return a single value, and not an adapter and fill a dataset.

 SqlConnection lSqlConnection = new SqlConnection("Data Source=SEBA-PC\\sqlexpress;Initial Catalog=Batalla_Naval;Integrated Security=True");
                SqlCommand lSqlCommand = null;
                try
                {
                    lSqlCommand = new SqlCommand();
                    lSqlCommand.CommandText = SqlQuery;
                    lSqlCommand.CommandType = CommandType.Text;
                    var result = lSqlCommand.ExecuteScalar();
                    int MyID = Convert.ToInt32(result);

                }
                catch(ex)
                {
                    // DO SOMETHING
                }

Upvotes: 1

Raidri
Raidri

Reputation: 17550

You have to select the first value from the dataset, you can not convert a complete dataset to integer:

Convert.ToInt32(lP.ObtenerRegistro(lQuery).Tables[0].Rows[0][0]);

or simpler (since the query returns an integer in the dataset):

(Int32)(lP.ObtenerRegistro(lQuery).Tables[0].Rows[0][0]);

And then you have to return or save the result instead of just returning 1.

Upvotes: 2

Related Questions