Click here to Skip to main content
15,903,388 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have problem

i add btn

VB
cmd = New SqlCommand("insert into TBLSubScriptions ([Name],[PhoneNo],[Weight],[Length],[TimeClass],[subscriptionStart],[subscriptionEnd])
values ('" & txtName.Text & "','" & txtphone.Text & "','" & txtwigh.Text & "," & txtLength.Text & "','" & comoTimecl.Text & "','" & txtDatest.Value.Date & "','" & txtenddate.Value.Date & "')", con)


i have error
there are more columns in the insert statement values specified in the values Clause

What I have tried:

i will tried but no solve

i tried solve but noway

VB
cmd = New SqlCommand("insert into TBLSubScriptions ([Name],[PhoneNo],[Weight],[Length],[TimeClass],[subscriptionStart],[subscriptionEnd])
values ('" & txtName.Text & "','" & txtphone.Text & "','" & txtwigh.Text & "," & txtLength.Text & "','" & comoTimecl.Text & "','" & txtDatest.Value.Date & "','" & txtenddate.Value.Date & "')", con)
Posted
Updated 1-Apr-17 19:48pm
v2
Comments
Graeme_Grant 1-Apr-17 11:20am    
"there are more columns in the insert statement values specified in the values Clause"

It explains the issue very clearly. Count your fields and count you values in your SQL statement, they don't match!
Member 13099338 1-Apr-17 11:25am    
The number of fields is equal with some
[no name] 1-Apr-17 11:30am    
No it isn't. Use a proper parameterized query and you would not have the missing ' character problem that you have now. And you will save your database from an SQL injection attack at the same time.
PIEBALDconsult 1-Apr-17 11:53am    
Always use parameterized statements.
And format your SQL so it's more readable.

Formatting the sql Query string is vulnerable to SQL Injection[^] attacks
always use Parameterized queries to prevent SQL Injection Attacks in SQL Server[^]

VB
cmd = New SqlClient.SqlCommand("insert into TBLSubScriptions (Name,PhoneNo,Weight,Length,TimeClass,subscriptionStart,subscriptionEnd)values (@Name,@PhoneNo,@Weight,@Length,@TimeClass,@subscriptionStart,@subscriptionEnd)")
cmd.Parameters.AddWithValue("@Name", txtName.Text)
cmd.Parameters.AddWithValue("@PhoneNo", txtphone.Text)
cmd.Parameters.AddWithValue("@Weight", txtwigh.Text)
cmd.Parameters.AddWithValue("@Length", txtLength.Text)
cmd.Parameters.AddWithValue("@TimeClass", comoTimecl.Text)
cmd.Parameters.AddWithValue("@subscriptionStart", txtDatest.Value.Date)
cmd.Parameters.AddWithValue("@subscriptionEnd", txtenddate.Value.Date)
 
Share this answer
 
Never build an SQL query by concatenating with user inputs, it is named "SQL injection", it is dangerous for your database and error prone.
A single quote in a name and your program crash. If a user input like "Brian O'Conner" can crash your app, it is an SQL injection vulnerability.
SQL injection - Wikipedia[^]
SQL Injection[^]

Your actual problem is about single quotes matching with your data
VB
' Error here         V                 and                       V
& txtphone.Text & "','" & txtwigh.Text & "," & txtLength.Text & "','" & comoTimecl.Text &
 
Share this answer
 
v2
If you used a parameterised query it would make it clearer.

However look at txtwigh.text, no single quote after, thus it merges with the next field.
 
Share this answer
 
v2

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