gregurs Posted April 25, 2012 at 11:37 PM Report #451498 Posted April 25, 2012 at 11:37 PM Boa noite pessoal, estou a fazer um programa que faz o registo de utilizadores e como tal se estiver a inserir utilizadores repetidos ele deve-me avisar, o problema é que dá este erro ExecuteNonQuery: Connection property has not been initialized. o código que tenho para isso é: private void button1_Click(object sender, EventArgs e) { string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"; SqlConnection cn = new SqlConnection(connection); try { cn.Open(); } catch (Exception) { MessageBox.Show("Did not connect"); } SqlCommand comand = null; if (textBox1.Text == "" || textBox2.Text == "") { MessageBox.Show("Não estão preenchidos todos os campos"); } else if (textBox1.Text != "" && textBox2.Text != "") { string sqlquery = ("Select distinct Username From Utilizadores Where Username IN (Select Username From Utilizadores Group By Username Having COUNT(*)>1"); comand = new SqlCommand(sqlquery); comand.ExecuteNonQuery(); MessageBox.Show("Utilizador já registado na base de dados"); } else { string sqlquery1 = "INSERT INTO [utilizadores] (Nome,Username,Pass,Email) VALUES('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')"; SqlCommand comand1 = new SqlCommand(sqlquery1, cn); comand1.ExecuteNonQuery(); MessageBox.Show("Registo concluido com sucesso"); textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = ""; } }
rumbafum Posted April 25, 2012 at 11:47 PM Report #451503 Posted April 25, 2012 at 11:47 PM passa a conexão no construtor do comando sql que deverá funcionar comand = new SqlCommand(sqlquery, cn);
gregurs Posted April 26, 2012 at 12:00 AM Author Report #451506 Posted April 26, 2012 at 12:00 AM Dá erro incorrect syntax nar '1'
rumbafum Posted April 26, 2012 at 08:00 PM Report #451638 Posted April 26, 2012 at 08:00 PM esse parece um erro de sql. Em que linha é?
migueldesousa Posted April 27, 2012 at 03:38 PM Report #451793 Posted April 27, 2012 at 03:38 PM Boas, ainda tens o problema?? tenho só umas dicas se as quiseres aceitar 🙂 1º acho que deverias juntar os dois queries e executar tudo de uma vez. 2º deves fechar a conexão. 3º a ultima coisa que deves fazer antes de executar é abrir a conexão para não a deixares pendurada e pode te dar um timeout. áááá caralhassssssss
apocsantos Posted May 1, 2012 at 06:59 PM Report #452504 Posted May 1, 2012 at 06:59 PM string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"; SqlConnection cn = new SqlConnection(connection); cn.open(); // se colocares a conecção dentro de um try ... catch assim que ele termine o bloco, destroi o objecto cn SqlCommand comand = null; if (textBox1.Text == "" || textBox2.Text == "") { MessageBox.Show("Não estão preenchidos todos os campos"); } else if (textBox1.Text != "" && textBox2.Text != "") { string sqlquery = ("Select distinct Username From Utilizadores Where Username IN (Select Username From Utilizadores Group By Username Having COUNT(*)>1"); comand = new SqlCommand(sqlquery); comand.ExecuteNonQuery(); MessageBox.Show("Utilizador já registado na base de dados"); } else { string sqlquery1 = "INSERT INTO [utilizadores] (Nome,Username,Pass,Email) VALUES('" + textBox1.Text + "','" + textBox2.Text + "','" + textBox3.Text + "','" + textBox4.Text + "')"; SqlCommand comand1 = new SqlCommand(sqlquery1, cn); comand1.ExecuteNonQuery(); MessageBox.Show("Registo concluido com sucesso"); textBox1.Text = ""; textBox2.Text = ""; textBox3.Text = ""; textBox4.Text = ""; } } "A paciência é uma das coisas que se aprendeu na era do 48k" O respeito é como a escrita de código, uma vez perdido, dificilmente se retoma o habito"
gregurs Posted May 4, 2012 at 01:58 PM Author Report #453071 Posted May 4, 2012 at 01:58 PM já está resolvido obrigado pela ajuda
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