Ariel Schiffini
Ariel Schiffini

Reputation: 11

Cannot implicity convert type object to system.data.datatable

Hi I'm trying to do the following but it throws the error title. As I can fix? Thanks!!!

public static object conectar(string query)
{
    DataTable Tabla = new DataTable();
    SqlConnection connection = BaseDatos1.getConexion();
    SqlDataAdapter Da = new SqlDataAdapter();
    DataSet Ds = new DataSet();
    Da = new SqlDataAdapter(query, connection);
    Da.FillSchema(Tabla, SchemaType.Source);
    Da.FillLoadOption = LoadOption.PreserveChanges;
    Da.Fill(Tabla);
    BaseDatos1.closeConnection();
    return Tabla;
}

public static string Verificar()
{
    bool functionReturnValue ;
    DataTable Dt1;
    DataTable Dt2;
    DataTable Dt3;
    int j;
    int k;
    int DigitoVerificador;
    int Acum;
    string A;
    string tablas = "";
    string[] table = new string[6];
    string registro = "";
    bool errorEnTablaActual;
    int reg = 0;
    try {
        functionReturnValue = true;
        //Verifico en Base Seguridad
        **Dt1 = conectar("select Tabla from DigitoVerificador");**

Upvotes: 1

Views: 933

Answers (1)

stuartd
stuartd

Reputation: 73313

You get the error because the conectar method returns an object, which you then assign to Dt1, which is a DataTable, and so you get the message that the value cannot be implicitly converted..

You can explicitly cast the return value to a DataTable, as the conectar method never returns null:

Dt1 = (DataTable)conectar("select Tabla from DigitoVerificador");

Alternatively, and probably better, you would change the return type of conectar to DataTable.

Upvotes: 2

Related Questions