【问题标题】:SqlException was unhandled by user codeSqlException 未被用户代码处理
【发布时间】:2012-10-25 09:27:03
【问题描述】:

我正在开发一个航空公司预订系统,一切都编译得很好......用户应该注册到该网站,然后检查可用的航班,然后用户可以预订航班,但是在预订航班时我遇到了问题飞行值无法插入数据库,弹出错误提示

SqlException was unhandled by user code
Violation of PRIMARY KEY constraint 'PK_Plist'. Cannot insert duplicate key in object 'dbo.Plist'.
The statement has been terminated.

什么是错的或者我在做什么是不对的 这是我的代码,异常在此代码上闪烁

int i = cmd.ExecuteNonQuery();

完整代码

protected void Button1_Click1(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection(constring);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        con.Open();
        cmd.CommandText = "select flightid from schedule where flightid='" + DropDownList1.Text + "' and Flightname='" + DropDownList2.Text + "' and Fromstation='" + DropDownList3.Text + "' and Tostation='" + DropDownList4.Text + "' and dateandtimings='" + DropDownList6.Text + "'";
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        DataSet ds = new DataSet();
        da.Fill(ds, "emp");
        if (ds.Tables["emp"].Rows.Count > 0)
        {

            cmd.CommandText = "insert into Plist(Pid,passengername,flightid,Flightname,Fromstation,Tostation,category,Dateandtimings) values('" + Autonumber() + "','" + TextBox1.Text + "','" + DropDownList1.Text + "','" + DropDownList2.Text + "','" + DropDownList3.Text + "','" + DropDownList4.Text + "','" + DropDownList5.Text + "','" + DropDownList6.Text + "')";
            int i = cmd.ExecuteNonQuery();
            cmd.CommandText = "update pid set pid='" + Autonumber() + "'";
            int k = cmd.ExecuteNonQuery();
            if (DropDownList5.Text == "Firstclass")
            {
                cmd.CommandText = "update schedule set Firstclass=Firstclass-1 where flightid='" + DropDownList1.SelectedValue + "'";
                int j = cmd.ExecuteNonQuery();
            }
            else if (DropDownList5.Text == "Bussinessclass")
            {
                cmd.CommandText = "update schedule set Bussinessclass=Bussinessclass-1 where flightid='" + DropDownList1.SelectedValue + "'";
                int j = cmd.ExecuteNonQuery();


            }
            else
            {
                cmd.CommandText = "update schedule set Economicclass=Economicclass-1 where flightid='" + DropDownList1.SelectedValue + "'";
                int j = cmd.ExecuteNonQuery();
            }
            if (i > 0)
            {
                Label.Visible = true;
                Label.Text = "success";
            }

            else
            {
                Label.Visible = true;
                Label.Text = "error";
            }

            Label16.Visible = true;
            Label16.Text = "Your Ticket ID is " + a;
            con.Close();
        }
        else
        {
            Label16.Visible = true;
            Label16.Text = "There is no flight with these details so please check flight schedule and submit your request";
        }

    }
    protected void SqlDataSource4_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {

    }
    protected void SqlDataSource5_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
    {

    }
}

【问题讨论】:

    标签: c# asp.net sql-server


    【解决方案1】:

    异常表示您正在插入重复的主键;可能 Pid 是这个主键。

    您可以做的是从插入中取出 Pid,然后 Pid 的值将来自数据库本身。通常,主键来自计数器。

    【讨论】:

      【解决方案2】:

      您正在尝试在主键字段中插入一个值。尝试为该列设置一个标识,以便 sql 自动创建没有重复的 id

      【讨论】:

        【解决方案3】:

        您正试图将重复的主键插入到表 Plist 中。主键必须是唯一的。我看起来您使用的这种自动编号方法不好。您可以更改 PID 列,使其成为标识列。使用标识列,sql server 将为您插入的每条记录提供一个唯一的序列号。

        SQL 身份 http://msdn.microsoft.com/en-us/library/ms186775.aspx

        【讨论】:

          【解决方案4】:

          您正在尝试插入重复的主键记录,这就是错误的原因。检查插入语句的主键值

          【讨论】:

            【解决方案5】:

            您要做的是首先检查现有记录,如果不存在,则添加新记录。您的代码将始终尝试添加新记录。

            if notexists(select id from tabname where id='yourid')
            
            begin
            
                -- Your insert statement
            
            end
            
            else
            
            begin
            
                return 0 -- for existing value
            
            end
            

            【讨论】:

              猜你喜欢
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 2012-05-21
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              • 1970-01-01
              相关资源
              最近更新 更多