erro codigo

.NET

28/03/2008

Fala pessoas beleza?
No meu projeto tenho uma parte que o usuário deve inserir seu cpf para poder logar . O sistema faz a pesquisa no banco, se existir vai para a pagina default senao existir vai para de erro
tenho uma classe empregado aonde tem um metodo que faz essa pesquisa por stored procedure e que recebe como parametro o textbox.
a classe esta sendo chamada atraves do comando Empregados.SelectByCpf(TextBox1.Text); mas esta dando erro bem nessa linha...ja tentei d tudo.
abaixo segue minha stored , minha classe e o codigo do botão...
Esta dando o seguinte erro...
Referência de objeto não definida para uma instância de um objeto.
Detalhes da Exceção: System.NullReferenceException: Referência de objeto não definida para uma instância de um objeto.

Linha 23: protected void Button1_Click(object sender, EventArgs e)
Linha 24: {
Linha 25: Empregados.SelectByCpf(TextBox1.Text) ;
Linha 26:



Stored Procedure

USE [CadastroIndustrial]
GO
/****** Object: StoredProcedure [dbo].[Cadastro_EmpregadoSelect] Script Date: 03/27/2008 13:55:43 ******/
SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO

ALTER PROCEDURE [dbo].[Cadastro_EmpregadoSelect]
(
@cpf int
)
AS
SELECT
*
FROM
Cadastro_EmpregadoSelect
WHERE
cpf = @cpf and terminado = 0


codigo botao

protected void Button1_Click(object sender, EventArgs e)
{



if (Empregados.SelectByCpf(TextBox1.Text))
{
Response.Redirect(´~/P_Geraf.aspx´);

}

else
{
Response.Redirect(´~/P_Erro.aspx´);
}

}
}


classe
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
///Importado
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Collections.Generic;


namespace Fiemt.CadastroIndustrial
{

[System.ComponentModel.DataObject]
public class Empregados
{

private static readonly string _connectionString;
private int _cpf;

public int Cpf
{
get { return _cpf; }
set { _cpf = value; }
}

static Empregados()
{
_connectionString = WebConfigurationManager.ConnectionStrings[´CadastroIndustrialString´].ConnectionString;
}


public Empregados(SqlDataReader reader)
{
_cpf = (int)reader[´cpf´];
}


/// <summary>
/// seleciona empregado por cpf
/// </summary>


[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
public static Empregados SelectByCpf(string cpf)
{
///initialize command
SqlConnection con = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand(´Cadastro_EmpregadoSelect´, con);
cmd.CommandType = CommandType.StoredProcedure;

///initilize parameters
cmd.Parameters.AddWithValue(´@cpf´, cpf);
Empregados result = null;

using (con)
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
if (reader.HasRows)
{
return true;
}
con.Close();

}
}

}



}


Gusdorta

Gusdorta

Curtidas 0

Respostas

Gusdorta

Gusdorta

28/03/2008

os codigos atuais do botao e da classe são


protected void Button1_Click(object sender, EventArgs e)
{



if (Empregados.SelectByCpf(TextBox1.Text))
{
Response.Redirect(´~/P_Geraf.aspx´);

}

else
{
Response.Redirect(´~/P_Erro.aspx´);
}

}



----------------------------------------------------------------------
classe:
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
///Importado
using System.Data.SqlClient;
using System.Web.Configuration;
using System.Collections.Generic;


namespace Fiemt.CadastroIndustrial
{

[System.ComponentModel.DataObject]
public class Empregados
{

private static readonly string _connectionString;
private int _cpf;

public int Cpf
{
get { return _cpf; }
set { _cpf = value; }
}

static Empregados()
{
_connectionString = WebConfigurationManager.ConnectionStrings[´CadastroIndustrialString´].ConnectionString;
}


public Empregados(SqlDataReader reader)
{
_cpf = (int)reader[´cpf´];
}


/// <summary>
/// seleciona empregado por cpf
/// </summary>


[System.ComponentModel.DataObjectMethodAttribute(System.ComponentModel.DataObjectMethodType.Select, true)]
public static bool SelectByCpf(string cpf)
{
///initialize command
SqlConnection con = new SqlConnection(_connectionString);
SqlCommand cmd = new SqlCommand(´Cadastro_EmpregadoSelect´, con);
cmd.CommandType = CommandType.StoredProcedure;

///initilize parameters
cmd.Parameters.AddWithValue(´@cpf´, cpf);



using (con)
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
reader.Read();
if (reader.HasRows)
{
return true;
}
else
{
return false;
}

con.Close();

}
}

}



}


GOSTEI 0
POSTAR