473,395 Members | 1,678 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

question on SQL INSERT string

49
hi
i make web application by c#,asp.net
i want to make page that regist in DB
but when i run the project tell me
Incorrect syntax near 'value' i want to now what is the problem
thanks
Feb 1 '09 #1
12 1994
could u plaese send the code u r using ?
Feb 1 '09 #2
orked
49
Expand|Select|Wrap|Line Numbers
  1.  
  2. protected void Button1_Click(object sender, EventArgs e)
  3.     {
  4.         SqlConnection dbCon = new SqlConnection();
  5.         dbCon.ConnectionString = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\db.mdf;Integrated Security=True;User Instance=True";
  6.         dbCon.Open();
  7.         SqlCommand sqlcom = new SqlCommand();
  8.         sqlcom.Connection = dbCon;
  9.         sqlcom.CommandType = CommandType.Text;
  10.         sqlcom.CommandText = sqlcom.CommandText = "insert into client (Name,SSN,Salary)" + "value(@NameTextBox.Text ,@SSNTextBox.Text ,@SalaryTextBox.Text )";
  11.         //sqlcom.CommandText = "insert into client (Name,SSN,Salary)  value  ('" + NameTextBox.Text + "','" + SSNTextBox.Text + "','" + SalaryTextBox.Text + "')";
  12.         SqlDataReader rdrData;
  13.         rdrData = sqlcom.ExecuteReader();
  14.         GridView1.DataSource = rdrData;
  15.         GridView1.DataBind();
  16.     }
  17.  
Feb 1 '09 #3
its not correct
sqlcom.CommandText = sqlcom.CommandText = "insert into client (Name,SSN,Salary)" + "value(@NameTextBox.Text ,@SSNTextBox.Text ,@SalaryTextBox.Text )";

it should be
sqlcom.CommandText = "insert into client (Name,SSN,Salary) value ('" + NameTextBox.Text + "','" + SSNTextBox.Text + "','" + SalaryTextBox.Text + "')";
Feb 1 '09 #4
orked
49
the same error appear,no exchange
Feb 1 '09 #5
Bassem
344 100+
Hi,
don't forget @ to the frist of your string, or could use \ as a scape char.
Feb 1 '09 #6
Bassem
344 100+
Oh i forgot, how did you declare your flieds SSN and Salary in your DB, it may not be suitable with your users inputs.
Consider ControlParameter, is the most suitable to collect data form users without having to worry about many.... .
Have attention to this !

Kind Regards,
Bassem
Feb 1 '09 #7
Plater
7,872 Expert 4TB
I think the words is VALUES not VALUE
Feb 2 '09 #8
orked
49
when i write values make another error
and when i remove it the same error appear again
Feb 2 '09 #9
Bassem
344 100+
Yes, it is VALUES and make your correction on that.
Feb 2 '09 #10
Plater
7,872 Expert 4TB
The string:
Expand|Select|Wrap|Line Numbers
  1. "insert into client (Name,SSN,Salary) values ('" + NameTextBox.Text + "', '" + SSNTextBox.Text + "', '" + SalaryTextBox.Text + "' )"; 
  2.  
Should be of correct format.
Assuming you have a table called "client", that table contains the columns Name - SSN - Salary, they all expect a string type of some sort as a value and that there are no other required columns in the table you are leaving out
Feb 2 '09 #11
Frinavale
9,735 Expert Mod 8TB
@amirghaffarie1362
You should not be dynamically creating your SQL command as suggested by amirghaffarie.

You should use parameters instead:

Expand|Select|Wrap|Line Numbers
  1. sqlcom.CommandText = "INSERT INTO CLIENT(Name, SSN, Salary)" +
  2.                      "VALUES(@clientName, @clientSSN, @clientSalary)"; 
  3.  
  4. sqlCom.Parameters.Add("@clientName", SqlDbType.VarChar).Value = NameTextBox.Text ;
  5. sqlCom.Parmaeters.Add("@clientSSN", SqlDbType.VarChar).Value = SSNTextBox.Text;
  6. sqlCom.Parameters.Add("@clientSalary",SqlDbType.VarChar).Value = SalaryTextBox.Text;
The reason for using parameters is because the information entered by the user will not be compiled with your SQL command.

If you create your SQL statement without parameters, anything entered by the user will be compiled with your sql command. This could leave you vulnerable to an SQL insertion attack.

When you encounter a new error, please post what the error message says so that we can continue to help you :)

-Frinny
Feb 2 '09 #12
orked
49
Hi,
thanks for all one ,the page run without error,if i face any error ,i will send again

regards
orked
Feb 3 '09 #13

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Fang | last post by:
I want to open a file and write some string between the the 3rd line and the 4th line. my code (as follows) can insert after the 3rd line but somehow it owerwrites the 4th line. So i lost its...
2
by: help | last post by:
I want to open a file and write some string between the the 3rd line and the 4th line. my code (as follows) can insert after the 3rd line but somehow it owerwrites the 4th line. So i lost its...
3
by: jonhanks | last post by:
Can anyone see anything wrong with this SQL string? REF is a long integer field and DATE is a text field. strSQL = "INSERT INTO PHOTOSAccess (REF, DATE)" _ & " VALUES (" & txtRef & ", '" &...
0
by: c_kubie | last post by:
I have an update string getting a list of names that are list1 but not in list2. (Sorry for the lame example) list1 ---- Bill Bill mike tom jim
0
by: Kevin Lawrence | last post by:
Hi I need to store binary data into SQL Server but is there anyway it can be done in a constructed INSERT rather than using the @parameters? ie INSERT INTO Table (BLOB)...
14
by: nospam | last post by:
From the book "There is an important difference between these definitions: char amessage="now is the time"; char *pmessage ="now is the time"; snip On the other hand, pmessage is a...
1
by: =?Utf-8?B?TW9uaWNh?= | last post by:
hi SQL queries does not recognize *$()!.,?/\{};:'"&^+=<>|. if I have a variable alike this: Date = "Thursday, March 01, 2007 - 9:53 PM"; how can I inset in my database? strCommand =...
1
by: David | last post by:
Hi, I have an asp page with a form, which has a lot of fields ........ 30 to 40 ! Is there an easier way to loop through all the fields submitted to the insert page or is the only way to do...
2
by: nrworld | last post by:
Hi, I am facing a problem while using pepared statement for inserting data in MS-Access db. private const String CONN_STR = "Provider=Microsoft.Jet.OLEDB.4.0;Data...
4
by: Abandoned | last post by:
Hi.. I want to insert some data to postgresql.. My insert code: yer="019" cursor.execute("INSERT INTO ids_%s (id) VALUES (%s)", (yer, id)) I don't want to use % when the insert operation. in...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.