如何将动态生成的文本框值插入数据库中 [英] How to insert the Dynamically generated textbox values in a database

查看:88
本文介绍了如何将动态生成的文本框值插入数据库中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  if (txtQuantity.Text!=  
{
try
{
for int i = 0 ; i < 数量; i ++)
{
foreach (Control ctrl in this .Controls)
{
TextBox txtBox1 = new TextBox();
txtBox1.ID = tb1 + i.ToString();
txtBox1.Text = txtBox1.Text;
PlaceHolder1.Controls.Add(txtBox1);
if (ctrl TextBox)
{

con.Open();
string qryInsertProduct = 插入Product_Details( GatePassNo,ClientID,Product_name,SerialNo,Status,CheckIN_Date,Customer_Name,Customer_location)值(' + GatePassNo + ',' + clientID + ',' + productName + < span class =code-string> ',' + txtBox1.Text + ',' + status + ', ' + TextBox1.Text + ',' + ddlCName.SelectedItem.Value + ',' + ddlCLocation.SelectedItem.Value + ');
SqlCommand comInsertProduct = new SqlCommand(qryInsertProduct,con);
comInsertProduct.ExecuteNonQuery();
}
}





这是我的代码,请帮忙。

解决方案

据我所知,您正在尝试动态创建TextBox控件,为其分配值并将其值保存到数据库中。您没有提供足够的信息来提供直接答案。



查看您提供的代码,我不明白为什么需要动态创建TextBox,尤其是当您使用相同的代码创建TextBox时将值保存到数据库中。这意味着用户无法在新创建的文本框中输入/输入任何值。



抱歉,它看起来像代码转储。在你不明白代码的作用之前,你将无法继续前进。



我建议从这里开始:

ASP.NET [ ^ ]

连接到ASP.NET中的数据库 [ ^ ]

演练:使用DetailsView Web服务器控件在网页中编辑和插入数据 [ ^ ]



另一方面,你应该阅读 SQL注入 [ ^ ]。

如何:防范ASP.NET中的SQL注入 [ ^ ]

在停止之前阻止SQL注入攻击 [ ^ ]

SQL注入以及如何避免它[ ^ ]


只需查看您的代码即可。



  for  int  i =  0 ; i < 数量; i ++)
{
foreach (控制ctrl .Controls)
{
TextBox txtBox1 = new TextBox();
txtBox1.ID = tb1 + i.ToString();
txtBox1.Text = txtBox1.Text;
PlaceHolder1.Controls.Add(txtBox1);
if (ctrl TextBox)
{

con.Open();
string qryInsertProduct = 插入Product_Details( GatePassNo,ClientID,Product_name,SerialNo,Status,CheckIN_Date,Customer_Name,Customer_location)值(' + GatePassNo + ',' + clientID + ',' + productName + < span class =code-string> ',' + txtBox1.Text + ',' + status + ', ' + TextBox1.Text + ',' + ddlCName.SelectedItem.Value + ',' + ddlCLocation.SelectedItem.Value + ');
SqlCommand comInsertProduct = new SqlCommand(qryInsertProduct,con);
comInsertProduct.ExecuteNonQuery();
}
}





在循环中你创建了文本框控件。接下来是一个insert语句。

您正在通过代码行为动态创建的文本框分配文本

 txtBox1.Text = txtBox1.Text; 





这里'txtBox1.Text'都是空白的。这就是为什么你没有得到任何文本值来插入。


if (txtQuantity.Text != "")
        {
            try
            {
                for (int i = 0; i < quantity; i++)
                {
                    foreach (Control ctrl in this.Controls)
                    {
                        TextBox txtBox1 = new TextBox();
                        txtBox1.ID = "tb1" + i.ToString();
                        txtBox1.Text = txtBox1.Text;
                        PlaceHolder1.Controls.Add(txtBox1);
                        if (ctrl is TextBox)
                        {
                            
                            con.Open();  
                            string qryInsertProduct = "insert into Product_Details(GatePassNo,ClientID,Product_name,SerialNo,Status,CheckIN_Date ,Customer_Name, Customer_location) values('" + GatePassNo + "','" + clientID + "','" + productName + "','" + txtBox1.Text + "','" + status + "','" + TextBox1.Text + "','" + ddlCName.SelectedItem.Value + "','" + ddlCLocation.SelectedItem.Value + "')";
                            SqlCommand comInsertProduct = new SqlCommand(qryInsertProduct, con);
                            comInsertProduct.ExecuteNonQuery();
                        }
                    }



This my code, please help.

解决方案

As per i understand you're trying to dynamically create TextBox control, assign a value to it and save its value into database. You did not provide enough information to provide direct answer.

Looking at code you've provided, i do not understand why do you need to create TextBox dynamically, especially when you create TextBox in the same code which is used to save value into database. It means that user can't enter/type any value into newly created textbox.

Sorry, but it looks like code-dump. You'll not be able go further, until you do not understand what your code does.

I would suggest to start here:
ASP.NET[^]
Connecting to Databases in ASP.NET[^]
Walkthrough: Editing and Inserting Data in Web Pages with the DetailsView Web Server Control[^]

On the other hand, you should read about SQL Injection[^].
How To: Protect From SQL Injection in ASP.NET[^]
Stop SQL Injection Attacks Before They Stop You[^]
SQL Injection and how to avoid it[^]


Just look at your code.

for (int i = 0; i < quantity; i++)
                {
                    foreach (Control ctrl in this.Controls)
                    {
                        TextBox txtBox1 = new TextBox();
                        txtBox1.ID = "tb1" + i.ToString();
                        txtBox1.Text = txtBox1.Text;
                        PlaceHolder1.Controls.Add(txtBox1);
                        if (ctrl is TextBox)
                        {
                            
                            con.Open();  
                            string qryInsertProduct = "insert into Product_Details(GatePassNo,ClientID,Product_name,SerialNo,Status,CheckIN_Date ,Customer_Name, Customer_location) values('" + GatePassNo + "','" + clientID + "','" + productName + "','" + txtBox1.Text + "','" + status + "','" + TextBox1.Text + "','" + ddlCName.SelectedItem.Value + "','" + ddlCLocation.SelectedItem.Value + "')";
                            SqlCommand comInsertProduct = new SqlCommand(qryInsertProduct, con);
                            comInsertProduct.ExecuteNonQuery();
                        }
                    }



Here in loop you have created text box controls.Just followed by an insert statement.
You are assigning text to dynamically created text boxes by the code line

txtBox1.Text = txtBox1.Text;



Here both 'txtBox1.Text' are blank.That's why you are not getting any text value to insert.


这篇关于如何将动态生成的文本框值插入数据库中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