Click here to Skip to main content
15,897,704 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hey guys, so my professor assigned this asp.net/c# project where we have to create a cryptogram. So there are two textboxes, the textbox on the left is supposed to be read-only, and underneath it is the "Get" button. When a player presses the "Get" button, it's supposed to output an encrypted text (kinda like caesar cipher) and players will type in their answers in the textbox on the right and click the "Solve" button underneath the right textbox. Anyway, my problem is when I get the get button, it doesn't clear the previous value and it just continues to add more encrypted words at the end of the first one. I want the textbox to refresh and get a different value and display only one encrypted text everytime a user clicks the "Get" button. How do I do that? I included my code below and posted a screenshot of the problem. I have yet to code the logic for solve button so don't worry about it!

Link to the image is: http://i405.photobucket.com/albums/pp138/la_chua15/cryptoquest-game_zpsorgf3frq.jpg[^]

As you can see, more words get added to the textbox when I click get button repeatedly.

Here's my code!

protected void Page_Load(object sender, EventArgs e)
        {
           
        }

        protected void btnGet_Click(object sender, EventArgs e)
        {
            {
                //1. build the connection string for our SQL instance
                string connectionString = "Data Source=LA-HPENVY15;Initial Catalog=CryptoText;Integrated Security=True";

                //2. build the connection to our SQL instance

                using (SqlConnection con = new SqlConnection(connectionString))
                {
                    //open the connection
                    con.Open();

                    //string variable to hold the ouput
                    string result = String.Empty;

                    //3. Build our SQL query
                    SqlCommand cmd = new SqlCommand("SELECT TOP 1 Cipher FROM CipherText ORDER BY NEWID()", con);

                    //4. Execute the query
                    SqlDataReader reader = cmd.ExecuteReader();

                    while (reader.Read())
                    {
                        //add the results from the SQL query to a textbox
                        result = reader["Cipher"].ToString();
                        txtGet.Text += result;
                    }

                    //5. Always close connections when you're done
                    con.Close();

                }
            }
        }

    }
}
Posted

That is because you are concatenation the result to the TextBox value.
C#
txtGet.Text += result;

Change this code to...
C#
txtGet.Text = result;
 
Share this answer
 
v2
Try to clearing out the textbox value outside of Postback block as below:
C#
protected void Page_Load(object sender, EventArgs e)
{
   txtGet.Text = "";
}
 
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