Error Incorrect syntax near ',' in asp.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JohnFlores424
    New Member
    • Oct 2013
    • 1

    Error Incorrect syntax near ',' in asp.net

    Hy,
    I have wrote in asp.net the next code:
    Code:
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Data.SqlClient;
    public partial class Default2 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
    
        }
        protected void Calendar1_SelectionChanged(object sender, EventArgs e)
        {
            TextBoxData.Text = Calendar1.SelectedDate.ToString();
        }
        protected void Button1_Click(object sender, EventArgs e)
        {
            try
            {
    
                string connString = "Initial Catalog=PEOPLE;Data Source=John-4E29FADEE;Integrated Security=SSPI;";
                SqlConnection conn = new SqlConnection(connString);
                conn.Open();
                Response.Write("Successful connection!");
                SqlCommand comanda = new SqlCommand();
                comanda.Connection = conn;
                comanda.CommandType = System.Data.CommandType.Text;
    
                comanda.CommandText = "INSERT INTO dbo.PERSON VALUES (" + TextBoxID.Text + "," + TextBoxName.Text + "," + TextBoxInitiala.Text + "," + TextBoxLastName.Text + "," + TextBoxData.Text + "," + TextBoxCNP.Text + "," + TextBoxMother.Text + "," + TextBoxFather.Text + ")";
                int no = comanda.ExecuteNonQuery();
    
                Response.Write("No of affected rows = " + no);
                conn.Close();
    
            }
            catch (SqlException exc)
            {
                Response.Write("Error " + exc.Message);
            }
    
        }
        
    }
    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
    Last edited by Rabbit; Oct 31 '13, 08:21 PM. Reason: Please use [CODE] and [/CODE] tags when posting code or formatted data.
  • psurvi
    New Member
    • Nov 2013
    • 6

    #2
    Your insert query must be changed as :
    Code:
    comanda.CommandText = "INSERT INTO dbo.PERSON VALUES (" + TextBoxID.Text + ",'" + TextBoxName.Text + "','" + TextBoxInitiala.Text + "','" + TextBoxLastName.Text + "','" + TextBoxData.Text + "','" + TextBoxCNP.Text + "','" + TextBoxMother.Text + "','" + TextBoxFather.Text + "')";
    assuming Name,Initials,L astname,Data,Bo xCNP,Mother,Fat her 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
    Code:
    '"+ example.Text+"'
    and for all fields declared in numeric category is
    Code:
    "+examplenumber.Text+"
    .

    Comment

    Working...