无法将动态创建的文本框的值插入或更新到SQL Server数据库Windows窗体C#

时间:2016-10-28 04:46:56

标签: c# sql-server ado.net

我已经动态创建了文本框和标签,如果我增加数据库的行,它会自动增加。但现在我想在文本框中插入或更新值以将数据存储在数据库中。

这是我的代码......

string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

using (SqlConnection con = new SqlConnection(cs))
{
        //string cmText = "select ProductId,ProductName,UnitPrice from tblProduct";
        string cmText = "Select Count(ProductId) from tblProduct";

        SqlCommand cmd = new SqlCommand(cmText, con);

        con.Open();
        Int32 count = (Int32)cmd.ExecuteScalar();

        cmText = "select ProductId,ProductName,UnitPrice from tblProduct";

        SqlCommand cmd1 = new SqlCommand(cmText, con);

        using (SqlDataReader rdr = cmd1.ExecuteReader())
        {
           //code for dynamically created textbox and label
        }
    }
}

private void button1_Click(object sender, EventArgs e)
{
    string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;

    using (SqlConnection con = new SqlConnection(cs))
    {
        //string cmText = "select ProductId,ProductName,UnitPrice from tblProduct";

        con.Open();

        //TextBox tx = (TextBox) Controls.Find(myTextbox[0], true)[0];
        for (int i = 0; i < this.Controls.Count; i++)
        {
            if (this.Controls[i] is TextBox)
            {
                TextBox myTextbox = (TextBox)this.Controls[i];
                string value = myTextbox.Text;

                string cmText = "insert into table tblProduct (UnitPrice) values('" + myTextbox.Text + "')";

                SqlCommand cmd = new SqlCommand(cmText, con);
            }
        }
    }
}

这是the output.

修改:以下是动态创建文本框的代码...

string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection con = new SqlConnection(cs))
{

    string cmText = "Select Count(ProductId) from tblProductInventory";
    SqlCommand cmd = new SqlCommand(cmText, con);
    con.Open();
    Int32 count = (Int32) cmd.ExecuteScalar();
    int i = 1;
    cmText = "select ProductId,ProductName,UnitPrice from tblProductInventory";
    SqlCommand cmd1 = new SqlCommand(cmText, con);
    using (SqlDataReader rdr = cmd1.ExecuteReader())
    {
        int y = 50;
        Label myLabel = new Label();
        TextBox MyTxt = New TextBox();

        while (rdr.Read())
        {
            myLabel = new Label();
            myLabel.Location = new Point(88, y);
            myLabel.Name = "txtVWReadings" + i.ToString();
            myLabel.Size = new Size(173, 20);
            myLabel.TabIndex = i;
            myLabel.Visible = true;
            myLabel.Text = rdr[1].ToString(); 
            y += 25;
            this.Controls.Add(myLabel);


            MyTxt.Text = rdr[2].ToString(); 
            i++;
        }
    }

2 个答案:

答案 0 :(得分:0)

正如我所看到的,您正在创建SqlCommand对象,但它没有被执行。您需要添加ExecuteNonQuery()。

private void button1_Click(object sender, EventArgs e)
{
    string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
    using (SqlConnection con = new SqlConnection(cs))
    {
        //string cmText = "select ProductId,ProductName,UnitPrice from tblProduct";

        con.Open();
        //TextBox tx = (TextBox) Controls.Find(myTextbox[0], true)[0];
        for (int i = 0; i < this.Controls.Count; i++)
        {
            if (this.Controls[i] is TextBox)
            {
                TextBox myTextbox = (TextBox)this.Controls[i];
                string value = myTextbox.Text;
                if (!String.IsNullOrEmpty(value)) 
                {
                string cmText = "insert into tblProduct (UnitPrice) values('" + myTextbox.Text + "')";
                SqlCommand cmd = new SqlCommand(cmText, con);
                int result = cmd.ExecuteNonQuery();
                if(result > 0)
                   {
                       //successful
                   }
                else
                 {
                    //fail
                 }
               } 
            }
        }


    }
}

答案 1 :(得分:0)

这不是一个完美的答案。但是你采取的方法存在问题。我的理解是你不能在这种情况下运行插入命令,因为运行插入命令或向数据库添加新记录意味着你想要为你的数据库添加一个新的产品,你需要为你的数据库添加新的控件首先形成。

您需要识别新添加的控件,然后为该产品运行insert命令。

因此,我认为您需要更新数据库中已有产品的值。但是,如果您运行更新命令,那么在更新产品价格之前,您应该知道哪个ProductID或ProductName必须更新。

更新按钮的代码几乎就像这样

点击更新按钮

string cs = ConfigurationManager.ConnectionStrings["DBCS"].ConnectionString;
using (SqlConnection connection = new SqlConnection(cs)) using (SqlCommand cmd = connection.CreateCommand())
{ 
    cmd.CommandText = "Update tblProduct SET UnitPrice =@paramunitprice WHERE ProductName = @paramproductname";
    con.Open();
    for (int i = 0; i < this.Controls.Count; i++)
    {
        if (this.Controls[i] is TextBox)
        {
            TextBox myTextbox = (TextBox)this.Controls[i];
            string txtvalue = myTextbox.Text;
            string lblvalue = //The Corresponding Label value to find which UnitPrice has to change.
            cmd.Parameters.AddWithValue("@paramunitprice", txtvalue);
            cmd.Parameters.AddWithValue("@paramproductname", lblvalue);

            cmd.ExecuteNonQuery();
        }
    }
}

但最大的问题是找到与文本框对应的lblvalue。这意味着如果您更新了Banana的值,则不应更新apple。

因此,我建议您使用DataGridView这种情况,您将在网格中更新记录。您也可以更新它并插入新记录。相信我比你想做的更容易。