2015-11-23 34 views
0

我已經創建了一個連接到數據庫的網站,我可以獲取數據並將其顯示在頁面上,沒有任何問題。但是,當我嘗試插入(或更新)它什麼都不做。asp.net項目C#,SQL服務器,插入不工作

我測試了SQL查詢,它工作得很好。

我在這裏尋找類似的情況和問題,這裏沒有運氣在過去的24小時。

我想要的網站,告訴用戶是否希望單向或雙向門票和提出請求。該表具有自動遞增的請求ID 然後我添加請求的學生的ID,出發票的ID然後是返回票的ID(如果它是2種方式,該值可以是空的)那裏也是待處理的狀態,直到主管接受或拒絕請求,一旦接受,發行日期將被添加並且狀態將改變爲批准。如果拒絕原因將被添加並且狀態更改爲拒絕。

主要問題,當我提出的要求,則不會創建該行並添加到數據庫中的主管以後查看。

這裏是我的代碼:

protected void Button1_Click(object sender, EventArgs e) 
{ 
    int parsedValue; 
    int.TryParse(DropDownList1.SelectedValue, out parsedValue); 
    SqlConnection myConnection = new SqlConnection(""); // I removed the connection string. 
    string sqlcommand = ""; 
    string idString = TextBox1.Text; 
    string idTwoString =""; 
    bool canContune = false; 
    if (parsedValue == 1) 
    { 
     System.Diagnostics.Debug.WriteLine("p"); 
     Panel3.Visible = true; 
     idTwoString = TextBox2.Text; 
     if (AllNumber(idString, TextBox1) && AllNumber(idTwoString, TextBox2)) 
     { 
      canContune = true; 
     } 
    } 
    else if (AllNumber(idString, TextBox1)) 
    { 
     canContune = true; 
    } 
    if (canContune) 
    { 
     int dId; 
     int dId2; 
     int.TryParse(idString, out dId); 
     int.TryParse(idTwoString, out dId2); 
     sqlcommand = "INSERT INTO TicketRequest.dbo.TicketRequest (student_id, departure_id, return_id, statues, issue_date, notes) " 
      + "VALUES (@student_id, @departure_id , @return_id , @statues, @issue_date, @notes)"; 

     try 
     { 
      SqlCommand cmd = new SqlCommand(sqlcommand); 
      cmd.CommandType = CommandType.Text; 
      cmd.Connection = myConnection; 
      myConnection.Open(); 
      cmd.Parameters.Add("@student_id", SqlDbType.Int).Value = id; 
      cmd.Parameters.Add("departure_id", SqlDbType.Int).Value = dId; //I used AddWithValue(@para, value) it didn't work. 
      if (parsedValue == 0) 
      { 
       cmd.Parameters.AddWithValue("@return_id", DBNull.Value); 
      } 
      else 
      { 
       cmd.Parameters.Add("@return_id", SqlDbType.Int).Value = dId2; 
      } 
      cmd.Parameters.Add("@statues", SqlDbType.Text).Value = "Pending"; 
      cmd.Parameters.AddWithValue("@issue_date", DBNull.Value); 
      cmd.Parameters.AddWithValue("@notes", DBNull.Value); 
      cmd.ExecuteNonQuery(); 
      myConnection.Close(); 
     } 
     catch (Exception ex) 
     { 
      System.Diagnostics.Debug.WriteLine(ex.ToString()); 
     } 
    } 
}` 

它不拋出任何異常,我真的不知道什麼是錯的。 我會非常感謝任何會在Insert查詢中指出我的錯誤的人。提前致謝。

============================================== ====

I apologized all, it worked just fine. it seemed that the code wasn't excuted to being with. Thanks Falanor, you helped me discover the problem. =)

+1

不拋出異常或者它把它在地毯下通過寫入調試控制檯? –

+0

根本沒有在調試控制檯中。 – Crystal

+0

作爲完整性檢查,您確定它正在寫入正確的數據庫,並且您正在檢查相同的數據庫嗎? – user1666620

回答

4

嘗試檢查返回值。

int modified =(int)cmd.ExecuteScalar(); 

這也缺少@符號參數

cmd.Parameters.Add("departure_id", SqlDbType.Int).Value = dId; //I used AddWithValue(@para, value) it didn't work. 
+0

似乎我多次編輯後都忘記了@。 並猜測方式根本沒有返回值。似乎它不執行查詢開始。將重新檢查我的代碼。 – Crystal

+0

對此進行處理以獲得結果。之前應該包括這個。 'SELECT CAST(scope_identity()as int)' – Falanor