问题
I have created textbox and label dynamically, which can automatically increased if I increase the rows of the database. But now I want to insert or update values into textbox to store data in the database.
Here is my code...
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);
}
}
}
}
Here is the output.
Edit: Here is the code for creating textbox dynamically...
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++;
}
}
回答1:
As I can see, you are creating the SqlCommand object but it is not getting executed. You need to add 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
}
}
}
}
}
}
回答2:
This is not a perfect answer. But there are issues with the approach you have taken. What I understand is you cannot run a insert command in this situation, because to run a Insert Command or to add a new record to your database means you wanted to add a new Product to your database for that you need to add new controls to your form first.
Than you need to identify the newly added controls and then run a insert command for that product.
So I believe you need to update the values of the Products you already have in the Database. But then again there is a problem if you run a Update command is that you should know which ProductID or ProductName has to be updated before updating the Price of the Product.
The code of the Update Button almost looks like this
On Click of Update Button
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();
}
}
}
But the biggest problem is to find the lblvalue
corresponding to the textbox. That means if you have updated the value of Banana it should not update apple.
So I would recommend you to use DataGridView for this kind of scenario where you will have updated record in the grid. You can update it and Insert new records as well. Believe me its lot easier than what you are trying to do.
来源:https://stackoverflow.com/questions/40298194/cant-insert-or-update-values-to-dynamically-created-textbox-into-sql-server-dat