gastao Posted August 21, 2014 Report Share Posted August 21, 2014 (edited) tou a fazer um form onde tou a carregar varias tabelas onde um campo é comum e depois consoantes os campos que necessito tenho outros meios de validação. Entretanto ao carregar uma parte de código ao ter dados ele preenche me os dados e se nao tiver um try ele deixa me avançar só que ao meter um try neste bocado de codigo rebenta sempre e anda me ali a fazer loop quando tenho tudo exactamente ao restante. ' 'chama a tabela dos telefones para os contatos - Telephone If consql.State = ConnectionState.Open Then consql.Close() consql.Open() Dim cmd3 As New SqlCommand("Select * from telephone where personid = '1' and partyid = '" & LB_PartyID.Text & "'", consql) Dim drdr3 As SqlDataReader = cmd3.ExecuteReader drdr3.Read() If drdr3.Item("telephonenumber") IsNot DBNull.Value Then TextBox13.Text = drdr3.Item("telephonenumber") Else TextBox13.Text = "" End If Edited August 21, 2014 by gastao Link to comment Share on other sites More sharing options...
nelsonr Posted August 21, 2014 Report Share Posted August 21, 2014 Não sei se estou a perceber. Esse codigo funciona mas ao colocares um try catch deixa de funcionar? Provavelmente será melhor mostrares o codigo com o try catch e indicar qual o erro Link to comment Share on other sites More sharing options...
gastao Posted August 22, 2014 Author Report Share Posted August 22, 2014 (edited) Try 'chama a tabela dos telefones para os contatos - Telephone If consql.State = ConnectionState.Open Then consql.Close() consql.Open() Dim cmd3 As New SqlCommand("Select * from telephone where personid = '1' and partyid = '" & LB_PartyID.Text & "'", consql) Dim drdr3 As SqlDataReader = cmd3.ExecuteReader drdr3.Read() If drdr3.Item("telephonenumber") IsNot DBNull.Value Then TextBox13.Text = drdr3.Item("telephonenumber") Else TextBox13.Text = "" End If Catch ex As Exception MsgBox(ex.Message, MsgBoxStyle.Critical) End Try Quando chamo um cliente que tem telefone ou seja campo nao vazio ele da me o resultado correcto, quando chamo cliente que nao tem telefone ou seja campo vazio da me logo "Tentativa inválida de leitura quando não existem dados." quando eu pelo codigo devia me era aparecer a minha textbox sem nada. Edited August 22, 2014 by gastao Link to comment Share on other sites More sharing options...
l00ker Posted August 22, 2014 Report Share Posted August 22, 2014 (edited) Tenta assim Método 1 If drdr3.Item("telephonenumber") <> "" Then TextBox13.Text = drdr3.Item("telephonenumber").ToString Else TextBox13.Text = "" End If Método 2 If IsDBNull(drdr3.Item("telephonenumber")) = false Then TextBox13.Text = drdr3.Item("telephonenumber").ToString Else TextBox13.Text = "" End If Não teste os códigos mas penso que devem funcionar Edited August 22, 2014 by l00ker Link to comment Share on other sites More sharing options...
nelsonr Posted August 22, 2014 Report Share Posted August 22, 2014 Então o problema não tem a ver com o try catch, mas sim com o campo telefone estar a vazio? O teu código tem um problema que é quando não encontra registos nesse SELECT. Estás a fazer o drdr3.Read() mas não verificas se leu algum registo e acedes logo de seguida. O mais correcto será algo assim: If drdr3.Read() Then If drdr3.Item("telephonenumber") IsNot DBNull.Value Then TextBox13.Text = drdr3.Item("telephonenumber") Else TextBox13.Text = "" End If End If Link to comment Share on other sites More sharing options...
gastao Posted August 22, 2014 Author Report Share Posted August 22, 2014 da maneira que disseste para fazer nelsonR nao rebenta sim, mas nao me devolve o "Vazio", imagina chamo primeiro um cliente com dados, esse dado quando chamo os outro que nao tenha dados nao me chama o vazio, ele nao me faz o else... Link to comment Share on other sites More sharing options...
gastao Posted August 22, 2014 Author Report Share Posted August 22, 2014 Os teus metodos l00ker já os tinha testado e fazem o mesmo que o meu dão sempre "Tentativa inválida de leitura quando não existem dados." o metodo do nelsonr nao rebenta mas continua sem me devolver o vazio Link to comment Share on other sites More sharing options...
l00ker Posted August 22, 2014 Report Share Posted August 22, 2014 E testaste os metodos que postei sem o Try catch? Link to comment Share on other sites More sharing options...
gastao Posted August 22, 2014 Author Report Share Posted August 22, 2014 sim com ou sem try catch ele funciona na perfeição, a unica coisa que nao faz é o else Link to comment Share on other sites More sharing options...
l00ker Posted August 22, 2014 Report Share Posted August 22, 2014 (edited) If drdr3.Read() Then If Convert.ToString(drdr3.Item("telephonenumber")) ="" Then TextBox13.Text = "" Else TextBox13.Text = drdr3.Item("telephonenumber") End If End If Edited August 22, 2014 by l00ker Link to comment Share on other sites More sharing options...
gastao Posted August 22, 2014 Author Report Share Posted August 22, 2014 If drdr3.Read() Then If Convert.ToString(drdr3.Item("telephonenumber")) ="" Then TextBox13.Text = "" Else TextBox13.Text = drdr3.Item("telephonenumber") End If End If tambem nao funciona ele nao faz o Else ele lê e faz logo o endIf e tou a fazer step by step para ver o que se está a passar. Link to comment Share on other sites More sharing options...
l00ker Posted August 22, 2014 Report Share Posted August 22, 2014 O que ele te está a apresentar na textbox? Link to comment Share on other sites More sharing options...
gastao Posted August 22, 2014 Author Report Share Posted August 22, 2014 se eu carregar imagina primeiro um contato com numero, ele fica me la com o contato ou seja nao me apresenta o falor Null que deveria apresentar se o contato tivesse vazio. se eu carregar um contato com valor vazio ele apresenta vazio mas pelo que tou a ver pelo codigo ele nem o else faz ja assume assim mm Link to comment Share on other sites More sharing options...
l00ker Posted August 22, 2014 Report Share Posted August 22, 2014 If drdr3.Read() Then If Convert.ToString(drdr3.Item("telephonenumber")) ="" Then Else TextBox13.Text = drdr3.Item("telephonenumber") If TextBox13.text = "" Then TextBox13.text = "Valor nulo" End If End If Link to comment Share on other sites More sharing options...
gastao Posted August 22, 2014 Author Report Share Posted August 22, 2014 Continua a nao dar ele le o "If drdr3.Read() Then" e depois faz logo "End If" Link to comment Share on other sites More sharing options...
l00ker Posted August 22, 2014 Report Share Posted August 22, 2014 (edited) drdr3.Read() If Convert.ToString(drdr3.Item("telephonenumber")) ="" Then Else TextBox13.Text = drdr3.Item("telephonenumber") If TextBox13.text = "" Then TextBox13.text = "Valor nulo" End If End If Edited August 22, 2014 by l00ker Link to comment Share on other sites More sharing options...
gastao Posted August 22, 2014 Author Report Share Posted August 22, 2014 (edited) continua sem dar e com a mesma situação, bue estranho mm, tou sem ideias Edited August 22, 2014 by gastao Link to comment Share on other sites More sharing options...
nelsonr Posted August 22, 2014 Report Share Posted August 22, 2014 Se quando faz o teste do if drdr3.Read() passa logo para o endif é porque não retorna registo nenhum. Se não tens registos não podes ler os valores. Se queres que limpe a textbox se não existir registo, podes fazer algo assim TextBox13.Text = "" If drdr3.Read() Then If drdr3.Item("telephonenumber") IsNot DBNull.Value Then TextBox13.Text = drdr3.Item("telephonenumber") End If End If Link to comment Share on other sites More sharing options...
gastao Posted August 22, 2014 Author Report Share Posted August 22, 2014 Solução Feita é mesmo isso amigo assim funciona na perfeição... Link to comment Share on other sites More sharing options...
bids Posted August 23, 2014 Report Share Posted August 23, 2014 On Error Resume Next Ajude-me? Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now