错误'slip_id'附近的语法错误是什么意思?

时间:2016-01-25 19:53:36

标签: c# asp.net

我在asp.net中有以下c#代码:

public partial class UpdateSlip : System.Web.UI.Page
{

protected void search_Click(object sender, EventArgs e)
{
    // Clear text controls
    txtSlipID.Text = string.Empty;
    txtSlipLength.Text = string.Empty;
    txtSlipWidth.Text = string.Empty; 
    txtCovered.Text = string.Empty;
    txtFee.Text = string.Empty;
    intDockID.Text = string.Empty;
    intBoatID.Text = string.Empty;

    string connection =
    WebConfigurationManager.ConnectionStrings["popeye_marina"].ConnectionString;
    SqlConnection con = new SqlConnection(connection);
    string selectSQL;
    selectSQL = "SELECT * FROM slip WHERE slip_id=@id";
    SqlCommand cmd = new SqlCommand(selectSQL, con);
    int anInteger = int.Parse(intSlipID_srch.Text);
    cmd.Parameters.Add("@id", SqlDbType.Int).Value = anInteger;

    // Open database and read information
    try
    {
        con.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        reader.Read();

        // Fill the page controls

        txtSlipID.Text = reader["slip_id"].ToString();
        txtSlipLength.Text = reader["slip_length"].ToString();
        txtSlipWidth.Text = reader["slip_width"].ToString();
        txtCovered.Text = reader["covered"].ToString();
        txtFee.Text = reader["annual_fee"].ToString();
        intDockID.Text = reader["dock_id"].ToString();
        intBoatID.Text = reader["boat_id"].ToString();
        reader.Close();
        lblStatus.Text = "";
    }
    catch (Exception err)
    {
        lblStatus.Text = "Error getting Slip.  ";
        lblStatus.Text += err.Message;
    }
    finally
    {
        con.Close();
    }
}

protected void btn_update_Click(object sender, EventArgs e)
{
    string connection =
    WebConfigurationManager.ConnectionStrings["popeye_marina"].ConnectionString;
    SqlConnection con = new SqlConnection(connection);
    string updateSQL;
    int anInt = int.Parse(intBoatID.Text);
    updateSQL = "UPDATE slip SET ";
    /*updateSQL += "slip_length = 'txtlength.Text'";
    updateSQL += "slip_width = txtSlipWidth.Text, ";
    updateSQL += "covered = txtCovered.Text, ";
    updateSQL += "annual_fee = 'fee' ";
    updateSQL += "dock_id = intDockID.Text, ";*/
    updateSQL += "boat_id = anInt";

    updateSQL += "WHERE slip_id=@id";
    SqlCommand cmd = new SqlCommand(updateSQL, con);
    int anInteger = int.Parse(intSlipID_srch.Text);
    cmd.Parameters.Add("@id", SqlDbType.Int).Value = anInteger;
    int updated = 0;
    try
    {
        con.Open();
        updated = cmd.ExecuteNonQuery();
        lblStatus.Text = updated.ToString() + " record updated.";
    }
    catch (Exception err)
    {
        lblStatus.Text = "Error saving Slip.  ";
        lblStatus.Text += err.Message;
    }
    finally
    {
        con.Close();
    }
}
}

函数search_click按预期工作,并从数据库中检索记录。但是当我尝试使用函数btn_update_Click更新记录时,我收到错误错误保存滑动。 'slip_id'附近的语法不正确。注意我已经注释了很多代码以努力缩小问题范围。

我做错了什么?

1 个答案:

答案 0 :(得分:1)

您忘记了要构建的查询字符串中的各种空格:

updateSQL += "boat_id = anInt";
                             ^--no space
updateSQL += "WHERE slip_id=@id";
              ^--no space

所以你正在建设

... boat_id = anIntWHERE slip_id=@id
                  ^^^^---unknown field name