Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
3.50/5 (2 votes)
See more:
Please help me what is the wrong with this code:
C#
private void btnSearch_Click(object sender, EventArgs e)
        {
if (rbtnFName.Checked)
                SearchVal = "FName";
            if (rbtnFName.Checked)
                SearchVal = SearchVal = "LName";
            if (rbtnFName.Checked)
                SearchVal = SearchVal = "NName";

           string my_Param = txtSearch.Text;
            MySql.Data.MySqlClient.MySqlParameter myParam = new MySql.Data.MySqlClient.MySqlParameter();
             myParam.ParameterName = "@my_Param";
             myParam.Value = my_Param;
             string query = "SELECT * FROM bookdetails WHERE '" + SearchVal + "' LIKE '%" + @my_Param + "%'";
             MySql.Data.MySqlClient.MySqlCommand myCommand = new MySql.Data.MySqlClient.MySqlCommand(query, connection);
      
            myCommand.Parameters.Add(myParam);
            MySql.Data.MySqlClient.MySqlDataReader myDataReader;

            myDataReader = myCommand.ExecuteReader();
            myDataReader.Read();

            MessageBox.Show("'" + SearchVal + "' = '" + txtSearch.Text + "'");
            if (myDataReader.HasRows){
                      // Matches are found, format and display 
                       while(myDataReader.Read()){
                              MessageBox.Show("Maches were found, try again.");
                              string strFName = myDataReader.GetString("FName").ToString();                     
                               
                       }
                   }
               // Nothing was found based on Search Criteria, Tell the System 
              else
                   {
                       MessageBox.Show("No matches were found, try again.");
                   }
            myDataReader.Close();
}
Posted
Updated 4-Dec-10 20:17pm
v2

Well, you haven't told what is going wrong with it or what errors you have or what expected behavior is not occurring, but it probably has something to do with:
1) the 3 if (rbtnFName.Checked). Assuming that rbtnFName is a RadioButton, you are checking the same RadioButton's .Checked value each time. If the rbtnFName is selected, your 'SearchVal' will always be "NName" because all the if statements will show true and the last thing that is done is to assign "NName" to the 'SearchVal'.
2) you are assigning 'SearchVal' over and over again. When you have the code SearchVal = SearchVal = "NName" what you are telling the compiler to do is assign the value "NName" to each variable with an equal sign after it, which in this case is the same variable.
The problem with your code is that you copied and pasted without cleaning up afterwards...just a guess. I didn't check the rest of your code...
 
Share this answer
 
Comments
Roger Wright 5-Dec-10 0:46am    
I think you've nailed it; that is the weirdest if construct I've ever seen. If the button FName is selected, SearchVal will be NName, otherwise it will be either undefined or an empty string. The message boxes won't be much better, as the OP misspelled 'Match' in the first one, and displays essentially the same message in both - "try again." Strange...
Isuru_senanayake 15-Dec-10 2:16am    
Thank you!!!
Hi
First off:
The second and third
if (rbtnFName.Checked)
statement is redundant and can be removed, unless you wanted it to actually mean something.
A radio button has only two options, checked and un-checked, unless you have it in a group box where you would have a radio1 checked and un-checked, radio2 checked and un-checked, radio3 checked and un-checked.

Maybe you wanted to do something like this:?

if (rbtnFName1.Checked)
{                
  SearchVal = "FName";
}            
if (rbtnLName.Checked)
{                
  SearchVal = "LName";
}            
if (rbtnNName.Checked)
{                
  SearchVal = "NName";
}


That is as far as I got, I have to get back to work now,
Regard, ;)
 
Share this answer
 
v2
Comments
Isuru_senanayake 15-Dec-10 2:16am    
Thank you!

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

  Print Answers RSS


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