【问题标题】:class giving not reading the form input on button click类在按钮单击时不读取表单输入
【发布时间】:2015-08-23 09:52:51
【问题描述】:

我有一个类,它接受搜索输入并使用它来查询数据库表。 但是,当我按下按钮并调用类时,我看到它显示输入框中没有任何内容,并且只显示我的 messageBox 消息,上面写着无效的搜索输入(不满足任何输入)。

我怎样才能让班级在正确的时间阅读表单搜索文本框中的内容,以便它可以处理更多信息以继续其功能。

这是该类的代码:

public  static string s;
    public static updateEmployeeForm uF = new updateEmployeeForm();
    public static void selectAndDisplay()
    {

        if ((uF.textID.Text.Length==0))
        {
            MessageBox.Show("Enter valid input to search by");
        }
        else
        {
            try
            {
                if (uF.textID.Text.Length > 0)
                {
                    s = "select * FROM mydb.employees WHERE id =" + int.Parse(uF.textID.Text);//declare variable called column that will contain a table column name set to be updated.

                    uF.colName = "id";
                    uF.updatingVar = uF.textID.Text;
                    uF.query4 = "Update mydb.employees SET name ='" + uF.nameTextBoxU.Text + "', surname='" + uF.surnameTextBoxU.Text + "', dept='" + uF.deptTextBoxU.Text + "', cost_center='" + uF.costCeTextBoxU.Text + "' ,address = '" + uF.addressTextBoxU.Text + "', dob= '" + uF.dobTextBoxU.Text + "',  tel1= '" + uF.tel1TextBoxU.Text + "', tel2= '" + uF.tel2TextBoxU.Text + "' ,tel3= '" + uF.tel3TextBoxU.Text + "', email= '" + uF.emailTextBoxU.Text + "', commission= '" + uF.commTextBoxU.Text + "', total_commission= '" + uF.totalCommTextBoxU.Text + "', sick_leave= '" + uF.sickLTextBoxU.Text + "', annual_leave= '" + uF.annualLTextBoxU.Text + "',  family_leave= '" + uF.familyLTextBoxU.Text + "', other_leave= '" + uF.otherLTextBoxU.Text + "',client_care_access='" + uF.clientCareChBox.Checked + "', sale_system_access='" + uF.StaffSystChBox.Checked + "', sale_system_access ='" + uF.SalesSystChBox.Checked + "', uploads_access='" + uF.UploadsChBox.Checked + "' WHERE id= '" + uF.updatingVar + "';";
                }

                else if (uF.idNumSearchTxtBox.Text.Length > 0)
                {
                    s = "select * FROM mydb.employees WHERE id_num ='" + uF.idNumSearchTxtBox.Text + "';";
                    uF.colName = "id_num";
                    uF.updatingVar = uF.idNumSearchTxtBox.Text;
                    uF.query4 = "Update mydb.employees SET name ='" + uF.nameTextBoxU.Text + "', surname='" + uF.surnameTextBoxU.Text + "', dept='" + uF.deptTextBoxU.Text + "', cost_center='" + uF.costCeTextBoxU.Text + "' ,address = '" + uF.addressTextBoxU.Text + "', dob= '" + uF.dobTextBoxU.Text + "',  tel1= '" + uF.tel1TextBoxU.Text + "', tel2= '" + uF.tel2TextBoxU.Text + "' ,tel3= '" + uF.tel3TextBoxU.Text + "', email= '" + uF.emailTextBoxU.Text + "', commission= '" + uF.commTextBoxU.Text + "', total_commission= '" + uF.totalCommTextBoxU.Text + "', sick_leave= '" + uF.sickLTextBoxU.Text + "', annual_leave= '" + uF.annualLTextBoxU.Text + "',  family_leave= '" + uF.familyLTextBoxU.Text + "', other_leave= '" + uF.otherLTextBoxU.Text + "',client_care_access='" + uF.clientCareChBox.Checked + "', sale_system_access='" + uF.StaffSystChBox.Checked + "', sale_system_access ='" + uF.SalesSystChBox.Checked + "', uploads_access='" + uF.UploadsChBox.Checked + "' WHERE id_num= '" + uF.updatingVar + "';";
                }
                else if (uF.nameSearchTextBox.Text.Length > 0)
                {
                    s = "select * FROM mydb.employees WHERE name ='" + uF.nameSearchTextBox.Text + "';";
                    uF.colName = "name";
                    uF.updatingVar = uF.nameSearchTextBox.Text;
                    uF.query4 = "Update mydb.employees SET name ='" + uF.nameTextBoxU.Text + "', surname='" + uF.surnameTextBoxU.Text + "', dept='" + uF.deptTextBoxU.Text + "', cost_center='" + uF.costCeTextBoxU.Text + "' ,address = '" + uF.addressTextBoxU.Text + "', dob= '" + uF.dobTextBoxU.Text + "',  tel1= '" + uF.tel1TextBoxU.Text + "', tel2= '" + uF.tel2TextBoxU.Text + "' ,tel3= '" + uF.tel3TextBoxU.Text + "', email= '" + uF.emailTextBoxU.Text + "', commission= '" + uF.commTextBoxU.Text + "', total_commission= '" + uF.totalCommTextBoxU.Text + "', sick_leave= '" + uF.sickLTextBoxU.Text + "', annual_leave= '" + uF.annualLTextBoxU.Text + "',  family_leave= '" + uF.familyLTextBoxU.Text + "', other_leave= '" + uF.otherLTextBoxU.Text + "',client_care_access='" + uF.clientCareChBox.Checked + "', sale_system_access='" + uF.StaffSystChBox.Checked + "', sale_system_access ='" + uF.SalesSystChBox.Checked + "', uploads_access='" + uF.UploadsChBox.Checked + "' WHERE name= '" + uF.updatingVar + "';";
                }
                else if (uF.surnameSearchTextBox.Text.Length > 0)
                {
                    s = "select * FROM mydb.employees WHERE surname ='" + uF.surnameSearchTextBox.Text + "';";
                    uF.colName = "surname";
                   uF.updatingVar = uF.surnameSearchTextBox.Text;
                    uF.query4 = "Update mydb.employees SET name ='" + uF.nameTextBoxU.Text + "', surname='" + uF.surnameTextBoxU.Text + "', dept='" + uF.deptTextBoxU.Text + "', cost_center='" + uF.costCeTextBoxU.Text + "' ,address = '" + uF.addressTextBoxU.Text + "', dob= '" + uF.dobTextBoxU.Text + "',  tel1= '" + uF.tel1TextBoxU.Text + "', tel2= '" + uF.tel2TextBoxU.Text + "' ,tel3= '" + uF.tel3TextBoxU.Text + "', email= '" + uF.emailTextBoxU.Text + "', commission= '" + uF.commTextBoxU.Text + "', total_commission= '" + uF.totalCommTextBoxU.Text + "', sick_leave= '" + uF.sickLTextBoxU.Text + "', annual_leave= '" + uF.annualLTextBoxU.Text + "',  family_leave= '" + uF.familyLTextBoxU.Text + "', other_leave= '" + uF.otherLTextBoxU.Text + "',client_care_access='" + uF.clientCareChBox.Checked + "', sale_system_access='" + uF.StaffSystChBox.Checked + "', sale_system_access ='" + uF.SalesSystChBox.Checked + "', uploads_access='" + uF.UploadsChBox.Checked + "' WHERE surname= '" + uF.updatingVar + "';";
                }

这里的代码调用它的形式是我调用它:

    private void btnSearch_Click(object sender, EventArgs e)
    {
        updatingDatareadingClass.selectAndDisplay();
    }

【问题讨论】:

  • 不要介意 else if 子句中的其他查询,我只是使用一个用于当前测试
  • 放一个断点,看看你的uF.textId.Text的值。这应该为您指明正确的道路
  • 我确实在uF.textId.Text 设置了一个断点,即使在输入某些内容后它也始终为空“”。这是我最大的问题,为什么???

标签: c# mysql sql .net visual-studio


【解决方案1】:

您正在创建表单的新实例,而不是使用原始实例(输入值将存储在哪里)。

您可以使用原始文本框值 textID.Text 或通过以下方式确保您的 uF 表单正在显示:

uf.Show(); // This will then allow you to use `uF.textID.Text` correctly. 

此外,您应该阅读 SQL 注入,因为您的表单是可注入的,例如,用户可以输入他们的姓名为 test or 1 = 1,这将允许随后访问您的应用程序,或者更糟糕的是,DROP TABLE mydb.employees; 这将删除您的所有员工桌子。

【讨论】:

  • 我应该把 uF.Show();使其有效,例如在课堂上的位置..beginning 或其他地方@Darren Davies
  • @darrenmarela - 每当您需要将其显示给用户时。默认情况下会加载原始表单,因此您可以使用它并直接引用 textID.text(不是 uF 实例)。
  • 当我尝试从类中访问表单文本框时,我收到消息:名称___ 在当前上下文中不存在,这就是为什么我想创建一个表单实例来访问其对象跨度>
猜你喜欢
  • 2020-08-03
  • 1970-01-01
  • 2017-08-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-12
  • 1970-01-01
  • 2021-12-08
相关资源
最近更新 更多