代码不起作用 [英] Code is not working

查看:78
本文介绍了代码不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

请帮助我这段代码有什么问题:

Please help me what is the wrong with this code:

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();
}

推荐答案

好吧,您尚未告诉您它出了什么问题,您有什么错误或没有发生什么预期的行为,但是它可能与以下内容有关:
1)3 如果(rbtnFName.Checked).假设rbtnFName是一个RadioButton,则每次都要检查相同的RadioButton的.Checked值.如果选择了rbtnFName,则您的"SearchVal"将始终为"NName",因为所有的if语句都将显示为true,而最后要做的就是将"NName"分配给"SearchVal". > 2)您一次又一次地分配"SearchVal".当您拥有代码SearchVal = SearchVal = "NName"时,您要告诉编译器执行的操作是为每个变量赋值"NName",并在其后加上等号,在这种情况下,该变量是相同的变量. 代码的问题在于,您复制和粘贴后没有进行清理...只是一个猜测.我没有检查其余的代码...
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...



首先出发:
第二和第三个
Hi
First off:
The second and third
if (rbtnFName.Checked)

语句是多余的,可以删除,除非您希望它实际上表示某些含义.
单选按钮只有两个选项,即已选中和未选中,除非您在一个组框中将其选中,未选中,将radio2选中和未选中,将radio3选中和未选中. />
也许您想做这样的事情:?

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, ;)


这篇关于代码不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