Jump to content

Problema com a datagridview e a bd


esponjinha
 Share

Recommended Posts

Boa noite

Tenho uma base de dados com uma tabela .

Onde tenho um form com uma datagridview e uma text box para insercao de dados .

Tenho um botao de adicionar editar e eliminar .

Tenho o botao de adicionar a funcionar .

O de apagar era para clicar na linha da datagridview e apagar so que ele apaga na datagridview mas na base de dados nao apaga.

Ja exprimentei varios codigos de formas diferentes mas nenhuma das formas trabalha.

Alguem consegue me ajudar?

Obrigado

Link to comment
Share on other sites

este e um dos codigos

string sql = "Delete from Marcas where marca= " + dataGridView1.CurrentRow.Cells[0].Value;

		SqlCommand comando = new SqlCommand(sql, con);
		con.Open();
		int i = comando.ExecuteNonQuery();
		if (i > 0)
		{
			MessageBox.Show(i + "Dados Excluídos Com Sucesso!!", "Sucesso", MessageBoxButtons.OK, MessageBoxIcon.Information);

		}
		con.Close();
Link to comment
Share on other sites

Tenho outro codigo

SqlConnection con = new SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename = C:\Users\Filipe\documents\visual studio 2010\Projects\Vmpa software\Vmpa software\Utilizadores.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");

		SqlCommand cmd = new SqlCommand ();
		for (int i = 0; i < dataGridView1.Rows.Count; i++)
		{
			DataGridViewRow dr = dataGridView1.Rows[i];
			if (dr.Selected == true)
			{
				dataGridView1.Rows.RemoveAt(i);
				try
				{
					con.Open();
					cmd.CommandText = "Delete from Marca where marca=" + i + "";
					cmd.ExecuteNonQuery();
					con.Close();
				}
				catch (Exception ex)
				{
					MessageBox.Show(ex.ToString());
				}
			}
		}

este apaga da dgv mas nao apaga da bd e da um erro

Edited by apocsantos
geshi
Link to comment
Share on other sites

tenho outro codigo

for (int i = 0; i < dataGridView1.Rows.Count; i++)
		{

			String strConnection = (@"Data Source=.\SQLEXPRESS;AttachDbFilename = C:\Users\Filipe\documents\visual studio 2010\Projects\Vmpa software\Vmpa software\Utilizadores.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");
			SqlConnection cn = new SqlConnection(strConnection);		
			DataGridViewRow dr = dataGridView1.Rows[i];
					if (dr.Selected == true)
					{
					dataGridView1.Rows.RemoveAt(i);
					cmd.CommandText = "Delete from Marcas where marca ='" + i + "'";
					cn.Open();
					cmd.ExecuteNonQuery();
					MessageBox.Show("Deleted");
					cn.Close();
					 }


		}

este da erro na linha cmd.ExecuteNonQuery();

erro:

ExecuteNonQuery: Connection property has not been initialized.

Link to comment
Share on other sites

Porque não existe nada para apagar.

A tua query:

cmd.CommandText = "Delete from Marcas where marca ='" + i + "'";

Resultado final:

DELETE FROM marcas WHERE marca = 1
DELETE FROM marcas WHERE marca = 2
DELETE FROM marcas WHERE marca = 3

O que tu queres é aceder ao índice da DataGridView e devolver o valor.

Edited by bioshock
Link to comment
Share on other sites

Ja fiz o codigo mas da me um erro nesta linha cmd.ExecuteNonQuery();

int selectrows;

		selectrows = dataGridView1.SelectedRows[0].Index;

		dataGridView1.Rows[selectrows].SetValues(textBox1.Text);


		System.Data.SqlClient.SqlConnection sqlConnection1 =
		new System.Data.SqlClient.SqlConnection(@"Data Source=.\SQLEXPRESS;AttachDbFilename = C:\Users\Filipe\documents\visual studio 2010\Projects\Vmpa software\Vmpa software\Utilizadores.mdf;Integrated Security=True;Connect Timeout=30;User Instance=True");

		string marcas = textBox1.Text;


		System.Data.SqlClient.SqlCommand cmd = new System.Data.SqlClient.SqlCommand();
		cmd.CommandType = System.Data.CommandType.Text;
		cmd.CommandText = "UPDATE Marcas ( marcas ) VALUES (@marcas)";
		cmd.Connection = sqlConnection1;
		cmd.Parameters.AddWithValue("@marcas", marcas);


		try
		{
			if (textBox1.Text.Trim() == "")
			{
				MessageBox.Show("Obrigatorio selecao de dados");
			}

			else
			{
				sqlConnection1.Open();
				cmd.ExecuteNonQuery();
				sqlConnection1.Close();
				MessageBox.Show("Dados editados");
				textBox1.Text = "";

			}
		}
		catch (Exception ex)
		{
			MessageBox.Show(ex.ToString());
		}
Link to comment
Share on other sites

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 account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...

Important Information

By using this site you accept our Terms of Use and Privacy Policy. We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.