Click here to Skip to main content
15,892,697 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i can't insert multiline textbox into database...i want to insert it as it is
C#
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["connection"].ConnectionString);
           SqlCommand com = new SqlCommand();
           con.Open();
           com.Connection = con;
           string text = TextBox1.Text.ToString();

           string cmd1 = "insert into student_note ([note]) values("+text+") where student_id='" + Session["student_id"] + "' and course_id='" + Session["course_id"] + "'";
           SqlCommand cmd1x = new SqlCommand(cmd1, con);
           cmd1x.ExecuteNonQuery();
           table3.Visible = false;
           table1.Visible = true;
Posted
Comments
Richard C Bishop 16-Apr-13 15:04pm    
What is the problem? Do you have a question?
amr mustafa 16-Apr-13 15:05pm    
i want to insert the text as it is...i can't insert to database the text whith characters and spaces
[no name] 16-Apr-13 15:08pm    
make a try - catch to see exception and watch where is the problem...

or

try to insert directly without defining String!

"insert into student_note ([note]) values("+textBox1.Text+") where student_id='" + Session["student_id"] + "' and course_id='" + Session["course_id"] + "'";
amr mustafa 16-Apr-13 15:10pm    
i made debugging...the problem basically in the spaces

1 solution

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. That will also solve your problem...

C#
string cmd1 = "INSERT INTO student_note ([note]) VALUES(@NOTE) WHERE student_id=@SID AND course_id=@CID";
SqlCommand cmd1x = new SqlCommand(cmd1, con);
cmd1x.Parameters.AddWithValue("@SID", Session["student_id"]);
cmd1x.Parameters.AddWithValue("@CID", Session["course_id"]);
cmd1x.Parameters.AddWithValue("@NOTE", TextBox1.Text);
cmd1x.ExecuteNonQuery();



[edit]
I must be part asleep!
You don't use a WHERE clause on an INSERT statement - it adds a new row, not filters existing ones. You could use a WHERE with an UPDATE, but if you want to add a new row then change your SQL above to:
INSERT INTO student_note ([note], student_id, course_id) VALUES(@NOTE, @SID, @CID)";
- OriginalGriff
[/edit]
[edit2]Forgot a comma :doh: - OriginalGriff[/edit2]
 
Share this answer
 
v4
Comments
amr mustafa 16-Apr-13 15:31pm    
incorrect syntax near where...
amr mustafa 16-Apr-13 15:33pm    
ok ok......thank you so much...i'll try it
amr mustafa 16-Apr-13 15:35pm    
The parameterized query '(@SID nvarchar(4000),@CID nvarchar(4000),@NOTE nvarchar(36))INSE' expects the parameter '@SID', which was not supplied.
amr mustafa 16-Apr-13 15:36pm    
which comma?
OriginalGriff 16-Apr-13 15:39pm    
It's gone in the latest version.

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