exception

17/06/2008

0

Ola...

Pessoal, fazendo a aula do Guinther de Multitier, ao executar a aplicação para fazer update, encontro o seguinte erro:

Exception has been thrown by the target of an invocation

Alguem já teve o mesmo problema.

Eu criei inclusive outra Solution... fiz a classe novamente... e da o mesmo pau...

Segue a classe:

using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Data;

namespace DAL
{
    public class Magazines
    {
        private String ConStr = @"Data Source=FERNANDA_XP\"+
            "SQLEXPRESS;Initial Catalog=magazines;Integrated"+" Security=True";
        public DataSet Select()
        {
            SqlConnection con = new SqlConnection(ConStr);
            string SQL = "Select * from magazines";
            SqlCommand cmd = new SqlCommand(SQL, con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            da.Fill(ds);
            return ds;
        }

        public void Update(int EDITION, string ARTICLE, double LEVEL)
        {
            SqlConnection con = new SqlConnection(ConStr);
            string SQL = "update magazines set ARTICLE=@ARTICLE, LEVEL=@LEVEL, where EDITION=@EDITION";
            SqlCommand cmd = new SqlCommand(SQL, con);
            cmd.Parameters.AddWithValue("@ARTICLE", ARTICLE);
            cmd.Parameters.AddWithValue("@LEVEL", LEVEL);
            cmd.Parameters.AddWithValue("@EDITION", EDITION);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }
    }
}



Ffpradella

Ffpradella

Responder

Posts

18/06/2008

Ricardo Silva

opa...

antes do where não tem a virgula

tira ela e ve se funciona

abraços


Responder

18/06/2008

Ffpradella

Perfect!

Obrigada!


Responder

18/06/2008

Ffpradella

Rica,

Sem querer abusar da sua boa vontade..
qual a outra burrice que eu cometi no Insert?

public void Insert(int Edition, string Article, double Level)
        {
            SqlConnection con = new SqlConnection(ConStr);
            string SQL = "insert into magazines (Edition,Article,Level) values(@Edition,@Article,@Level)";
            SqlCommand cmd = new SqlCommand(SQL, con);
            cmd.Parameters.AddWithValue("@Article", Article);
            cmd.Parameters.AddWithValue("@Level", Level);
            cmd.Parameters.AddWithValue("@Edition", Edition);
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }


o Erro eh exatamente o mesmo.... :(

Obrigada


Responder

18/06/2008

Ricardo Silva

algum campo é Incremental...
pois se tiver vc não deve colocalo.

tipo se o EditionfPor autoIncremento a estrução sera assim:
string SQL = "insert into magazines (Article,Level) values(@Article,@Level)"; 


e não presisa jogar o parametro do Edition


Responder

18/06/2008

Ffpradella

Edition nao eh autoincremento..
Eh PK mas deve ser informada...

Alguma outra ideia??

Obrigada


Responder

18/06/2008

Ricardo Silva

A Priori, a estrução SQL parece estar certa....
pode ser problemas no parametros. vo te mostrar como eu faria
public void Update(int EDITION, string ARTICLE, double LEVEL) {
            SqlConnection con = new SqlConnection(ConStr);
            string SQL = "update magazines set ARTICLE=@ARTICLE, LEVEL=@LEVEL, where EDITION=@EDITION";
            SqlCommand cmd = new SqlCommand(SQL, con);
            cmd.Parameters.Add("@ARTICLE", SqlDbType.VarChar, 50).Value = ARTICLE;
            cmd.Parameters.Add("@LEVEL", SqlDbType.Decimal).Value = LEVEL;
            cmd.Parameters.Add("@EDITION", SqlDbType.Int).Value = EDITION;
            con.Open();
            cmd.ExecuteNonQuery();
            con.Close();
        }


tenta isso e ve o q acontece...


Responder

18/06/2008

Ricardo Silva

Otra coisa que pode fazer é colocar um BreakPoint na linha do ExecuteNonQuery e ver os valores do Objeto cmd.

é super facil...

na duvida é soh posta...

ah... apos a 13:00 eu do uma olhada no Forum
T++


Responder

18/06/2008

Ffpradella

vc se confundiu...

o Update funcionou corretamente depois de retirar a (,) antes do where..

o pau esta no Insert!

obrigada


Responder

18/06/2008

Ricardo Silva

ops...

não que eu tenha me confundido... é apenas a mania do CRTL+C e CRTL+V
acabei copiando codigo errado... :oops:

Desculpe, espero q naum aconteça denovo (rsrsrsrrs) :wink:
public void Insert(int Edition, string Article, double Level) 
        { 
            SqlConnection con = new SqlConnection(ConStr); 
            string SQL = "insert into magazines (Edition,Article,Level) values(@Edition,@Article,@Level)"; 
            SqlCommand cmd = new SqlCommand(SQL, con); 
            cmd.Parameters.Add("@ARTICLE", SqlDbType.VarChar, 50).Value = ARTICLE; 
            cmd.Parameters.Add("@LEVEL", SqlDbType.Decimal).Value = LEVEL; 
            cmd.Parameters.Add("@EDITION", SqlDbType.Int).Value = EDITION;
            con.Open(); 
            cmd.ExecuteNonQuery(); 
            con.Close(); 
        }



Responder

18/06/2008

Ffpradella

Um amigo me sugeriu a seguinte modificacao no Insert:

cmd.Parameters.AddWithValue(´@Edition´, ´Edition´);

ao inves de:

cmd.Parameters.AddWithValue(´@Edition´, Edition);

Funcionou!
Soh que eu nao entendi o motivo!!


Responder

18/06/2008

Ricardo Silva

opssss....

mas é identico...
axo q vc tambem usa CTRL+C

uhauhaua


Responder

18/06/2008

Ffpradella

Nao eh,
olha as aspas simples no Edition..... :S


Responder

18/06/2008

Ricardo Silva

ata....

mas axo que o que ele ira gravar no banco é a palavra ´Edition´, ao inves do valor armazenado nele.... concorda...

verifica e ve se eu naum estou falando besteira


Responder

18/06/2008

Ffpradella

Gravou certinho... de acordo com os valores colocados nos fields...

:?


Responder

18/06/2008

Ricardo Silva

Legal...
Aprendi mais Uma....

T+


Responder

Assista grátis a nossa aula inaugural

Assitir aula

Saiba por que programar é uma questão de
sobrevivência e como aprender sem riscos

Assistir agora

Utilizamos cookies para fornecer uma melhor experiência para nossos usuários, consulte nossa política de privacidade.

Aceitar