2017-11-16 34 views
0

私はスケジュールを含めて最初に選択するためのリストセミナーを作成した支払いセクションを介して、自分のWebサイトに外部キーを追加する方法を知りたいと思います。その後、ユーザーは登録をクリックし、ページをクレジットカードまたは現金を含む支払詳細フォームにリダイレクトすることができる。ユーザーが支払いを行い、確認ボタンをクリックすると、IDの主キー(自動生成)を含むデータベースが1つ増えます。ASP.NETでSQL Serverに外部キーを追加する方法C#

私の問題は、IDが-1であるために-1でもないID(データベースがSQL Serverで更新されていないことを確認しました)です。これは、あなたが追加する必要がありますあなたの答え ために、しかし、それを行うための良い方法ではありません

public static int purchaseSeminar(ShoppingCart sc) 
{ 
    int id = -1; 
    SqlConnection con = new SqlConnection(conStr); 

    try 
    { 
     SqlCommand command = new SqlCommand(); 
     command.Connection = con; 
     command.CommandText = "insert into Payment (payment_id, payment_name, payment_ccNo, payment_ccType, payment_ccCode, payment_expDate, payment_price, payment_optionPay, payment_date, reg_id) values (@payment_id, @payment_name, @payment_ccNo, @payment_ccType, @payment_ccCode, @payment_expDate, @payment_price, @payment_optionPay, @payment_date, @reg_id)"; 
     command.Parameters.AddWithValue("@payment_id", sc.Id); 
     command.Parameters.AddWithValue("@payment_ccNo", sc.CreditCard); 
     command.Parameters.AddWithValue("@payment_ccType", sc.CreditCardType); 
     command.Parameters.AddWithValue("@payment_ccCode", sc.SecurityCode); 
     command.Parameters.AddWithValue("@payment_expDate", sc.CCExpiryDate); 
     command.Parameters.AddWithValue("@payment_price", sc.TotalAmount); 
     command.Parameters.AddWithValue("@payment_optionPay", sc.OptionPay); 
     command.Parameters.AddWithValue("@reg_id", sc.Registration.RegId); 

     DateTime da = DateTime.Now; 
     command.Parameters.AddWithValue("@payment_date", da); 

     con.Open(); 

     command.CommandText = "SET IDENTITY_INSERT Payment ON"; 

     if (command.ExecuteNonQuery() > 0) 
     { 
      command.CommandText = "Select @@identity"; 
      id = Convert.ToInt32(command.ExecuteScalar()); 
     } 

     return id; 
    } 
    finally 
    { 
     con.Close(); 
    } 
} 
+0

"insert identity_insert ..."を実行するだけですので、 "set identity_insert ..."を実行してください。 –

答えて

0

でなければなりません。次に、その(sc.Id)をデータベースに挿入すると、NULL値が挿入されます。したがって、データベースからクエリを実行し、それをint32値に変換すると、 '-1'が返されます。

これは、コードの構造に伴う問題の原因と考えられます。

あなたのIDは自動生成されるため、insertコマンドから 'payment_ID'を除外してください。例:

command.CommandText = "insert into Payment (payment_name, payment_ccNo, 
         payment_ccType, payment_ccCode, payment_expDate, payment_price, 
         payment_optionPay, payment_date, reg_id) 
       values (@payment_name, @payment_ccNo, @payment_ccType, @payment_ccCode, 
         @payment_expDate, @payment_price, @payment_optionPay, 
         @payment_date, @reg_id)"; 

command.Parameters.AddWithValue("@payment_ccNo", sc.CreditCard); 
command.Parameters.AddWithValue("@payment_ccType", sc.CreditCardType); 
command.Parameters.AddWithValue("@payment_ccType", sc.CreditCardType); 
command.Parameters.AddWithValue("@payment_ccCode", sc.SecurityCode); 
command.Parameters.AddWithValue("@payment_expDate", sc.CCExpiryDate); 
command.Parameters.AddWithValue("@payment_price", sc.TotalAmount); 
command.Parameters.AddWithValue("@payment_optionPay", sc.OptionPay); 
command.Parameters.AddWithValue("@reg_id", sc.Registration.RegId); 

IDカラムとして、 'payment_ID'は値を期待しないため、insert文から除外します。そうすれば、IDENTITY INSERTをオンにする必要はありません。だからあなたもコードからそれらのステートメントを削除します。

+0

id is自動生成するので、ユーザーはIDを取得できません。 –

+0

次に、その列の入力があってはなりません。新しいSqlCommand( "INSERT INTO Photo VALUES(@ StaffId、@ StaffPhoto、@指紋)"、SQL);ここで、PhotoIdは自動生成されたプライマリキーなので、insertコマンドには含めませんでした。 IDENTITY INSERTをオンにする必要はありません。 – Shully

1

:ここ

は私のコードは私の支払いのデータベース・マネージャー・コードの支払いセクション

if (tbxName.Text != null && tbxCC.Text != null && ddlCCType.Text != null && tbxExpDate.Text != null) 
{ 
     ShoppingCart sc = (ShoppingCart)Session["cart"]; 
     sc.Name = tbxName.Text; 

     if (rdbCash.Checked == true) 
     { 
      sc.OptionPay = rdbCash.Text; 
     } 
     else 
     { 
      sc.OptionPay = rdbCC.Text; 
     } 

     sc.CreditCard = tbxCC.Text; 
     sc.CreditCardType = ddlCCType.Text; 
     sc.SecurityCode = tbxCode.Text; 
     sc.CCExpiryDate = tbxExpDate.Text; 
     //sc.Registration.RegId = sc.Id; 

     int id = ShoppingCartDBMgr.purchaseSeminar(sc); 

     lblOutput.Text = "Confirm. order id is " + id; 
     //display output for payment successfully 
     //lblOutput.Text = "Payment Successfully!"; 
     //make it null for amount, date and session of cart after transaction are being successful 
     lblAmount.Text = null; 
     lblDate.Text = null; 
     Session["cart"] = null; 

用についてです

そして、これはsc.Idが他の人(例:sc.Name)とは異なり、値が割り当てられている場合、私は見ていないあなたの最後の行

command.CommandText = "SET IDENTITY_INSERT Payment OFF"; 
+0

ok、私のコードを編集して、最初の行にIDを設定してください。最後の行にIDを設定してください。 –

関連する問題