首页 > 解决方案 > 在另一个文本框中输入数据时,将 SQL Server 表中的数据添加到文本框中

问题描述

这是我的程序:

程序

我想要做什么:当我输入1文本框时ProductID,我想要category,namepriceforProductID = 1被填充到他们的文本框中。

我试图从产品表中读取位置ProductID = ProductIDTB.Text,然后更改其他文本框以显示其他列中的数据

这是更改 ProductID 文本框时的代码:

protected void ProductIDTB_TextChanged(object sender, EventArgs e)
{
        string connectionString1;
        SqlConnection cnn1;

        connectionString1 = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Greenwich_Butchers;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";
        cnn1 = new SqlConnection(connectionString1);

        string selectSql1 = "SELECT * FROM [Product] WHERE ProductID = ('" + Convert.ToInt32(ProductIDTB.Text) + "') ";

        SqlCommand com1 = new SqlCommand(selectSql1, cnn1);

        try
        {
            cnn1.Open();

            using (SqlDataReader read = com1.ExecuteReader())
            {
                while (read.Read())
                {
                    String productcategory = Convert.ToString(read["ProductCategory"]);
                    ProductCategoryTB.Text = productcategory;
                    String productname = Convert.ToString(read["ProductName"]);
                    ProductNameTB.Text = productname;
                    String productprice = Convert.ToString(read["ProductPrice"]);
                    ProdPriceTB.Text = productprice;
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write("error" + ex.ToString());
        }
        finally
        {
            cnn1.Close();
        }
    }

解决方法:由于 textbox_textchanged 事件不起作用,我决定添加一个使用 ID 查找产品的按钮:

protected void FindProductBtn_Click(object sender, EventArgs e)
{
        string connectionString1;
        SqlConnection cnn1;

        connectionString1 = "Data Source=(localdb)\\MSSQLLocalDB;Initial Catalog=Greenwich_Butchers;Integrated Security=True;Connect Timeout=30;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False";

        cnn1 = new SqlConnection(connectionString1);

        string selectSql1 = "SELECT * FROM [Product] WHERE ProductID = (" + Convert.ToInt32(ProductIDTB.Text) + ") ";

        SqlCommand com1 = new SqlCommand(selectSql1, cnn1);

        try
        {
            cnn1.Open();

            using (SqlDataReader read = com1.ExecuteReader())
            {
                while (read.Read())
                {
                    String productcategory = Convert.ToString(read["ProductCategory"]);
                    ProductCategoryTB.Text = productcategory;
                    String productname = Convert.ToString(read["ProductName"]);
                    ProductNameTB.Text = productname;
                    String productprice = Convert.ToString(read["ProductPrice"]);
                    ProdPriceTB.Text = productprice;
                }
            }
        }
        catch (Exception ex)
        {
            Response.Write("error" + ex.ToString());
        }
        finally
        {
            cnn1.Close();
            ProductCategoryTB.ReadOnly = true;
            ProductNameTB.ReadOnly = true;
            ProdPriceTB.ReadOnly = true;
        }
    }

标签: c#asp.netsql-servervisual-studio

解决方案


将文本框的 AutoPostBack 属性设置为 true

更多信息:https ://meeraacademy.com/textbox-autopostback-and-textchanged-event-asp-net/

<asp:TextBox ID="ProductIDTB" runat="server" AutoPostBack="True" 
OnTextChanged="ProductIDTB_TextChanged"></asp:TextBox>

顺便说一句,SqlParameter使用参数化查询。除了防止sql注入攻击外,参数化查询还可以帮助RDBMS存储和重用类似查询的执行计划,以确保更好的性能。请参阅:https ://dba.stackexchange.com/questions/123978/can-sp-executesql-be-configured-used-by-default

string selectSql1 = "SELECT * FROM [Product] WHERE ProductID = @productIdFilter";

int productIdFilter = Convert.ToInt32(ProductIDTB.Text);

SqlCommand com1 = new SqlCommand(selectSql1, cnn1);
com1.Parameters.AddWithValue("productIdFilter", productIdFilter);

推荐阅读