Código:
private void btnIngresar_Click(object sender, System.EventArgs e) { Database conectar = new Database(); int contador = 0; while (conectar.getDataReader().Read()) contador++; if(contador==0) Response.Redirect("index.aspx"); else Response.Redirect("principal.aspx"); }
El error:
Cita:
Server Error in '/CFE' Application.
--------------------------------------------------------------------------------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 58: Database conectar = new Database();
Line 59: int contador = 0;
Line 60: while (conectar.getDataReader().Read())
Line 61: contador++;
Line 62: if(contador==0)
--------------------------------------------------------------------------------
Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.
Source Error:
Line 58: Database conectar = new Database();
Line 59: int contador = 0;
Line 60: while (conectar.getDataReader().Read())
Line 61: contador++;
Line 62: if(contador==0)
La clase Database
Código:
Alguien sabe que esta pasando? using System; using System.Data; using MySQLDriverCS; namespace CFE { /// <summary> /// Summary description for Database. /// </summary> public class Database { string servidor; string bd; string usuario; string password; MySQLConnection conexion; MySQLCommand comando; string sql; MySQLDataReader dataReader; public Database() { this.servidor = "localhost"; this.bd = "CFE"; this.usuario = "root"; this.password = ""; creaConexion(); } private void creaConexion() { MySQLConnectionString cadena = new MySQLConnectionString(this.servidor,this.bd, this.usuario,this.password); this.conexion = new MySQLConnection(cadena.AsString); this.conexion.Open(); } private void creaDataReader() { dataReader = (MySQLDataReader)this.getComando().ExecuteReader(CommandBehavior.CloseConnection); } public MySQLDataReader getDataReader() { return this.dataReader; } public MySQLCommand getComando() { return comando; } public void creaComando() { this.comando.CommandText = this.getSql(); this.comando.Connection = this.getConexion(); } public MySQLConnection getConexion() { return conexion; } public void setSql(string sql) { this.sql = sql; } public string getSql() { return this.sql; } } }