locked
Cannot convert type 'int' to 'System.Data.DataTable RRS feed

  • Question

  • User-1658074666 posted

    Hello!

    when I try to run a stored procedure, this error is returned tome.

    Can not convert type 'int' to 'System.Data.DataTable'

    the error happens in this parameter: return AcessoDados.ExecuteNonQuery(command);


    the codes are below:

    using System;
    using System.Data;
    using System.Data.Common;
    using Oracle.DataAccess.Client;
    
    namespace PreCadastros
    {
        public static class AcessoDados
        {
            static AcessoDados()
            {
            }
            //Cria um comando
            public static DbCommand CreateCommand()
            {
                string dbProviderName = Config.DbProviderName;
                string dbConnStr = Config.DbConnStr;
                DbProviderFactory factory = DbProviderFactories.GetFactory(dbProviderName);
                DbConnection objConn = factory.CreateConnection();
                objConn.ConnectionString = dbConnStr;
                DbCommand objCmd = objConn.CreateCommand();
                objCmd.CommandType = CommandType.StoredProcedure;
                return objCmd;
            }
    
            public static DataTable ExecuteReader(DbCommand objCmd)
            {
                DataTable table;
                try
                {
                    objCmd.Connection.Open();
                    DbDataReader reader = objCmd.ExecuteReader();
                    table = new DataTable();
                    table.Load(reader);
                }
                catch (Exception ex)
                { throw ex; }
                finally
                { objCmd.Connection.Close(); }
                return table;
            }
    
            public static int ExecuteNonQuery(DbCommand objCmd)
            {
                int linhasAfetadas = -1;
                try
                {
                    objCmd.Connection.Open();
                    linhasAfetadas = objCmd.ExecuteNonQuery();
                }
                catch (Exception ex)
                { throw ex; }
                finally
                { objCmd.Connection.Close(); }
                return linhasAfetadas;
            }
    
            public static string ExecuteScalar(DbCommand objCmd)
            {
                string valor = "";
                try
                {
                    objCmd.Connection.Open();
                    valor = objCmd.ExecuteScalar().ToString();
                }
                catch (Exception ex)
                { throw ex; }
                finally
                { objCmd.Connection.Close(); }
                return valor;
            }
        }
    }
    
    
    
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data;
    using System.Data.Common;
    using Oracle.DataAccess.Client;
    using Oracle.DataAccess.Types;
    
    namespace PreCadastros
    {
        public class DAL1001
        {
            public static DataTable Create1001()
            {
               string nome;
               string cadastro;
    
                DbCommand command = AcessoDados.CreateCommand();
                command.CommandText = "sp_1001_c";
                command.Parameters.Add(":NOME");
                command.Parameters.Add(":CADASTRO");
                command.Parameters[0].Value = nome;
                command.Parameters[1].Value = cadastro;
                return AcessoDados.ExecuteNonQuery(command);
            }
    
    Friday, July 29, 2011 7:58 AM

Answers

  • User-1412735316 posted

    Got it. Sorry didn't c it early.

    Your execute nonquery method is returning integer on the other hand the return type of  create1001 data table. And integer can't be automatically cast to datatable.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 29, 2011 8:23 AM
  • User-1412735316 posted

    if you change public static DataTable Create1001() to public static int Create1001() 
    then your problem will be solved if you there is no such reason for datatabe as return type.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 29, 2011 8:25 AM

All replies

  • User-551623815 posted

    Pass the value of the parameters

    command.Parameters.Add(":NOME");
    Friday, July 29, 2011 8:04 AM
  • User-1658074666 posted

    Pass the value of the parameters

    command.Parameters.Add(":NOME");

    I edited my post with the updated data, the error persists

    Friday, July 29, 2011 8:05 AM
  • User-1011137159 posted

    In this code  linhasAfetadas = objCmd.ExecuteNonQuery(); you are trying exectues sql statement which is returs some data. So please check your Store prodecedure/sql query.

    Friday, July 29, 2011 8:10 AM
  • User-551623815 posted

    Post your stored procedures code

    Friday, July 29, 2011 8:11 AM
  • User-1412735316 posted

    Pelase share your SP.

    Friday, July 29, 2011 8:14 AM
  • User-1658074666 posted
    CREATE OR REPLACE PROCEDURE sp_1001_c
    (
      p_id       OUT tb_1001.id%TYPE
     ,p_nome     IN tb_1001.nome%TYPE
     ,p_cadastro IN tb_1001.cadastro%TYPE
     ,p_stx001   IN tb_1001.stx001%TYPE
     ,p_erro     OUT NUMBER
    ) AS
      v_msg_erro CLOB;
      LF         CHAR(2):= CHR(10);
    BEGIN
      p_erro := 0;
      SELECT nvl(MAX(id), 0)
        INTO p_id
        FROM tb_1001;
      INSERT INTO tb_1001
        (id
        ,nome
        ,cadastro
        ,stx001
        ,stx002)
      VALUES
        (p_id
        ,fn_c0004(p_nome)
        ,p_cadastro
        ,p_stx001
        ,SYSDATE);
      COMMIT;
      sp_0009_i(p_stx001,1,p_id,NULL,'tb_1001',1);
    EXCEPTION
      WHEN OTHERS THEN
        v_msg_erro := SQLERRM;
        v_msg_erro := TRIM(v_msg_erro||LF);
        v_msg_erro := TRIM(v_msg_erro||LF)||'Registro: '||p_id;
        sp_0012_i(p_erro,p_stx001,1,v_msg_erro,'sp_1001_c',3);
    END;
    Friday, July 29, 2011 8:18 AM
  • User-1412735316 posted

    Got it. Sorry didn't c it early.

    Your execute nonquery method is returning integer on the other hand the return type of  create1001 data table. And integer can't be automatically cast to datatable.

    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 29, 2011 8:23 AM
  • User-1412735316 posted

    if you change public static DataTable Create1001() to public static int Create1001() 
    then your problem will be solved if you there is no such reason for datatabe as return type.
    • Marked as answer by Anonymous Thursday, October 7, 2021 12:00 AM
    Friday, July 29, 2011 8:25 AM
  • User-1658074666 posted

    thx Shuvo Aymon!

    this realy solved my problem. Smile

    Friday, July 29, 2011 8:31 AM