Click here to Skip to main content
15,886,137 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:

C#
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?
Posted

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


After that, while firing the insert query, concat with the new one
C#
string AboutText = string.Concat(Session["OldText"].ToString(),txtAbout.Text.Trim());


Hope this helps.
 
Share this answer
 
Comments
AlaaHalabi 24-Jul-14 13:23pm    
Thank you for your help! it wasn't the solution i was looking for, but it helped fix the problem! Many Thanks.
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.
 
Share this answer
 
Use stringbuider.
Put the textbox data(original) into it. And then append the new text to the old one.
 
Share this answer
 
Thank You All for your Help, I Used a button next to text box that retrieves old data and display it in the text box, this way you could delete or add to the text box.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900