C# Combobox'a SQL'den Veri Çekme

Kroxxy

Hectopat
Katılım
31 Ocak 2019
Mesajlar
74
Daha fazla  
Cinsiyet
Erkek
Merhaba. SQL'den combobox a veri çekme sorunu yaşıyorum. Kategori comboboxuna gelecek veri markaya gidiyor ve kategoride bir şey listelenmiyor. Formun kodları ve ekran görüntüleri bu şekilde;

Using System;
Using System. Collections. Generic;
Using System.componentModel;
Using System. Data;
Using System. Data. SqlClient;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Threading. Tasks;
Using System. Windows. Forms;

Namespace StokTakip
{
Public partial class frmÜrünListele: Form
{
Public frmÜrünListele()
{
InitializeComponent();
}
SqlConnection baglanti = New SqlConnection("Data Source = DESKTOP-C5B2TEJ; Initial Catalog = Stok_Takip; Integrated Security = True");
DataSet daset = New DataSet();
Private Void kategorigetir()
{
baglanti.Open();
SqlCommand komut = New SqlCommand("select * from kategoribilgileri", baglanti);
SqlDataReader read = komut.ExecuteReader();
While (read.Read())
{
comboMarka.Items.Add(read["kategori"].ToString());
}
baglanti.Close();
}
Private Void frmÜrünListele_Load(object sender, EventArgs e)
{
ÜrünLİstele();
kategorigetir();

}

Private Void ÜrünLİstele()
{
baglanti.Open();
SqlDataAdapter adtr = New SqlDataAdapter("select *from ürün", baglanti);
Adtr. Fill(daset, "ürün");
DataGridView1.DataSource = daset. Tables["urun"];
baglanti.Close();
}

Private Void dataGridView1_CellDoubleClick(object sender, DataGridViewCellEventArgs e)
{
BarkodNotxt. Text = dataGridView1.CurrentRow.Cells["barkodno"].Value.ToString();
Kategoritxt. Text = dataGridView1.CurrentRow.Cells["kategori"].Value.ToString();
Markatxt. Text = dataGridView1.CurrentRow.Cells["marka"].Value.ToString();
ÜrünAdıtxt. Text = dataGridView1.CurrentRow.Cells["urunadi"].Value.ToString();
Miktarıtxt. Text = dataGridView1.CurrentRow.Cells["miktari"].Value.ToString();
AlışFiyatıtxt. Text = dataGridView1.CurrentRow.Cells["alisfiyati"].Value.ToString();
SatışFiyatıtxt. Text = dataGridView1.CurrentRow.Cells["satisfiyati"].Value.ToString();
}

Private Void btnGüncelle_Click(object sender, EventArgs e)
{
baglanti.Open();
SqlCommand komut = New SqlCommand("Update ürün set urunadi=@urunadi, miktari=@miktari, alisfiyati=@alisfiyati, satisfiyati=@satisfiyati where barkodno=@barkodno", baglanti);
Komut. Parameters. AddWithValue("@barkodno", BarkodNotxt. Text);
Komut. Parameters. AddWithValue("@urunadi", ÜrünAdıtxt. Text);
Komut. Parameters. AddWithValue("@miktari", int. Parse(Miktarıtxt. Text));
Komut. Parameters. AddWithValue("@alisfiyati", double. Parse(AlışFiyatıtxt. Text));
Komut. Parameters. AddWithValue("@satisfiyati", double. Parse(SatışFiyatıtxt. Text));
komut.ExecuteNonQuery();
baglanti.Close();
daset.Tables["urun"].Clear();
ÜrünLİstele();
MessageBox. Show("Güncelleme Yapıldı");
Foreach (Control item in this. Controls)
{
İf (item is TextBox)
{
İtem. Text = "";
}

}

}

Private Void btnMarkaGuncelle_Click(object sender, EventArgs e)
{
İf (BarkodNotxt. Text!="")
{
baglanti.Open();
SqlCommand komut = New SqlCommand("Update ürün set kategori=@kategori, marka=@marka where barkodno=@barkodno", baglanti);
Komut. Parameters. AddWithValue("@barkodno", BarkodNotxt. Text);
Komut. Parameters. AddWithValue("@kategori", comboKategori. Text);
Komut. Parameters. AddWithValue("@Marka", comboMarka. Text);

komut.ExecuteNonQuery();
baglanti.Close();
MessageBox. Show("Güncelleme Yapıldı");
daset.Tables["urun"].Clear();
ÜrünLİstele();
}
Else
{
MessageBox. Show("Barkodno yazılı değil");
}

Foreach (Control item in this. Controls)
{
İf (item is ComboBox)
{
İtem. Text = "";
}
}
}

Private Void comboKategori_SelectedIndexChanged(object sender, EventArgs e)
{
comboMarka.Items.Clear();
ComboMarka. Text = "";

baglanti.Open();
SqlCommand komut = New SqlCommand("SELECT * FROM markabilgileri WHERE kategori='" + comboKategori. SelectedItem + "'", baglanti);

SqlDataReader read = komut.ExecuteReader();
While (read.Read())
{
comboMarka.Items.Add(read["marka"].ToString());
}
baglanti.Close();
}

Private Void comboMarka_SelectedIndexChanged(object sender, EventArgs e)
{

}
}
}
 

