Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: Windows C#4.0
I have done this query work behind a button(search).
private void irfanUrdu(string searchtext)
        {
            BindingClear();
            OleDbConnection connection = new OleDbConnection(conStr);
            
           if (comboBoxSurahName.SelectedIndex >= 0 && comboBoxSurahName.SelectedIndex <= 113)
            {
                 searchsql  =  "Select Juz_Surah,Surah_Name,Ayat_No,Surah_Ayat,trans_irfanulquran_urdu FROM Quran Where trans_irfanulquran_urdu LIKE '%" + searchtext + "%' AND Juz_Surah=" + comboBoxSurahName.SelectedValue + ";";
            }
            else if (comboBoxSurahName.SelectedIndex == -1 || comboBoxSurahName.SelectedIndex == 114)
            {
                searchsql =  "Select Juz_Surah,Surah_Name,Ayat_No,Surah_Ayat,trans_irfanulquran_urdu FROM Quran Where trans_irfanulquran_urdu LIKE '%" + searchtext + "%';";
 
            }
            connection.Open();
            OleDbDataAdapter da = new OleDbDataAdapter(searchsql, connection);
            //DataTable dt = new DataTable();
            DataSet ds = new DataSet();
            da.Fill(ds, "Quran");
            connection.Close();
 
            dataGridView1.DataSource = ds;
            dataGridView1.DataMember = "Quran";
            sd.dataGridviewDetail.DataSource = ds;
            sd.dataGridviewDetail.DataMember = "Quran";
            bindingManager = this.BindingContext[ds, "Quran"];
 
            sd.label7.Text = searchtext;
            sd.textBoxAyatText.DataBindings.Add("Text", ds, "Quran.Surah_Ayat");
            sd.textBoxTranslation.DataBindings.Add("Text", ds, "Quran.trans_irfanulquran_urdu");
            sd.textBoxAyatNo.DataBindings.Add("Text", ds, "Quran.Ayat_No");
            sd.textBoxSurahNo.DataBindings.Add("Text", ds, "Quran.Juz_Surah");
            sd.textBoxSName.DataBindings.Add("Text", ds, "Quran.Surah_Name");
            string count = "select Count(*) from Quran WHERE trans_irfanulquran_urdu LIKE '%" + searchtext + "%';";
            
              if (comboBoxSurahName.SelectedIndex >= 0 && comboBoxSurahName.SelectedIndex <= 113)
            {
                count =count = "select Count(*) from Quran WHERE trans_irfanulquran_urdu LIKE '%" + searchtext + "%' AND Juz_Surah=" + comboBoxSurahName.SelectedValue + ";";
            }
            else if (comboBoxSurahName.SelectedIndex == -1 || comboBoxSurahName.SelectedIndex == 114)
            {
                count = "select Count(*) from Quran WHERE trans_irfanulquran_urdu LIKE '%" + searchtext + "%';";
 
            }
            
            connection.Open();
            OleDbCommand command = new OleDbCommand(count, connection);
            // textBoxTotalRecord.Clear();
            textBoxTotalRecord.Text = command.ExecuteScalar().ToString();
            connection.Close();
 
        }
 
with the help of dot operator (Like e.g sd.label7.Text = searchtext; I have bold that coding above) i am also showing my searched data into an other form in different textboxes and in a grid view .
is this the right way or style to show my searched data into another (different) form?
Posted 26-Jan-13 20:15pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

Wrong way. What you are doing is considered very bad practice.
 
As soon as you make controls public on a form, you lock the design of that form so you can't make any changes to it without considering the effects on the outside world. Specifically, you can't even rename controls to make them easier to work with: "Label7" must now stay "Label7" forever - you cannot change it to "labSearchText" to make the code of the destination form more self documenting.
 
You can't replace the textboxes with a grid, or property view if that would look better, because the outside world knows they exist, and can do what it likes with them.
 
The proper way (the OOPs way) is to use public properties (either individual strings with sensible names that currently set the textboxes, labels, and so forth) or implement a data transfer class that contains all of them, and set a property in the destination form to that.
That way, the display implementation remains private to the form, and the data interface does not change when the form does. (In the case of a data transfer class, it can often be reused to other purposes)
  Permalink  
Comments
Muhamad Faizan Khan at 30-Jan-13 11:23am
   
but this is just a function and i am calling it
OriginalGriff at 30-Jan-13 11:28am
   
sd.textBoxAyatText.DataBindings.Add("Text", ds, "Quran.Surah_Ayat");
The Add is a method yes, but to get to it you have to go via the TextBox.DataBindings property - which means exposing the whole textbox outside the containing class. Bad practice.
Muhamad Faizan Khan at 30-Jan-13 11:33am
   
instead?
OriginalGriff at 30-Jan-13 11:45am
   
As I said 3 days ago...:laugh:
Muhamad Faizan Khan at 30-Jan-13 22:27pm
   
;-) i will try

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 530
1 mhegazy94 460
2 Ravi Bhavnani 230
3 Kornfeld Eliyahu Peter 185
4 Shemeemsha RA 160
0 Sergey Alexandrovich Kryukov 7,205
1 OriginalGriff 6,801
2 CPallini 5,350
3 George Jonsson 3,644
4 Gihan Liyanage 2,797


Advertise | Privacy | Mobile
Web02 | 2.8.140922.1 | Last Updated 27 Jan 2013
Copyright © CodeProject, 1999-2014
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100