Click here to Skip to main content
15,886,199 members
Please Sign up or sign in to vote.
2.25/5 (4 votes)
See more:
Hello.

I have 1 textbox, 1 button, and 1 SQL database.

I need update the database content with the textbox, but doesn't work.

What I can do?

The table:
<br />
|  ID  |  PagePath  |  PageName  |  PageContent  |<br />
|auto| varchar | varchar | varchar |<br />
<br />
| 1 | StartPage | Start | <h1>Hello!</h1> |


The code:

C#
protected void BtnUpdate_Click (object sender, EventArgs e)
{
   UpdateContent("StartPage","Start",TxtBoxEdit.Text);
}

private void UpdateContent(string PagePath, string PageName, string NewText)
{
    SqlCommand EditPage = new SqlCommand(
       "UPDATE Pages SET PageContent='" + NewText +
       "' WHERE PagePath='" + PagePath + "' AND PageName='" + PageName + "'", DBConnection);
            DBConnection.Open();        
            EditPage.ExecuteNonQuery();
            DBConnection.Close();
}
Posted
Comments
ZurdoDev 28-Feb-12 14:07pm    
Do you get an error? Have you done a SQL trace to see what is happening?
TANicox 28-Feb-12 14:19pm    
no, just dont update
ZurdoDev 28-Feb-12 14:20pm    
Then do a SQL trace because likely your WHERE statement is not finding a match.
Dean Oliver 28-Feb-12 14:18pm    
What is the exact error you are getting?
TANicox 28-Feb-12 14:28pm    
there is not an error

C#
string commandText = "UPDATE Pages SET PageContent = @pc WHERE PagePath=@pp AND PageName=@pn";

   using (SqlConnection connection = new SqlConnection(connectionString))
   {
       SqlCommand command = new SqlCommand(commandText, connection);
       command.Parameters.Add("@pp", SqlDbType.NVarChar);
       command.Parameters.Add("@pn", SqlDbType.NVarChar);

       // Use AddWithValue to assign values.

       command.Parameters.AddWithValue("@pc", NewText);

       try
       {
           connection.Open();
           Int32 rowsAffected = command.ExecuteNonQuery();
           Console.WriteLine("RowsAffected: {0}", rowsAffected);
       }
       catch (Exception ex)
       {
           Console.WriteLine(ex.Message);
       }
 
Share this answer
 
By looks of your table data, I am guessing your are trying to enter HTML tag inside the TextBox and grab those to save into DB.

You might need to use following method[^] to get the contents of TextBox and then write that back to the DB.

Hope it helps :)
 
Share this answer
 
Assign you update command to a string variable and check it's value during debugging. It seems your update statement's WHERE clause has no rows to update. That's why you don't see any error and still no rows are updated.
 
Share this answer
 
Comments
Oshtri Deka 28-Feb-12 15:11pm    
Sounds reasonable.
You do realise how dangerous that is, don't you?
Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

The original problem is probably that you need to change your connection string:
AttachDbFilename=~\App_Data\SAFA.mdf
May be the problem. Try changing it to use Server.MapPath to change teh "~" into a "normal" file path specification.

But seriously, use parametrized queries - a four character sequence typed in the textbox will destroy your entire database table!
 
Share this answer
 
if you are not getting error then
it may possible it is not finding the field according to your search
your text is being changed due to page script check enable i
think if you will update without script then it will update
enter a simple text it will work
 
Share this answer
 
The problem was in the Page_Load

When the page loaded, the control loading the text in the database, so when I pressed the button the value backs to default.

I use an

C#
if (!Page.IsPostBack)
to change that, and it's works

Thanks everyone for help
 
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