Dosya Ekleri

  • kaetgori.png
    kaetgori.png
    138,3 KB · Görüntüleme: 35
  • marka.png
    marka.png
    140,2 KB · Görüntüleme: 28
Merhaba. SQL'den combobox a veri çekme sorunu yaşıyorum. Kategori comboboxuna gelecek veri markaya gidiyor ve kategoride bir şey listelenmiyor. Formun kodları ve ekran görüntüleri bu şekilde;

Using System;
Using System. Collections. Generic;
Using System.componentmodel;
Using System. Data;
Using System. Data. Sqlclient;
Using System. Drawing;
Using System. Linq;
Using System. Text;
Using System. Threading. Tasks;
Using System. Windows. Forms;

Namespace stoktakip.
{
Public partial class frmürünlistele: Form.
{
Public frmÜrünListele()
{
InitializeComponent();
}
Sqlconnection baglanti = New sqlconnection("data Source = desktop-C5b2tej; ınitial catalog = stok_takip; ıntegrated Security = true");
Dataset daset = New DataSet();
Private Void kategorigetir()
{
baglanti.Open();
Sqlcommand komut = New sqlcommand("select * from kategoribilgileri", baglanti);
Sqldatareader read = komut.ExecuteReader();
While (read.Read())
{
comboMarka.Items.Add(read["kategori"].ToString());
}
baglanti.Close();
}
Private Void frmürünlistele_load(object sender, eventargs e)
{
ÜrünLİstele();
kategorigetir();

}

Private Void ÜrünLİstele()
{
baglanti.Open();
Sqldataadapter adtr = New sqldataadapter("select *from ürün", baglanti);
Adtr. Fill(daset, "ürün");
Datagridview1.datasource = daset. Tables["urun"];
baglanti.Close();
}

Private Void datagridview1_celldoubleclick(object sender, datagridviewcelleventargs e)
{
Barkodnotxt. Text = dataGridView1.CurrentRow.Cells["barkodno"].Value.ToString();
Kategoritxt. Text = dataGridView1.CurrentRow.Cells["kategori"].Value.ToString();
Markatxt. Text = dataGridView1.CurrentRow.Cells["marka"].Value.ToString();
Ürünadıtxt. Text = dataGridView1.CurrentRow.Cells["urunadi"].Value.ToString();
Miktarıtxt. Text = dataGridView1.CurrentRow.Cells["miktari"].Value.ToString();
Alışfiyatıtxt. Text = dataGridView1.CurrentRow.Cells["alisfiyati"].Value.ToString();
Satışfiyatıtxt. Text = dataGridView1.CurrentRow.Cells["satisfiyati"].Value.ToString();
}

Private Void btngüncelle_click(object sender, eventargs e)
{
baglanti.Open();
Sqlcommand komut = New sqlcommand("Update ürün set urunadi=@urunadi, miktari=@miktari, alisfiyati=@alisfiyati, satisfiyati=@satisfiyati where barkodno=@barkodno", baglanti);
Komut. Parameters. Addwithvalue("@barkodno", barkodnotxt. Text);
Komut. Parameters. Addwithvalue("@urunadi", ürünadıtxt. Text);
Komut. Parameters. Addwithvalue("@miktari", int. Parse(miktarıtxt. Text));
Komut. Parameters. Addwithvalue("@alisfiyati", double. Parse(alışfiyatıtxt. Text));
Komut. Parameters. Addwithvalue("@satisfiyati", double. Parse(satışfiyatıtxt. Text));
komut.ExecuteNonQuery();
baglanti.Close();
daset.Tables["urun"].Clear();
ÜrünLİstele();
Messagebox. Show("güncelleme yapıldı");
Foreach (Control item in this. Controls)
{
İf (item is textbox)
{
İtem. Text = "";
}

}

}

Private Void btnmarkaguncelle_click(object sender, eventargs e)
{
İf (barkodnotxt. Text!="")
{
baglanti.Open();
Sqlcommand komut = New sqlcommand("Update ürün set kategori=@kategori, marka=@marka where barkodno=@barkodno", baglanti);
Komut. Parameters. Addwithvalue("@barkodno", barkodnotxt. Text);
Komut. Parameters. Addwithvalue("@kategori", combokategori. Text);
Komut. Parameters. Addwithvalue("@Marka", combomarka. Text);

komut.ExecuteNonQuery();
baglanti.Close();
Messagebox. Show("güncelleme yapıldı");
daset.Tables["urun"].Clear();
ÜrünLİstele();
}
Else.
{
Messagebox. Show("barkodno yazılı değil");
}

Foreach (Control item in this. Controls)
{
İf (item is combobox)
{
İtem. Text = "";
}
}
}

Private Void combokategori_selectedındexchanged(object sender, eventargs e)
{
comboMarka.Items.Clear();
Combomarka. Text = "";

baglanti.Open();
Sqlcommand komut = New sqlcommand("select * from markabilgileri where kategori='" + combokategori. Selectedıtem + "'", baglanti);

Sqldatareader read = komut.ExecuteReader();
While (read.Read())
{
comboMarka.Items.Add(read["marka"].ToString());
}
baglanti.Close();
}

Private Void combomarka_selectedındexchanged(object sender, eventargs e)
{

}
}
}

