如何使用文本框更新数据 [英] how to update data using textbox

查看:120
本文介绍了如何使用文本框更新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,

我正在尝试使用基本按钮来更新数据库中的条目.我已经在某些文本框中填充了包含5列的单个数据.我现在想做的是如何使用文本框内的编辑文本来更新数据.所有的答复将不胜感激.这是我的代码:

Hi All,

I''m trying to use a basic button to update an entry in my database. I already populated a single data with 5 columns on some textboxes. What I''m trying to do now is how to update the data by using the edited text inside the textboxes. All responses will be highly appreciated. Here is my code:

protected void ButtonUpdate_Click(object sender, EventArgs e)
        {
            var connectionString = "server=127.0.0.1;uid=root;" + "pwd=xxxxxxxx;database=DBFB;";
            MySql.Data.MySqlClient.MySqlConnection conn = new MySql.Data.MySqlClient.MySqlConnection(connectionString);
            
            using (MySqlConnection con = new MySqlConnection(connectionString)) 
            using (MySqlCommand cmd = new MySqlCommand("update TBL_FB Set FirstName=@FirstName, LastName=@LastName, ContactNumber=@ContactNumber, Email=@Email, Message=@Message where ID= " + Request.QueryString["ID"], con))
            {
                cmd.Parameters.Add("@FirstName", TextBoxFirstName.Text);
                cmd.Parameters.Add("@LastName", TextBoxLastName.Text);
                cmd.Parameters.Add("@ContactNumber", TextBoxContactNumber.Text);
                cmd.Parameters.Add("@Email", TextBoxEmail.Text);
                cmd.Parameters.Add("@Message", TextBoxMessage.Text);
                
                con.Open();
                cmd.ExecuteNonQuery();
            }
            Response.Redirect("Details.aspx?ID=" + TextBoxID.Text);
        }

推荐答案

问题解决了……感谢所有回答了这个问题的人...

我只是忘了将其添加到页面上load
problem solved... thanks to everyone who replied...

i just forgot to add this on the page load
<pre lang="Javascript">

if (Page.IsPostBack)
            {
            }


为什么您不将命令字符串编写为文本字符串而不是参数化字符串?

(此字符串是外部SQL字符串,需要在适当的值周围加上单引号)

然后,如果它不起作用,那么您可以轻松地检查发送到服务器的tsql,并找出为什么它没有按照您的意愿进行操作.

口味各不相同,但是除非您在循环中输入数据,否则参数化字符串会带来很多额外的工作.
Why don''t you compose your command string is a text string instead of parametrized string?

(This string, being an outside SQL string will require single quotes around appropriate values)

Then, if it doesn''t work, you can then easily examine the tsql being sent to the server and find out why it''s not doing what you wish.

Tastes vary, but a parametrized string is a lot of extra work unless you''re entering data in a loop.


这篇关于如何使用文本框更新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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