Home > Enterprise >  C# SQL Server - getting error creating subcategory
C# SQL Server - getting error creating subcategory

Time:04-09

When I select the category from the comboBox, type it into the textbox and add it, there are gaps in the database. What is the reason for this? can anyone help? The photo of the database and the C# codes are below.

try
{
    vt.baglanti();
    komut = new SqlCommand("insert into Kategoriler(kategori, kategori_id) values(@p1, @p2)", vt.baglanti());

    komut.Parameters.AddWithValue("@p1", tbAltKategori.Text);
    komut.Parameters.AddWithValue("@p2", comboBox1.SelectedValue);

    komut.ExecuteNonQuery();

    MessageBox.Show("Alt Kategori Oluşturulmuştur");
    vt.baglanti().Close();
    tbAltKategori.Clear();

    dialog = MessageBox.Show(tbAltKategori.Text   "'a Ait Yeni Bir Alt Kategori Oluşturacak mısınız?", "Uyarı", MessageBoxButtons.YesNo, MessageBoxIcon.Information);

    if (dialog == DialogResult.Yes)
    {
        comboboxVeri.comboboxListelemeIdAlmaSartla("id", "kategori", "Kategoriler", "kategori_id ="   comboBox1.SelectedValue, comboBox1);
                
        komut = new SqlCommand("insert into Kategoriler(kategori, kategori_id) values(@p1, @p2)", vt.baglanti());

        komut.Parameters.AddWithValue("@p1", tbAltKategori.Text);
        komut.Parameters.AddWithValue("@p2", comboBox1.SelectedValue);

        komut.ExecuteNonQuery();
    }

    vt.baglanti().Close();
}

This is a method I wrote. I don't think there will be a mistake in this, but I'm still leaving the codes below.

comboboxVeri.comboboxListelemeIdAlmaSartla("id", "kategori", "Kategoriler", "kategori_id ="   comboBox1.SelectedValue, comboBox1)

public void comboboxListelemeIdAlmaSartla(string id, string alanAdi, string tabloAdi, string sart, ComboBox cb)
    {
        komut = new SqlCommand("select "   id   ", "   alanAdi   " from "   tabloAdi   " where "   sart, baglanti());
        da = new SqlDataAdapter(komut);
        dt = new DataTable();
        da.Fill(dt);
        cb.DisplayMember = alanAdi;
        cb.ValueMember = "id";
        cb.DataSource = dt;
    }

database image this is the image of the database.

CodePudding user response:

you're clearing the textbox before the second dialogbox

tbAltKategori.Clear();

and later, if the user answers yes

komut.Parameters.AddWithValue("@p1", tbAltKategori.Text);
  • Related