C# 在文本框中显示建议
我在销售发票中处理销售发票我在相关字段中自动填写产品数据,例如当用户在产品代码文本框中输入产品代码时,产品名称和产品价格文本框通过从数据库检索数据自动填写,我希望当用户在这里启动类型代码时,程序给出关于数据库中所有产品的建议。例如,当用户输入1时,程序会给出有关产品代码的建议,以1开头的产品代码会自动显示,用户只需选择他想要的产品代码 我在product code文本框的文本更改事件中执行的代码是C# 在文本框中显示建议,c#,winforms,sql-server-2008,textbox,autosuggest,C#,Winforms,Sql Server 2008,Textbox,Autosuggest,我在销售发票中处理销售发票我在相关字段中自动填写产品数据,例如当用户在产品代码文本框中输入产品代码时,产品名称和产品价格文本框通过从数据库检索数据自动填写,我希望当用户在这里启动类型代码时,程序给出关于数据库中所有产品的建议。例如,当用户输入1时,程序会给出有关产品代码的建议,以1开头的产品代码会自动显示,用户只需选择他想要的产品代码 我在product code文本框的文本更改事件中执行的代码是 private void textBox2_TextChanged(object sender,
private void textBox2_TextChanged(object sender, EventArgs e)
{
if (txtProductCode1.Text == "")
{
txtProductName1.Text = "";
txtQty.Text = "";
txtSalePrice.Text = "";
txtTotal.Text = "";
}
string sql = "select productprice, ProductName";
sql += " from dbo.productlog";
sql += " where productCode = '" + txtProductCode1.Text + "'"; // Placing ProductCode in single quotes because it's not an int column, but a varchar column, in SQL server
SqlConnection cn = new SqlConnection();
SqlCommand rs = new SqlCommand();
SqlDataReader sdr = null;
clsConnection clsCon = new clsConnection();
clsCon.fnc_ConnectToDB(ref cn);
rs.Connection = cn;
rs.CommandText = sql;
sdr = rs.ExecuteReader();
if (sdr.Read())
{
txtProductName1.Text = sdr["ProductName"].ToString();
txtSalePrice.Text = sdr["ProductPrice"].ToString();
}
else if (txtProductName.Text == "")
{
goto exitPoint;
}
else if (!sdr.Read())
{
MessageBox.Show("Data not found", Application.ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
txtProductName.Focus();
}
exitPoint:
sdr.Close();
rs = null;
cn.Close();
}
如何在文本框中显示有关产品代码的建议
编辑:
它不是windform应用程序,意味着它是基于桌面的应用程序,我正在使用VS2010在C#net中创建它。检查此项,希望它能为您工作
web应用程序/windows窗体应用程序?windows窗体应用程序请不要使用
goto
在检查空文本的if中添加返回。这允许删除goto并避免对数据库的无用调用。虽然提供的链接可能包含正确的答案,但不支持仅链接的答案。链接有死的坏习惯,如果这种情况发生,你的答案是无用的。尝试在此处添加该链接中的一些信息