如何从数据库中检索文本,编辑它并将其重新发送到数据库 [英] How to retrieve text from database, edit it and resend it to Database

查看:70
本文介绍了如何从数据库中检索文本,编辑它并将其重新发送到数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在数据库中存储了一些文本,我能够检索它并在文本框中显示它,但是如果我想要更新内容它不会存储任何添加只是原始文本。

这是我的代码:



so i have stored some text in the database, and i was able to retrieve it and display it in a textbox however if i want to update the content it wont store any addition just the original text.
Here is my code:

protected void Page_Load(object sender, EventArgs e)
    {
        if (Session["Admin"] == null)
        {
            Response.Redirect("~/Admin/Login.aspx");


    }


            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
            con.Open();


            SqlCommand com = new SqlCommand("SELECT * from AboutUsData", con);
            SqlDataAdapter da = new SqlDataAdapter(com);
            DataSet ds = new DataSet();
            da.Fill(ds, "AboutUsData");



            txtAbout.Text = ds.Tables["AboutUsData"].Rows[0]["About"].ToString();
            con.Close();

         
          




        
    }
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        
        if (txtAbout.Text.Length != 0)
        {

            string AboutText = txtAbout.Text.Trim();
         
            SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);


            
           
            con.Open();
            SqlCommand cmd1 = new SqlCommand("INSERT into AboutHistory Values ('" + AboutText + "')", con);
            cmd1.ExecuteNonQuery();
            con.Close();



            con.Open();
            SqlCommand cmd = new SqlCommand("DELETE from  AboutUsData", con);

            SqlDataReader rd = cmd.ExecuteReader();

            con.Close();





           
            SqlCommand cmd2 = new SqlCommand("INSERT into AboutUsData Values ('" + txtAbout.Text  + "')", con);
          
             con.Open();
            cmd2.ExecuteNonQuery();
            con.Close();




            Response.Redirect("~/Pages/AboutUs.aspx");


        }









问题是如何将旧文本+新文本一起提交?





The thing is how to submit the old text + the new text together?

推荐答案

将旧文本放在某些会话变量中,如

Put the old text in some session varible, like
Session["OldText"] = ds.Tables["AboutUsData"].Rows[0]["About"].ToString();            





之后,在触发插入查询时,连接新的



After that, while firing the insert query, concat with the new one

string AboutText = string.Concat(Session["OldText"].ToString(),txtAbout.Text.Trim());





希望这会有所帮助。



Hope this helps.


您可以使用隐藏控件而不是使用会话状态。在文本框中填充值时页面加载,也在隐藏控件中填充相同的值。



保存新值时,从隐藏控件中选择旧值使用Textbox控件中提供的新值添加它并保存到数据库中。



一般情况下,当需要存储要在整个数据库中使用的值时,需要使用Sessions页面和应用程序的特定会话。
You can use Hidden controls rather than using the Session state. On page load when you fill value in Textbox, fill the same value in hidden control as well.

While saving the new value, pick the old value from hidden control and add it with new value provided in Textbox control and save into the database.

Generally Sessions need to be used when there is a requirement to store the value to use across the pages and for particular session of the application.


使用stringbuider。

将文本框数据(原始)放入其中。然后将新文本附加到旧文本。
Use stringbuider.
Put the textbox data(original) into it. And then append the new text to the old one.


这篇关于如何从数据库中检索文本,编辑它并将其重新发送到数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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