开发者

Guess the error in the insert statement

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 occurre开发者_StackOverflow中文版d:');</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


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.


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, ...
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