Müşteriye girip SS atar mısın?

Müşteriye girip SS atar mısın?

Veya SQL dorkunu al SQL injectiondan çek bilgileri o da olur.
 
Öncelikle lütfen kod paylaşırken daha okunaklı olması için kod bölümünden paylaşın.
1672916776734.png


Olaya gelirsek burada yapılabilecek en iyi şey alt kategori oluşturmak olur.
 
Müşteriye girip SS atar mısın?



Veya SQL dorkunu al SQL injectiondan çek bilgileri o da olur.
Reis SQL'e hakim değilim. Müşteri SS i atayım ama niye istedin bilemedim açıkçası.

@MisakiTaro @Crypto Guard @ayrx şöyle bir durum var ben youtube da birini izleyip onunla beraber yürütmeye çalışıyorum. Adamda sorunsuz bende sıkıntı var kodlarımız birebir aynı.
 
Örnek videoyu atar mısın?

Bu içeriği görüntülemek için üçüncü taraf çerezlerini yerleştirmek için izninize ihtiyacımız olacak.
Daha detaylı bilgi için, çerezler sayfamıza bakınız.

Sonlara doğru test ediyor benim bahsettiğim boxları silme işleminden önce.
 
Son düzenleme:
Bu içeriği görüntülemek için üçüncü taraf çerezlerini yerleştirmek için izninize ihtiyacımız olacak.
Daha detaylı bilgi için, çerezler sayfamıza bakınız.

Sonlara doğru test ediyor benim bahsettiğim boxları silme işleminden önce.
Şuan evde değilim. Bir kaç saate düzenleyip atarım kodu.

Şu an evde değilim. Birkaç saate düzenleyip atarım kodu.

Kusura bakma şu anda hastanedeyim. Aklımda kod eve gittiğimde yapmaya çalışırım.

İki adet tablo oluşturdum "UstVeri" ve "AltVeri" adında.
"AltVeri" tablosunda ki "UstVeri_ID" önemli bu sayede oraya göre veri alacağız.

1673012695878.png
1673012709681.png


SQLite kullandım bunu SqlClient'e göre ayarlarsın.

Bu kodlar Bir çok yerde kullanma dorumunda tekrar tekrar yazmak istemediğim için.

C#:
private SQLiteCommand command;
private SQLiteDataReader reader;

Listele adında bir void yaptım ki iki kere aynı işlemi yapmayayım.

C#:
private void listele(string sqlCommand, System.Windows.Forms.ComboBox comboBoxName, String valueName)
        {
            try
            {
                using (SQLiteConnection connection = new SQLiteConnection("Data source =.\\TestDB.db; Versiyon = 3"))
                {
                    if (connection.State == ConnectionState.Closed)
                    {
                        connection.Open();
                    }

                    using (command = new SQLiteCommand(sqlCommand, connection))
                    {
                        using (reader = command.ExecuteReader())
                        {
                            while (reader.Read())
                            {
                                comboBoxName.Items.Add(reader[valueName].ToString());
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
        }

Form açıldığında "UstVeri" tablosunu "comboBox1" içine "UstVeri_Name" bilgisini listeliyorum.

C#:
private void Form1_Load(object sender, EventArgs e)
        {
            listele("SELECT* FROM UstVeri ORDER BY UstVeri_ID ASC", comboBox1, "UstVeri_Name");
        }

ComboBox1'de seçilene göre listeliyorsun burada.

C#:
private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            comboBox2.Items.Clear();
            listele("SELECT * FROM AltVeri where UstVeri_ID=" + (comboBox1.SelectedIndex + 1) + " ORDER BY AltVeri_ID ASC", comboBox2, "AltVeri_Name");
        }
 
Son düzenleme:

Geri
Yukarı