我不了解如何在放置在窗体面板上的文本框中捕获值。我正在尝试使用这些值更新数据库表。
谁能指出我正确的方向?
代码:
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
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句