1
I have a database and I wanted to dial n to n on C#, but I don’t know how to get the data from my other table.
I wish where it says id_equipa the data of the team selected in the combobox
Step to demonstrate the code :
public Jogadores()
        {
            InitializeComponent();
            DataTable dataTable = Database.DatabaseAPI.SelectDataTable(@"Select Jogador.Id_Jogador, Jogador.Nome_Jogador, Jogador.Idade, Jogador.País, Jogador.id_equipa from Jogador Join Equipa on Jogador.id_equipa = Equipa.Nome");
            dataGridView1.DataSource = dataTable;
            this.dataGridView1.Columns["Id_Jogador"].Visible = false;
        }
        private void button1_Click(object sender, EventArgs e)
        {
            if (textBox1.Text != "")
            {
                Database.DatabaseAPI.NonQuery("INSERT INTO Jogador(Id_Jogador, Nome_Jogador, Idade, País, Jogador.id_equipa) Values (NewID(), '" + textBox1.Text.Replace("'", "''") + "', '" + textBox2.Text.Replace("'", "''") + "', '" + textBox3.Text.Replace("'", "''") + "', '" + comboBox1.ValueMember.ToString() + "')");
                if (textBox1.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Nome em Falta");
                    return;
                }
                if (textBox2.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("Idade em Falta");
                    return;
                }
                if (textBox3.Text.Trim() == string.Empty)
                {
                    MessageBox.Show("País em Falta");
                    return;
                }
                if (comboBox1.Text.Trim() == "Escolha uma :")
                {
                    MessageBox.Show("Equipa em Falata");
                    return;
                }
                MessageBox.Show("Dados do Jogador adicionado com sucesso!");
                DataTable dataTable = Database.DatabaseAPI.SelectDataTable(@"Select Id_Jogador, Nome_Jogador, Idade, País from Jogador Join Equipa on Jogador.id_equipa = Equipa.id_equipa");
                dataGridView1.DataSource = dataTable;
            }
Table example :
Code of combobox :
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string ID = comboBox1.SelectedValue.ToString();
        }
        private void Jogadores_Load(object sender, EventArgs e)
        {
            string constr = @"server =PC-SQLEXPRESS;database = Tabelaas;UID=sa;password=1234";
            using (SqlConnection con = new SqlConnection(constr))
            {
                using (SqlDataAdapter sda = new SqlDataAdapter("Select id_equipa, Nome From Equipa", con))
                {
                    DataTable dt = new DataTable();
                    sda.Fill(dt);
                    DataRow row = dt.NewRow();
                    row[1] = "Escolha uma :";
                    dt.Rows.InsertAt(row, 0);
                    comboBox1.DataSource = dt;
                    comboBox1.DisplayMember = "Nome";
                    comboBox1.ValueMember = "id_equipa";
                }
            }
        }
						
How an SQL Injection happens
– Augusto Vasques
How are you filling in the
ComboBox? Edit your question and put the code there.– João Martins
@Joãomartins sorry the delay was without computer, but it’s already there everything.
– Jose Badim
Alter
comboBox1.ValueMember.ToString()forcomboBox1.SelectedValue.ToString().– João Martins
To avoid the SQL Injection should use, for example,
SqlParametersrather than concatenatestrings.– João Martins