Click here to Skip to main content
15,896,063 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm using C# and Microsoft Microsoft® SQL Server® Compact

All data inserts like this in the database

Ex I input my name database store like this

System.Windows.Forms.TextBox, Text: chamikara

There any way to remove this part "System. Windows. Forms. TextBox, Text: "

your help is greatly appreciated


My Code

C#
SqlCeConnection con = new SqlCeConnection("Data Source=" + app_path + "\\application.sdf;Persist Security Info=False;");
            con.Open();
            SqlCeCommand cm = new SqlCeCommand("INSERT INTO tbl_user(u_id,u_name,u_scl,u_que) VALUES (@u_id,@u_name, @u_scl, @u_que)", con);
            cm.Parameters.AddWithValue("@u_id", 1);
            cm.Parameters.AddWithValue("@u_name", txtpass.ToString());
            cm.Parameters.AddWithValue("@u_scl", txtans.ToString());
            cm.Parameters.AddWithValue("@u_que", index);      
            

            try
            {
                int eff = cm.ExecuteNonQuery();

                if (eff == 1)
                {
                    con.Close();
                    MessageBox.Show("Data Added " , "Done");
                }
                else
                {
                    con.Close();
                    MessageBox.Show("not inserted ", "Error");
                }
            }
            catch (SqlCeException ex)
            {
                MessageBox.Show(ex.ToString());
            }
Posted
Updated 7-Nov-13 8:26am
v2

1 solution

Yes. Either:
1) Use the TextBox.Text property instead of the Textbox.ToString method.
2) Use parametrised queries and the TextBox.Text property. This way you avoid SQL Injection attacks as well, which can damage or destroy your database...
 
Share this answer
 
Comments
Manoj Chamikara 7-Nov-13 14:27pm    
@OriginalGriff Thank your quick response yes dude i code like that
i improve question with code
OriginalGriff 7-Nov-13 14:30pm    
"yes dude i code like that"
Probably not.
If txtpass and txtans are TextBoxes (and I suspect they are) then ToString returns the type and the Text property. If there were strings, you wouldn't need the ToString call at all, would you? :laugh:
Try this:
cm.Parameters.AddWithValue("@u_name", txtpass.Text);
cm.Parameters.AddWithValue("@u_scl", txtans.Text);
Manoj Chamikara 7-Nov-13 14:34pm    
You right Thank you my problem solved
OriginalGriff 7-Nov-13 14:41pm    
You're welcome!

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