Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何从放置在窗体上的面板中捕获值_C#_Mysql_Winforms - Fatal编程技术网

C# 如何从放置在窗体上的面板中捕获值

C# 如何从放置在窗体上的面板中捕获值,c#,mysql,winforms,C#,Mysql,Winforms,我不理解如何在表单面板中的文本框中捕获值。我正在尝试用这些值更新我的数据库表 谁能给我指出正确的方向吗 代码: private void btnSupplier_Click(object sender, EventArgs e) { try { Panel pnlAddSupplier = new Panel(); TextBox txtSupplierID = new TextBox();

我不理解如何在表单面板中的文本框中捕获值。我正在尝试用这些值更新我的数据库表

谁能给我指出正确的方向吗

代码:

private void btnSupplier_Click(object sender, EventArgs e)
    {
        try
        {
            Panel pnlAddSupplier = new Panel();
            TextBox txtSupplierID = new TextBox();
            TextBox txtSupplierName = new TextBox();
            Button btnAddSupplier = new Button();
            Label lblSupplierID = new Label();
            Label lblSupplierName = new Label();

            // Initialize the Panel control.
            pnlAddSupplier.Location = new Point(56, 74);
            pnlAddSupplier.Size = new Size(200, 200);
            // Set the Borderstyle for the Panel to three-dimensional.
            pnlAddSupplier.BorderStyle = System.Windows.Forms.BorderStyle.Fixed3D;

            // Initialize the Label and TextBox controls.
            lblSupplierID.Location = new Point(60, 0);
            lblSupplierID.Text = "Supplier ID";
            lblSupplierID.Size = new Size(104, 20);
            txtSupplierID.Location = new Point(20, 40);
            txtSupplierID.Text = "";
            txtSupplierID.Size = new Size(152, 20);

            lblSupplierName.Location = new Point(20, 70);
            lblSupplierName.Text = "Supplier Name";
            lblSupplierName.Size = new Size(150, 20);
            txtSupplierName.Location = new Point(20, 90);
            txtSupplierName.Text = "";
            txtSupplierName.Size = new Size(152, 20);

            lblSupplierID.Location = new Point(16, 16);
            lblSupplierID.Text = "Supplier ID";


            btnAddSupplier.Location = new Point(60, 120);
            btnAddSupplier.Text = "Add";
            btnAddSupplier.Click += btnAddSupplier_Click;
            // Add the Panel control to the form. 
            this.Controls.Add(pnlAddSupplier);
            // Add the Label and TextBox controls to the Panel.
            pnlAddSupplier.Controls.Add(lblSupplierID);
            pnlAddSupplier.Controls.Add(txtSupplierID);
            pnlAddSupplier.Controls.Add(lblSupplierName);
            pnlAddSupplier.Controls.Add(txtSupplierName);
            pnlAddSupplier.Controls.Add(btnAddSupplier);

        }
        catch { }
    }

 public void btnAdd_Click(object sender, EventArgs e)
        {
            string username= //how to assign textbox value?;
            int age = //how to assign textbox value;

            MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();

            string query = "INSERT INTO table(username, age) VALUES ('@xxx', '@xxx') "; 

             //please not these values are for demonstration.

            using (connection)
            {
                  ObjDb.OpenConnection();
                ObjDb.ExecuteNonQuery(query, connection);
            }
        }
正如您所看到的,这段代码对SQL注入开放,我想使用参数。为此,我需要捕获输入的详细信息。如何做到这一点

我已尝试分配
supplierID=txtSupplierID.Text
,但收到一条消息说
txtSupplier.Text
不存在


谢谢。

我不知道哪个文本框适用于这个年龄段,但我希望这能为您提供一个良好的起点

string username = txtSupplierID.Text; // how to assign textbox value?;
int age = Int32.Parse(...); // how to assign textbox value;
你需要更多的年龄验证。因此,最好使用:

int age;
if (!Int32.TryParse(..., out age))
{
    MessageBox.Show("Incorrect age!");
    return;
}
将txtSupplierID更改为字段:

private TextBox txtSupplierName;
并更新这些行:

txtSupplierName = new TextBox(); // remove TextBox

您正在将供应商ID文本框设置为btnSupplier\u Click函数的局部变量。您需要将其设置为类变量。然后,如果使用设计器将该变量拖放到网页上,则该变量应出现在*.designer.cs文件中

