By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,952 Members | 1,619 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,952 IT Pros & Developers. It's quick & easy.

Error Incorrect syntax near ',' in asp.net

P: 1
Hy,
I have wrote in asp.net the next code:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Web;
  5. using System.Web.UI;
  6. using System.Web.UI.WebControls;
  7. using System.Data.SqlClient;
  8. public partial class Default2 : System.Web.UI.Page
  9. {
  10.     protected void Page_Load(object sender, EventArgs e)
  11.     {
  12.  
  13.     }
  14.     protected void Calendar1_SelectionChanged(object sender, EventArgs e)
  15.     {
  16.         TextBoxData.Text = Calendar1.SelectedDate.ToString();
  17.     }
  18.     protected void Button1_Click(object sender, EventArgs e)
  19.     {
  20.         try
  21.         {
  22.  
  23.             string connString = "Initial Catalog=PEOPLE;Data Source=John-4E29FADEE;Integrated Security=SSPI;";
  24.             SqlConnection conn = new SqlConnection(connString);
  25.             conn.Open();
  26.             Response.Write("Successful connection!");
  27.             SqlCommand comanda = new SqlCommand();
  28.             comanda.Connection = conn;
  29.             comanda.CommandType = System.Data.CommandType.Text;
  30.  
  31.             comanda.CommandText = "INSERT INTO dbo.PERSON VALUES (" + TextBoxID.Text + "," + TextBoxName.Text + "," + TextBoxInitiala.Text + "," + TextBoxLastName.Text + "," + TextBoxData.Text + "," + TextBoxCNP.Text + "," + TextBoxMother.Text + "," + TextBoxFather.Text + ")";
  32.             int no = comanda.ExecuteNonQuery();
  33.  
  34.             Response.Write("No of affected rows = " + no);
  35.             conn.Close();
  36.  
  37.         }
  38.         catch (SqlException exc)
  39.         {
  40.             Response.Write("Error " + exc.Message);
  41.         }
  42.  
  43.     }
  44.  
  45. }
And i get the message: Successful connection! Error Incorrect syntax near ','
I looked many times but I can not figure out what's wrong
Please help me
Oct 31 '13 #1
Share this Question
Share on Google+
1 Reply


P: 6
Your insert query must be changed as :
Expand|Select|Wrap|Line Numbers
  1. comanda.CommandText = "INSERT INTO dbo.PERSON VALUES (" + TextBoxID.Text + ",'" + TextBoxName.Text + "','" + TextBoxInitiala.Text + "','" + TextBoxLastName.Text + "','" + TextBoxData.Text + "','" + TextBoxCNP.Text + "','" + TextBoxMother.Text + "','" + TextBoxFather.Text + "')";
  2.  
assuming Name,Initials,Lastname,Data,BoxCNP,Mother,Father to be as varchar /string type in the database.

The format to insert/retrieve all string type declared values in the database from C# is
Expand|Select|Wrap|Line Numbers
  1. '"+ example.Text+"'
and for all fields declared in numeric category is
Expand|Select|Wrap|Line Numbers
  1. "+examplenumber.Text+"
.
Nov 19 '13 #2

Post your reply

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