Raphael Guarita Raphael Guarita -4 years ago 46
ASP.NET (C#) Question

Aafter create a routine to check if user is registred at BD get error

after include a routine to check if an user "Usuario" is Registered at database i'm getting error message below from any navigator:

error '80020009'
/novo.asp, line 31

The routine basic is

set verificar = conexao.execute ("select Usuario from empresas")
while not verificar.eof

verificar.movenext

if Cadastrar_CodEmpresa = verificar("Usuario") Then
response.Redirect("index.asp?pagina=login")



if not verificar.eof Then

verificar.movenext
End if


set cadastrar_cadastro = conexao.execute("insert into Empresas (Usuario,Telefone) Values ('" & Cadastrar_CodEmpresa & "' , '" & Cadastrar_Telefone & "')")



End if Wend


Following My full code:

if request.Form("commentForm") = "sim" then



Cadastrar_CodEmpresa = request.Form("CodEmpresa")
Cadastrar_Segmento = request.Form("Segmento")
Cadastrar_Endereco = request.Form("Endereco")
Cadastrar_Bairro = request.Form("Bairro")
Cadastrar_Cidade = request.Form("Cidade")
Cadastrar_CEP = request.Form("CEP")
Cadastrar_Pais = request.Form("Pais")
Cadastrar_Contato = request.Form("Contato")
Cadastrar_Telefone = request.Form("Telefone")
Cadastrar_Email = request.Form("email")


set verificar = conexao.execute ("select Usuario from empresas")
while not verificar.eof

verificar.movenext

if Cadastrar_CodEmpresa = verificar("Usuario") Then
response.Redirect("index.asp?pagina=login")



if not verificar.eof Then

verificar.movenext
End if


set cadastrar_cadastro = conexao.execute("insert into Empresas (Usuario,Telefone) Values ('" & Cadastrar_CodEmpresa & "' , '" & Cadastrar_Telefone & "')")



End if Wend

Answer Source

It seems that you get that error message if a recordset field is null. What I believe that you are trying to do is to redirect a user to a login page if he's already registered, otherwise insert a new record into the database. There's a simpler way of doing this

set verificar = conexao.execute ("select * from empresas where Usuario = '"& Cadastrar_CodEmpresa &"'")

If not (verificar.eof and verificar.bof) Then
    verificar.close
    response.Redirect("index.asp?pagina=login")
Else
    verificar.close
    conexao.execute("insert into Empresas (Usuario,Telefone) Values ('" & Cadastrar_CodEmpresa & "' , '" & Cadastrar_Telefone & "')")
End If

The logic of this is, if the form value "Codempressa" doesn't match a value in the database, you get an empty recordset and a database insert occurs, if the value does match then you get a redirect.

I should add a warning seeing as you're inserting data - if this is a public facing page then you need to look at ways of guarding against sql injection attacks

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download