以下是如何使用参数来防止SQL注入:

            DbCommand myCommand = Connection.CreateCommand();
            myCommand.CommandText = @"select * from myTable mt
                where 
                mt.paramter = @myParameter";

            DbParameter myParameter = myCommand.CreateParameter();
            myParameter.ParameterName = "@myParameter";
            myCommand.Parameters.Add(myParameter);    

首先,您需要命名控件:

 txtSupplierID.Name = "txtSupplierID";
然后您可以得到如下值:

Control[] Found = this.Controls.Find("txtSupplierID", true);

TextBox IDTextBox = Found[0] as TextBox;

string SupplierID = IDTextBox.Text;

别忘了添加错误检查

这就是你的意思吗?假设用户总是在txtSupplierID文本框中输入整数值,这应该可以正常工作。将Name属性指定给控件并按该名称获取它。请随便问任何我没有解释的问题。干杯

private void btnSupplier_Click(object sender, EventArgs e)
{
    Panel pnlAddSupplier = new Panel()
    {
        Name = "pnlAddSupplier",
        Location = new Point(56, 74),
        Size = new Size(200, 200),
        BorderStyle = BorderStyle.Fixed3D
    };
    TextBox txtSupplierID = new TextBox()
    {
        Name = "txtSupplierID",
        Location = new Point(20, 40),
        Text = string.Empty,
        Size = new Size(152, 20)
    };
    TextBox txtSupplierName = new TextBox()
    {
        Name = "txtSupplierName",
        Location = new Point(20, 90),
        Text = string.Empty,
        Size = new Size(152, 20)
    };
    Label lblSupplierID = new Label()
    {
        Name = "lblSupplierID",
        Location = new Point(20, 20),
        Text = "Supplier ID:",
        Size = new Size(104, 20)
    };
    Label lblSupplierName = new Label()
    {
        Name = "lblSupplierName",
        Location = new Point(20, 70),
        Text = "Supplier Name:",
        Size = new Size(150, 20)
    };
    Button btnAddSupplier = new Button()
    {
        Name = "btnAddSupplier",
        Location = new Point(60, 120),
        Text = "Add",
    };
    btnAddSupplier.Click += btnAdd_Click;

    // Add controls to the Panel.
    pnlAddSupplier.Controls.Add(lblSupplierID);
    pnlAddSupplier.Controls.Add(txtSupplierID);
    pnlAddSupplier.Controls.Add(lblSupplierName);
    pnlAddSupplier.Controls.Add(txtSupplierName);
    pnlAddSupplier.Controls.Add(btnAddSupplier);

    // Add the Panel control to the form. 
    this.Controls.Add(pnlAddSupplier);
}

public void btnAdd_Click(object sender, EventArgs e)
{
    string username = this.Controls["pnlAddSupplier"].Controls["txtSupplierName"].Text;
    int age = Int32.Parse(this.Controls["pnlAddSupplier"].Controls["txtSupplierID"].Text);

    MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand();

    string query = "INSERT INTO table(username, age) VALUES ('@xxx', '@xxx') ";

    using (connection)
    {
        ObjDb.OpenConnection();
        ObjDb.ExecuteNonQuery(query, connection);
    }
}

我已经试过了,我似乎无法访问这个文本框。我得到的“txtSupplierID.Text”不存在。另外,这些值只是为了演示,我并没有实际存储年龄值。但是很好地指出了验证。在这种情况下,将
txtSupplierID
设置为一个字段,而不是一个局部变量。在创建面板之前,我已经声明了这一点。TextBox txtSupplierID=new TextBox();如果您更早声明它,它仍然是一个局部变量。当您的
btnSupplier\u单击
结束时,局部变量超出范围。所以,你应该把它变成一个领域。我明白了,所以如果我真的把它变成一个领域。这是否会影响txtSupplierID.Location=新点(20,40)?我确信将抛出空引用错误。
Control ctrl=Controls[“txtSupplierID”]
也可用于定位控件。我在字符串username=this上得到一个空引用异常。控件[“pnlAddSupplier”]。控件[“txtSupplierName”]。文本@哈里,我刚试过,效果很好。您必须分配控件的Name属性。txtSupplierName.Name=“txtSupplierName”;“你确定是你干的吗?”哈利,我很高兴这有帮助;)