Click here to Skip to main content
15,892,059 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am trying to insert Record into a Database, but i am getting following error.

Incorrect Syntax error near ')'



C#
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;


namespace Thursday
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void button1_Click(object sender, EventArgs e)
        {
            try
            {

                SqlConnection sqlconn = new SqlConnection("Data Source=AAN-PC;Initial Catalog=Thusdata;Integrated Security=True");
                SqlDataAdapter sa = new SqlDataAdapter();
                sa.InsertCommand = new SqlCommand("insert into Table_1(SensorValue)",sqlconn);
                sa.InsertCommand.Parameters.Add("@SensorValue", SqlDbType.Int).Value = SensorValue.Text;
                sqlconn.Open();
                try
                {
                    sa.InsertCommand.ExecuteNonQuery();
                }
                catch (FormatException ex) { MessageBox.Show(ex.Message); }
                MessageBox.Show("Record Inserted");
                sqlconn.Close();
            }
            catch (SqlException ea) { MessageBox.Show(ea.Message); }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }
    }
}
Posted
Updated 16-Jan-13 18:55pm
v2

replace two line of your code for this

C#
sa.InsertCommand = new SqlCommand("insert into Table_1(SensorValue)values(@SensorValue)",sqlconn);
sa.InsertCommand.Parameters.AddWithValue("@SensorValue", SensorValue.Text);
 
Share this answer
 
Comments
fjdiewornncalwe 17-Jan-13 9:55am    
My 5. This is the way to do this.
Shambhoo kumar 18-Jan-13 12:20pm    
Thanks.. :)
change ur code as given below
C#
sa.InsertCommand = new SqlCommand("insert into Table_1(SensorValue) values (@SensorValue)",sqlconn);
 
Share this answer
 
Comments
ontheline89 17-Jan-13 0:54am    
Thank you for your reply but i am still getting error.

Must Declare the Scalar Variable "@SensorValue"
HI,

Insetead of using the code like:

SQL
SqlDataAdapter sa = new SqlDataAdapter();
                sa.InsertCommand = new SqlCommand("insert into Table_1(SensorValue)",sqlconn);
                sa.InsertCommand.Parameters.Add("@SensorValue", SqlDbType.Int).Value = SensorValue.Text;



Use like:

SQL
SqlDataAdapter sa = new SqlDataAdapter();
string query = "insert into Table_1(SensorValue) values(" + SensorValue.Text + ")";
                sa.InsertCommand = new SqlCommand(query ,sqlconn);


Thanks
 
Share this answer
 
Comments
ontheline89 17-Jan-13 1:08am    
Thank you so much Sisri Patro.
It worked.
[no name] 17-Jan-13 1:16am    
Yours welcome dear...
fjdiewornncalwe 17-Jan-13 9:54am    
This answer is nothing more than an sql injection invasion request. SQL queries should ALWAYS use paramatarized arguments, not inline ones as you suggest here.
[no name] 17-Jan-13 10:07am    
I would rather suggest for the stored procedure not inline query.
use this
C#
SqlConnection sqlconn = new SqlConnection("Data Source=AAN-PC;Initial Catalog=Thusdata;Integrated Security=True");
                SqlDataAdapter sa = new SqlDataAdapter();	
				sa.InsertCommand = new SqlCommand("insert into Table_1(SensorValue) values ("+SensorValue.Text+")",sqlconn);
                
                sqlconn.Open();
try
{
 sa.InsertCommand.ExecuteNonQuery();
}				
catch (FormatException ex) { MessageBox.Show(ex.Message); }
                MessageBox.Show("Record Inserted");
                sqlconn.Close();
 
Share this answer
 
v3
Comments
ontheline89 17-Jan-13 1:14am    
Thank you so much !

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