Click here to Skip to main content
15,891,372 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
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

1 solution

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)
 
Share this answer
 
Comments
Muhamad Faizan Khan 30-Jan-13 11:23am    
but this is just a function and i am calling it
OriginalGriff 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 30-Jan-13 11:33am    
instead?
OriginalGriff 30-Jan-13 11:45am    
As I said 3 days ago...:laugh:
Muhamad Faizan Khan 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)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900