En iyi yanıtlayıcılar
Aynı anda iki kritere göre arama yaptırma

Soru
-
Arkadaşlar selam, Projede hem ürün numarasına göre hem de renge göre arama yaptırmaya çalışıyorum. Ama yazdığım kodlara göre 1.Textboxa girilen değere göre arama yapılıyor ama 2.Textboxta veri girildiği an 1.nin arama sonuçları uçuyor. Ne yapmam gerek ?
private void textBox1_TextChanged(object sender, EventArgs e) { if (txt_urunNo.Text != "") { aramaYap(txt_urunNo.Text, "urun_id"); } else { KayitlariGetir(); } } private void txt_renk_TextChanged(object sender, EventArgs e) { if (txt_renk.Text != "") { aramaYap(txt_renk.Text, "renk"); } else { KayitlariGetir(); } } private void aramaYap(string txt, string alan) { string sorgu = "SELECT * FROM tbl_urunBilgileri,tbl_stokDurumu WHERE tbl_urunBilgileri.urun_id=tbl_stokDurumu.urun_id and tbl_urunBilgileri."; if (txt_urunNo.Text != "") { sorgu += alan + " like'%" + txt + "%'"; KayıtGuncelle(sorgu); } else if (txt_renk.Text != "") { sorgu += alan + " like '%" + txt + "%'"; KayıtGuncelle(sorgu); } } private void KayıtGuncelle(string sorgu) { SqlDataAdapter da = new SqlDataAdapter(sorgu, baglanti); DataSet ds = new DataSet(); da.Fill(ds); dataGridViewUrunler.DataSource = ds.Tables[0]; }
Yanıtlar
-
aramaYap metodu yanlış. İlk olarak sorgulama metodunu komple düzeltmeni öneririm.
Hatanın sebebini söylemek gerekirse. aşağıdaki durumu incelemen yeterli olacak. Buna göre çözümü bulabileceğine inanıyorum
private void aramaYap(string txt, string alan) {
//ikinci sorgu için txt_renk değişti string sorgu = "SELECT * FROM tbl_urunBilgileri,tbl_stokDurumu WHERE tbl_urunBilgileri.urun_id=tbl_stokDurumu.urun_id and tbl_urunBilgileri.";
//txt_urunNo boş değilse sorgun şu şekilde olacak
//"SELECT * FROM tbl_urunBilgileri,tbl_stokDurumu WHERE tbl_urunBilgileri.urun_id=tbl_stokDurumu.urun_id and tbl_urunBilgileri.renk like '%mavi%'"; if (txt_urunNo.Text != "") { sorgu += alan + " like'%" + txt + "%'"; KayıtGuncelle(sorgu); }
//else if olduğu için direk bu kısmı atlıyor çünkü alt blok txt_urunNo.Text =="" olduğu zaman çalışır.
else if (txt_renk.Text != "") { sorgu += alan + " like '%" + txt + "%'"; KayıtGuncelle(sorgu); } }
- Yanıt Olarak İşaretleyen Kyamuran SalibryamMicrosoft contingent staff, Moderator 4 Mayıs 2016 Çarşamba 08:51
-
Şu kodları kendine göre uyarlayabilirsen işine yarayabilir.
private void Kayit_Getir()
{
string idno = HttpContext.Current.User.Identity.Name;
string whereSTR;
whereSTR = "";
if (drd_sinif.SelectedValue != "Hepsi")
{
if (whereSTR != "") { whereSTR = whereSTR + " AND"; }
whereSTR = whereSTR + " tbl_urunler.sinif=" + drd_sinif.SelectedValue;
}
if (drd_tur.SelectedValue != "Hepsi")
{
if (whereSTR != "") { whereSTR = whereSTR + " AND"; }
whereSTR = whereSTR + " tbl_urunler.tur=" + drd_tur.SelectedValue;
}
if (drd_il.SelectedValue != "Hepsi")
{
if (whereSTR != "") { whereSTR = whereSTR + " AND "; }
whereSTR = whereSTR + " tbl_urunler.il='" + drd_il.SelectedItem.Text + "' ";
}
if (drd_ilce.SelectedValue != "Hepsi")
{
if (whereSTR != "") { whereSTR = whereSTR + " AND"; }
whereSTR = whereSTR + " tbl_urunler.ilce='" + drd_ilce.SelectedItem.Text + "' ";
}
if (DropDownList1.SelectedValue != "Hepsi")
{
if (whereSTR != "") { whereSTR = whereSTR + " AND "; }
whereSTR = whereSTR + " tbl_urunler.yetkili= " + Lbl_id.Text;
}
if (whereSTR != "") { whereSTR = " WHERE " + whereSTR; }
SqlConnection MyConnection;
SqlCommand MyCommand;
SqlDataAdapter MyAdapter;
DataTable MyTable;
MyConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DB"].ConnectionString);
MyCommand = new SqlCommand();
MyCommand.CommandText = "SELECT tbl_urunler.urun_id, tbl_urunler.baslik, tbl_urunler.il, tbl_urunler.ilce, tbl_urunler.fiyat, tbl_urunler.aktif, tbl_urunler.yetkili,tbl_sinif.sinif_id,tbl_sinif.sinif, tbl_tur.id,tbl_tur.tur, tbl_parabr.birim FROM tbl_urunler INNER JOIN tbl_tur ON tbl_urunler.tur = tbl_tur.id INNER JOIN tbl_parabr ON tbl_urunler.parabr = tbl_parabr.id INNER JOIN tbl_sinif ON tbl_urunler.sinif = tbl_sinif.sinif_id " + whereSTR + " ORDER BY tbl_urunler.urun_id DESC";
MyCommand.CommandType = CommandType.Text;
MyCommand.Connection = MyConnection;
MyTable = new DataTable();
MyAdapter = new SqlDataAdapter();
MyAdapter.SelectCommand = MyCommand;
grdKayitlar.DataSource = MyTable.DefaultView;
grdKayitlar.DataBind();
MyAdapter.Dispose();
MyCommand.Dispose();
MyConnection.Dispose();
}
- Yanıt Olarak İşaretleyen Kyamuran SalibryamMicrosoft contingent staff, Moderator 4 Mayıs 2016 Çarşamba 08:50
Tüm Yanıtlar
-
aramaYap metodu yanlış. İlk olarak sorgulama metodunu komple düzeltmeni öneririm.
Hatanın sebebini söylemek gerekirse. aşağıdaki durumu incelemen yeterli olacak. Buna göre çözümü bulabileceğine inanıyorum
private void aramaYap(string txt, string alan) {
//ikinci sorgu için txt_renk değişti string sorgu = "SELECT * FROM tbl_urunBilgileri,tbl_stokDurumu WHERE tbl_urunBilgileri.urun_id=tbl_stokDurumu.urun_id and tbl_urunBilgileri.";
//txt_urunNo boş değilse sorgun şu şekilde olacak
//"SELECT * FROM tbl_urunBilgileri,tbl_stokDurumu WHERE tbl_urunBilgileri.urun_id=tbl_stokDurumu.urun_id and tbl_urunBilgileri.renk like '%mavi%'"; if (txt_urunNo.Text != "") { sorgu += alan + " like'%" + txt + "%'"; KayıtGuncelle(sorgu); }
//else if olduğu için direk bu kısmı atlıyor çünkü alt blok txt_urunNo.Text =="" olduğu zaman çalışır.
else if (txt_renk.Text != "") { sorgu += alan + " like '%" + txt + "%'"; KayıtGuncelle(sorgu); } }
- Yanıt Olarak İşaretleyen Kyamuran SalibryamMicrosoft contingent staff, Moderator 4 Mayıs 2016 Çarşamba 08:51
-
Şu kodları kendine göre uyarlayabilirsen işine yarayabilir.
private void Kayit_Getir()
{
string idno = HttpContext.Current.User.Identity.Name;
string whereSTR;
whereSTR = "";
if (drd_sinif.SelectedValue != "Hepsi")
{
if (whereSTR != "") { whereSTR = whereSTR + " AND"; }
whereSTR = whereSTR + " tbl_urunler.sinif=" + drd_sinif.SelectedValue;
}
if (drd_tur.SelectedValue != "Hepsi")
{
if (whereSTR != "") { whereSTR = whereSTR + " AND"; }
whereSTR = whereSTR + " tbl_urunler.tur=" + drd_tur.SelectedValue;
}
if (drd_il.SelectedValue != "Hepsi")
{
if (whereSTR != "") { whereSTR = whereSTR + " AND "; }
whereSTR = whereSTR + " tbl_urunler.il='" + drd_il.SelectedItem.Text + "' ";
}
if (drd_ilce.SelectedValue != "Hepsi")
{
if (whereSTR != "") { whereSTR = whereSTR + " AND"; }
whereSTR = whereSTR + " tbl_urunler.ilce='" + drd_ilce.SelectedItem.Text + "' ";
}
if (DropDownList1.SelectedValue != "Hepsi")
{
if (whereSTR != "") { whereSTR = whereSTR + " AND "; }
whereSTR = whereSTR + " tbl_urunler.yetkili= " + Lbl_id.Text;
}
if (whereSTR != "") { whereSTR = " WHERE " + whereSTR; }
SqlConnection MyConnection;
SqlCommand MyCommand;
SqlDataAdapter MyAdapter;
DataTable MyTable;
MyConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["DB"].ConnectionString);
MyCommand = new SqlCommand();
MyCommand.CommandText = "SELECT tbl_urunler.urun_id, tbl_urunler.baslik, tbl_urunler.il, tbl_urunler.ilce, tbl_urunler.fiyat, tbl_urunler.aktif, tbl_urunler.yetkili,tbl_sinif.sinif_id,tbl_sinif.sinif, tbl_tur.id,tbl_tur.tur, tbl_parabr.birim FROM tbl_urunler INNER JOIN tbl_tur ON tbl_urunler.tur = tbl_tur.id INNER JOIN tbl_parabr ON tbl_urunler.parabr = tbl_parabr.id INNER JOIN tbl_sinif ON tbl_urunler.sinif = tbl_sinif.sinif_id " + whereSTR + " ORDER BY tbl_urunler.urun_id DESC";
MyCommand.CommandType = CommandType.Text;
MyCommand.Connection = MyConnection;
MyTable = new DataTable();
MyAdapter = new SqlDataAdapter();
MyAdapter.SelectCommand = MyCommand;
grdKayitlar.DataSource = MyTable.DefaultView;
grdKayitlar.DataBind();
MyAdapter.Dispose();
MyCommand.Dispose();
MyConnection.Dispose();
}
- Yanıt Olarak İşaretleyen Kyamuran SalibryamMicrosoft contingent staff, Moderator 4 Mayıs 2016 Çarşamba 08:50
-
Bu konuyu halledince şu konuları bir oku :