Click here to Skip to main content
15,886,095 members
Please Sign up or sign in to vote.
4.00/5 (2 votes)
See more:
hello friends i m having text editor in that i want to store data in data base. in that i found error when i type 'asdfasdf'
i know the logic when we replace these
'
to some sign and store it in database but i dont know how to done dis

freetextbox1.text is <big>text editor</big>

my code is
try
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("update content set pagetitle='"+TxtPagetitle.Text+"',description='"+FreeTextBox1.Text+"' where pagename='"+ddlQuees.Text+"' ", con);
            cmd.ExecuteNonQuery();
            
            Up.Text = "Updated Successfullly";
            con.Close();
            bind();
        }
        catch
        {

            Up.Text = "not done";
        }
Posted
Updated 31-May-11 2:50am
v3

You should never, ever use direct unvalidated user input in a SQL script. Search for SQL Injection to understand why.

You should be using stored procedures or at the least parametrized queries.
 
Share this answer
 
Comments
Wonde Tadesse 26-May-11 20:28pm    
5+
Don't concatenate strings. Not only does it cause problems like the one you are having, but it leaves you open to an SQL Injection attack, which could accidentally or deliberately destroy your database.

Instead, use parametrized queries:
try
    {
    con.Open();
    SqlCommand cmd = new SqlCommand("UPDATE content SET pagetitle=@PT, description=@DS WHERE pagename=@PN", con);
    cmd.Parameters.AddWithValue("@PT", TxtPagetitle.Text);
    cmd.Parameters.AddWithValue("@DS", FreeTextBox1.Text);
    cmd.Parameters.AddWithValue("@PN", ddlQuees.Text);
    cmd.ExecuteNonQuery();

    Up.Text = "Updated Successfullly";
    bind();
    }
catch
    {
    Up.Text = "not done";
    }
finally
    {
    con.Close();
    }
That should fix your problem!
 
Share this answer
 
Comments
beginner in C#.net 26-May-11 9:35am    
it works 100%...my 5
parmar_punit 26-May-11 10:12am    
good answer my 5
Wonde Tadesse 26-May-11 20:28pm    
5+

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