简体   繁体   中英

SqlException was unhandled by user code

i am working on an airline reservation system everything compiled nicely...a user is supposed to register to the site then check available flights after that the user can book a flight,but i am having problems when it comes to booking a flight the values can not be inserted in the database and there is an error popping up saying

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.

what is wrong or what am i doing that is not right this is my code and an exception is flashing on this code

int i = cmd.ExecuteNonQuery();

the full code

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)
    {

    }
}

The exception says you're inserting a duplicate primary key; probably Pid is this primary key.

What you can do is take Pid out of your insert, then the value for Pid will come from the database itself. Commonly, primary keys come from a counter.

您正尝试在主键字段中插入值。尝试为列设置标识,以便sql自动创建标识而不重复

You are attempting to insert a duplicate primary key record and that is the reason for the error. Check the value of the primary key for the insert statement

You are trying to insert a duplicate primary key into the table Plist. The primary key must be unique. I looks like this autonumber method you are using is no good. You can change the PID column so it is an identity column. With an identity column sql server will give you a unique sequential number for each record you insert.

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

What you want to do is first check for the existing record, and if it doesn't exist, then add a new one. Your code will always attempt to add a new record.

if notexists(select id from tabname where id='yourid')

begin

    -- Your insert statement

end

else

begin

    return 0 -- for existing value

end

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM