when we connect my registion form in sql server in cmd.ExecuteNonQuery(); line error will occur

Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.Data.SqlClient.SqlException: Incorrect syntax near '-'. Incorrect syntax near '-'.





using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace Regisation_Form
{
    public partial class WebForm1 : System.Web.UI.Page
    {
        SqlCommand cmd = new SqlCommand();
        SqlConnection con = new SqlConnection();
        protected void Page_Load(object sender, EventArgs e)
        {

            con.ConnectionString = "Data Source=DESKTOP-7FCU1CM\\SQLEXPRESS; Initial Catalog=RegistationFormData; Integrated Security=True";
            con.Open();
        }

        protected void Unnamed12_Click(object sender, EventArgs e)
        {
           // String cs = ConfigurationManager.ConnectionStrings["RegistationFormDataConnectionString"].ConnectionString;
            //SqlConnection con = new SqlConnection(cs);
            
            SqlCommand cmd = new SqlCommand("Insert into RegistationForm" + "(First-Name, Last-Name, Mobile-No, Date Of Birth, E-mail, Address,Pincode,State,Country, Gender,Password) values(@First-Name,@Last-Name,@Mobile-no,@Date Of Birth,@E-mail,@Address,@pincode,@Gender, @State,@Country, @Password)", con);
            cmd.Parameters.AddWithValue("@First-Name", TextBox1.Text);
            cmd.Parameters.AddWithValue("@Last-Name", TextBox2.Text);
            cmd.Parameters.AddWithValue("@Mobile-no", TextBox3.Text);
                cmd.Parameters.AddWithValue("@Date Of Birth", TextBox4.Text);
            cmd.Parameters.AddWithValue("@E-Mail", TextBox5.Text);
            cmd.Parameters.AddWithValue("@Address", TextBox6.Text);
            cmd.Parameters.AddWithValue("@pincode", TextBox7.Text);
           cmd.Parameters.AddWithValue("@State", TextBox8.Text);
           cmd.Parameters.AddWithValue("@Country", TextBox9.Text);
            cmd.Parameters.AddWithValue("@Gender", DropDownList1 .SelectedItem.Value);
            cmd.Parameters.AddWithValue("@Password", TextBox10.Text);
           cmd.ExecuteNonQuery();
     




        }
    }
}


Read more here: https://stackoverflow.com/questions/64945777/when-we-connect-my-registion-form-in-sql-server-in-cmd-executenonquery-line-e

Content Attribution

This content was originally published by Anubhav Rajput at Recent Questions - Stack Overflow, and is syndicated here via their RSS feed. You can read the original post over there.

%d bloggers like this: