none
insert query with where clause in windows form C#

    Question

  • Hi everyone!

    I am using the following code to insert data into a table called 'callreference' 

               

    cmd = new SqlCommand("insert into callreference (cat1,pd1,q1,vendor1,pi1,idate1) values ('" + comboBox9.Text + "','" + textBox3.Text + "','" + textBox5.Text + "','" + comboBox1.Text + "','" + textBox7.Text + "','" + dateTimePicker2.Text + "' where callref = '"+textBox1.Text+"')", con);

    cmd.ExecuteNonQuery();

    The attribute callref used with where clause is already present in the table.            


    and I am getting the exception :- Incorrect syntax near the keyword 'where'. 

    Why is this so?

    kindly help! thanks!

    Wednesday, June 19, 2013 7:34 AM

Answers

  • Hi,

    You cannot use WHERE clause with INSERT statement.

    Are you trying to update values in tables based on WHERE condition? in that case you need to use UPDATE statement.

    cmd = new SqlCommand("update callreference SET cat1 = '" + comboBox9.Text + "', pd1 = '" + textBox3.Text + "', q1 = '" + textBox5.Text + "', vendor1 = '" + comboBox1.Text + "', pi1 = '" + textBox7.Text + "',' idate1 = " + dateTimePicker2.Text + "' where callref = '" + textBox1.Text + "'", con);



    - Vishal

    SqlAndMe.com


    Wednesday, June 19, 2013 7:40 AM

  • The attribute callref used with where clause is already present in the table.            

    and I am getting the exception :- Incorrect syntax near the keyword 'where'. 


    If the value is already present then you should be trying UPDATE command instead of Insert.

    The insert query mentioned the values are passed directly , then it doesnt require WHERE clause.


    Thanks
    Sarat

    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Wednesday, June 19, 2013 7:41 AM
    Answerer

All replies

  • Hi,

    You cannot use WHERE clause with INSERT statement.

    Are you trying to update values in tables based on WHERE condition? in that case you need to use UPDATE statement.

    cmd = new SqlCommand("update callreference SET cat1 = '" + comboBox9.Text + "', pd1 = '" + textBox3.Text + "', q1 = '" + textBox5.Text + "', vendor1 = '" + comboBox1.Text + "', pi1 = '" + textBox7.Text + "',' idate1 = " + dateTimePicker2.Text + "' where callref = '" + textBox1.Text + "'", con);



    - Vishal

    SqlAndMe.com


    Wednesday, June 19, 2013 7:40 AM

  • The attribute callref used with where clause is already present in the table.            

    and I am getting the exception :- Incorrect syntax near the keyword 'where'. 


    If the value is already present then you should be trying UPDATE command instead of Insert.

    The insert query mentioned the values are passed directly , then it doesnt require WHERE clause.


    Thanks
    Sarat

    Please use Marked as Answer if my post solved your problem and use Vote As Helpful if a post was useful.

    Wednesday, June 19, 2013 7:41 AM
    Answerer
  • please,please do not use  such technique , because it prompts to SQL Injection, have you heard about it? Why not using parameters?

    string stmt = "INSERT INTO dbo.Test(id, name) VALUES(@ID, @Name)";
    
    SqlCommand cmd = new SqlCommand(smt, _connection);
    cmd.Parameters.Add("@ID", SqlDbType.Int);
    cmd.Parameters.Add("@Name", SqlDbType.VarChar, 100);
    
    for (int i = 0; i < 10000; i++)
    {
        cmd.Parameters["@ID"].Value = i;
        cmd.Parameters["@Name"].Value = i.ToString();
    
        cmd.ExecuteNonQuery();
    }


    Best Regards,Uri Dimant SQL Server MVP, http://sqlblog.com/blogs/uri_dimant/

    MS SQL optimization: MS SQL Development and Optimization
    MS SQL Blog: Large scale of database and data cleansing
    Remote DBA Services: Improves MS SQL Database Performance

    Wednesday, June 19, 2013 8:52 AM
    Answerer
  • Ohh...that was so stupid of me!

    Thanks!!

    Wednesday, June 19, 2013 9:08 AM