2

Anybody out there with some eagle eyes? I want to fish out the location of the error in the following insert statement. i used a breakpoint to track the codes while running and i get the exception error that says (There was an Incorrect syntax near the keyword 'Case'). Get you eagles eyes on...

public partial class OpenCase : System.Web.UI.Page
{
    string adminString;
    protected void Page_Load(object sender, EventArgs e)
    {
        adminString = "CA123";
    }

    protected void openCaseBotton_Click(object sender, EventArgs e)
    {
        //SQL connection string
        SqlDataSource CSMDataSource = new SqlDataSource();
        CSMDataSource.ConnectionString = ConfigurationManager.ConnectionStrings["CMSSQL3ConnectionString"].ToString();

        //SQL Insert command with variables
        CSMDataSource.InsertCommandType = SqlDataSourceCommandType.Text;
        CSMDataSource.InsertCommand = "INSERT INTO Case (CaseID, CaseDesc, DateOpened, CasePriority, AdministratorID) VALUES (@CaseID, @CaseDesc, @DateOpened, @CasePriority, @AdministratorID)";

        //Actual Insertion with values from textboxes into databse fields
        CSMDataSource.InsertParameters.Add("CaseID", caseIDTextBox.Text);
        CSMDataSource.InsertParameters.Add("CaseDesc", caseDescTextBox.Text);
        CSMDataSource.InsertParameters.Add("DateOpened", DateTime.Now.ToString());
        CSMDataSource.InsertParameters.Add("CasePriority", null);
        CSMDataSource.InsertParameters.Add("AdministratorID", adminString.ToString());

        int rowsCommitted = 0;

        //Try catch method to catch exceptions during insert
        try
        {
            rowsCommitted = CSMDataSource.Insert();
            Response.Write(@"<script language='javascript'>alert('The following errors have occurred:');</script>");
        }

        catch (Exception ex)
        {
            //error message displayed when exception occurs


            string script = "<script>alert('" + ex.Message + "');</script>";
            Response.Write("The following Error occurred while entering the records into the database" + " " + ex.ToString() + " ");
            Response.Redirect("~/ErrorPage.aspx", false);
        }
        finally
        {
            CSMDataSource = null;
        }

        //Where to go next if insert was successful or failed
        if (rowsCommitted != 0)
        {
            Response.Redirect("~/CaseAdmin.aspx", false);
        }
        else
        {

            Response.Redirect("~/ErrorPage.aspx", false);
        }

    }

    protected void addExhibitBotton_Click(object sender, EventArgs e)
    {
        Response.Redirect("~/EntryForms/AddExhibit.aspx", false);

    }
}

Seen anything? may be u need a C# error vision goggles...lol

2 Answers 2

7

Near the INSERT ? well, CASE is a SQL reserved word, so try simply:

CSMDataSource.InsertCommand = "INSERT INTO [Case] (...

to tell it that Case is an object name, not the SQL keyword.

5
  • @Selase: Also try [CASE] (in upper case). Because DBMS can capitalize objects' names (Oracle does). Use [Object Name] in MS SQL Server, "Object Name" in Oracle Jan 11, 2011 at 7:20
  • @Alexander in SQL Server, the casing can be significant, depending on the database settings. If it is in case-sensitive mode, then [Case] will work if and only if the table was actually created as Case - and [CASE] will work if and only if the table was created as CASE. In case-insensitive mode, either will work fine. Jan 11, 2011 at 7:26
  • @Marc: I thought [] mean exact match in any situation. So, if you are creating obj in case-insensitive mode, DBMS will capitalize its name and later when using [] you have to spell it how it's stored, i.e. upper case. Don't have SQL Server instance at hand. Am I wrong ? Jan 11, 2011 at 7:37
  • @Alexander - no, in SQL server the [] is optional and merely says "this is an object"; used mainly for keywords or for [tables with spaces], but select * from foo and select * from [foo] are identical. Jan 11, 2011 at 7:39
  • Wish i had seen your comment earlier Marc. guess i scrolled too much and ended up on the answer from paxdiablo that advised 'Case'. am most certain [Case] would have worked having read other answers. Thank you very much for you contribution...its was very very helpful... thanks again
    – Selase
    Jan 11, 2011 at 8:54
4

I'm pretty certain that case is actually an SQL keyword.

Depending on the DBMS, you may have to do something like enclose it in backticks or other delimiters to tell the execution engine to treat it as a non-keyword, something like:

CSMDataSource.InsertCommand = "INSERT INTO `Case` (CaseID, ...
11
  • is it? well i named my database table case so how am i supposed to work around that? At first i had a table named users and it was giving me errors to so i tested the statement in SQL management studio and found out that table previously named Users was a keyword. i went through hell to change the name and all the relationships involved. How many keywords am i supposed to change? Gosh..!!!!
    – Selase
    Jan 11, 2011 at 7:03
  • (1) Find and use the escape delimiters. (2) Rename your table. (3) Well, actually, two was all I could think of (and the first is probably the best bet if you don't want to worry about keyword clashes). I never use keywords for objects myself partially because you should be using more descriptive names and partially because I've seen DB2/z code that looked like "insert into date(date,seq) select date,seq+1 from date".
    – paxdiablo
    Jan 11, 2011 at 7:06
  • 1
    @Selase - none at all; please look carefully at the two answers provided - which show you how to handle this for different databases. Jan 11, 2011 at 7:06
  • @ Marc- I tried the correction from the first answer by placing the case in single quotes like 'Case' and this time around the exception message displays "$exception {"Incorrect syntax near 'Case'."}" so i would have to rename the table then i guess....
    – Selase
    Jan 11, 2011 at 7:20
  • @Selease, for a start, you need to tell us which DBMS you are using. MySQL for example uses backticks (not apostrophes like you have used in the previous comment). I think SQLServer user square brackets. But, to be honest (and, apologetically, brutal), "case" is actually a crappy name for a table. You would be vastly better off with something more descriptive.
    – paxdiablo
    Jan 11, 2011 at 7:24

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.