我正在研究航空公司预订系统所有编译得很好...一个用户应该注册到该网站,然后检查可用的航班,之后用户可以预订航班,但我在预订时遇到问题飞行值不能插入数据库中,并且弹出错误
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)
{
}
}
答案 0 :(得分:2)
例外情况表示您正在插入重复的主键;可能Pid是这个主键。
你可以做的是从你的插入中取出Pid,然后Pid的值将来自数据库本身。通常,主键来自柜台。
答案 1 :(得分:2)
您正尝试在主键字段中插入值。尝试为列设置标识,以便sql自动创建id而不会重复
答案 2 :(得分:1)
您正在尝试插入重复的主键记录,这就是错误的原因。检查插入语句的主键值
答案 3 :(得分:1)
您正尝试将重复的主键插入表Plist中。主键必须是唯一的。我看起来像你使用的这种自动编号方法并不好。您可以更改PID列,使其成为标识列。使用标识列,sql server将为您插入的每条记录提供唯一的序列号。
答案 4 :(得分:1)
您要做的是首先检查现有记录,如果它不存在,则添加一个新记录。您的代码将始终尝试添加新记录。
if notexists(select id from tabname where id='yourid')
begin
-- Your insert statement
end
else
begin
return 0 -- for existing value
end