0

I have text and a button and when on submit, I am checking whether the database has any rows-if not then insert rows or else update them, but on submit its throwing an error saying incorrect syntax at "cmd.ExecuteNonQuery" in the else condition

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;


public partial class CM : System.Web.UI.Page
{
    DataSet ds = new DataSet();
    SqlDataAdapter da;
    SqlCommand cmd;
    DataTable dt;
    SqlConnection con = new SqlConnection("server =consulting76\\SQLEXPRESS; database = msdb; Integrated Security=True; MultipleActiveResultSets=True");

    protected void Page_Load(object sender, EventArgs e)
    {
        da = new SqlDataAdapter("Select * from NOTESMAKER", con);
        da.Fill(ds);
        //dt = ds.Tables["NOTESMAKER"];
    }

    protected void Button1_Click(object sender, EventArgs e)
    {   
        con.Open();
        if (ds.Tables[0].Rows.Count == 0)
        {
            cmd = new SqlCommand("Insert into NOTESMAKER(NOTESMAKER) Values(@text1)",con);
            cmd.Parameters.Add(new SqlParameter("@text1", SqlDbType.NText)).Value = TextBox1.Text;
            da.InsertCommand = cmd;
            cmd.ExecuteNonQuery();
        }
        else
        {
            cmd = new SqlCommand("Update NOTESMAKER set NOTESMAKER = @text1)",con);
            cmd.Parameters.Add(new SqlParameter("@text1", SqlDbType.NText)).Value = TextBox1.Text;
            da.UpdateCommand = cmd;
            cmd.ExecuteNonQuery();
        }
        con.Close();
    }

}
4
  • 1
    Apart from error, you are trying to update without a WHERE clause ? you know it will update all the rows Commented Nov 20, 2013 at 16:52
  • Updated the question. Its in the else condition Commented Nov 20, 2013 at 16:52
  • Is the table name and the one column name really the same ("NOTESMAKER")? Commented Nov 20, 2013 at 16:53
  • @LeftyCoder - Yes the table name and column name are same Commented Nov 20, 2013 at 17:03

1 Answer 1

5

You are closing a bracket on this line, which is never opened:

cmd = new SqlCommand("Update NOTESMAKER set NOTESMAKER = @text1)",con);

Also, setting the InsertCommand and UpdateCommand properties of the data adapter isn't neccessary.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.