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 made a sql class where I will keep sql queries of my project.
And the sql class is given below:
class sql
    {
        Form2_Search_Word SW = new Form2_Search_Word();
 
        BindingManagerBase bindingManager;
        DataTable dt;
        string conStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=D:\QuranDBdetails.accdb";
 
        public void KanzESearch(string kanztext)
        {
            OleDbConnection connection = new OleDbConnection(conStr);
            string searchsql = "Select Juz_Surah,Ayat_No,Surah_Ayat,[Kanz-ul-Iman_English] from Quran WHERE  Surah_Ayat LIKE '%" + kanztext + "%';";
            connection.Open();
            OleDbDataAdapter da = new OleDbDataAdapter(searchsql, connection);
            dt = new DataTable();
            da.Fill(dt);
            connection.Close();
            SW.dataGridView1.DataSource = dt;
            //bindingManager = this.BindingContext[dt];
            string count = "select Count(*) from Quran WHERE Surah_Ayat LIKE '%" + kanztext + "%';";
            connection.Open();
            OleDbCommand command = new OleDbCommand(count, connection);
            // command.CommandText = searchsql;
            SW.textBox4.Text = command.ExecuteScalar().ToString();
            connection.Close();
        }
 
    }
 
Now I call this KanzESearch function in my Main form class but it is not working
public partial class Form2_Search_Word : Form
    {
 

private void button1_Click(object sender, EventArgs e)//search Button
        {
 
sql queries = new sql();
 

            try
            {
                if (radioButton1.Checked == true) //arabic/urdu
                {
                    if (textBox1.Text != "")
                    {
 
                    }
                    else
                    {
                        MessageBox.Show("Must Enter a Word or Phrase", "Enter Text Please!");
                    }
 

                }
                else if (radioButton2.Checked == true)//English Search
                {
                    if (textBox1.Text != "")
                    {
                        if(rbtransKanzE.Checked==true)
                        {
                            string searchtext = textBox1.Text.ToString(); 
                            queries.KanzESearch(searchtext);  //here I am calling it …
                        }
                    }
                    else
                    {
                        MessageBox.Show("Must Enter a Word or Phrase", "Enter Text Please!");
                    }
                
                }
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message,"Error!");
            }
 
}
 
When I click the my search button
it does not show any thing. Help please
and in my sql class bindingContext also not working.
The same sql class code does work when I put it in the back of button.
Posted 19-Jan-13 16:35pm
Comments
Sergey Alexandrovich Kryukov at 20-Jan-13 2:20am
   
"Not working" is not informative, and even "does not show anything" is not. Start with explaining what exactly did you mean to achieve, use the debugger. Use "Improve question" above. And ask a question.
—SA
Muhamad Faizan Khan at 20-Jan-13 8:55am
   
oh.......wroking but not showing data;-(
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 2

You are instantiating a Form2_Search_Word object inside your sql class, and a sql object inside the button1_Click method of your Form2_Search_Word class. You seem to be somewhat confused about how all the pieces are supposed to fit together here.
 
Also take a look at these lines of code:
if (textBox1.Text != "")
{
 
}
else
{
    MessageBox.Show("Must Enter a Word or Phrase", "Enter Text Please!");
}
Why not just:
if (textBox1.Text == "")
{
    MessageBox.Show("Must Enter a Word or Phrase", "Enter Text Please!");
}
  Permalink  
Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

There are a couple of things here, but we can't solve your main problem - you will have to do that yourself.
 
But first, a couple of cahnges for you:
1) You do not need to call ToString on Text fields (or any other string datatype):
                            string searchtext = textBox1.Text.ToString(); 
                            queries.KanzESearch(searchtext);  //here I am calling it …
When you do things like this you are just showing that you haven't thought about what you are doing. It does nothing at all, except to waste typing and processing time.
2) There are a couple of useful String class static methods which it is worth your looking at, Instead of:
                    if (textBox1.Text != "")
Try
                    if (!string.IsNullOrEmpty(textBox1.Text))
Or
                    if (!string.IsNullOrWhitespace(textBox1.Text))
(.NET 4.0 and above only)
3) Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.
 
When you have done this, you can look at finding the source of your problem.
Put a breakpoint on the first line of your Button Click event handler:
sql queries = new sql();
And run your program. Fill in your text box, and press the button. The debugger will stop executing when it hits the breakpoint.
Now, single step through your program and see where control goes - you can step into your KanzESearch method and look at the variables as the program executes - and at each stage work out what you expect to happen before you execute the line. Does it happen the way it should? No? Then you can look at why not, and either fix the problem yourself, or you will have much better information to come back to us with.
 
At the moment, we have no information, and without your database (which we don't have or want) we can't check your code to get information. So try it, and let us know what you find.
  Permalink  
Comments
Richard MacCutchan at 20-Jan-13 3:43am
   
Someone gave you a 1 for this, I have tried to counteract.
Muhamad Faizan Khan at 26-Jan-13 23:42pm
   
what is the difference b/w
if (textBox1.Text != "")
and
if (!string.IsNullOrEmpty(textBox1.Text))
??
OriginalGriff at 27-Jan-13 3:54am
   
In the case of a TextBox, there isn't a lot of difference - it just adds a test for a condition which should never happen (some idiot on your team set the textbox.Text property to null) - but it is worth getting into the habit of using, because it can sve you program from an unnecessary crash with "normal" strings.
 
I listed it because the much better version "IsNullOrWhiteSpace" only became available at .NET V4.0 and that is well worth using, since it effectively is:
public void MyMethod(string s)
{
if (s != null && s.Trim() != "")
{
....
 
It may be more typing than
if (s != "")
but it is a lifesaver in terms of preventing problems that really don't need to be brought to the users notice.

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



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