2016-03-26 72 views
0

我試圖插入到使用存儲過程的表中。執行的存儲產品結果爲零(0)。我猜這意味着存儲過程是成功的。插入到存儲過程不工作

但是當我查看錶格時,不添加記錄。我該如何解決它?

SQL

ALTER PROCEDURE displayToDO 
    @ID int =2, 
    @Title varchar(50), 
    @Description varchar(255), 
    @Date date, 
    @Time time, 
    @Location varchar(50), 
    @Priority varchar(20), 
    @NotificationType varchar(30) 
AS 
BEGIN 
    INSERT INTO toDOLIST(ID, Title, Description, Date, Time, Location, Priority, NotificationTYPE) 
    VALUES (@Title, @Description, @Date, @Time, @Location, @Priority, @NotificationType) 
END 

Asp.net:

<form id="form1" runat="server"> 
<div> 
    <div class="insertData"> 
     <asp:Label ID="title" runat="server" Text="Title"></asp:Label> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:TextBox ID="titleBox" runat="server"></asp:TextBox><br /> 

     <asp:Label ID="description" runat="server" Text="Description"></asp:Label> 
     &nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:TextBox ID="descriptionBox" runat="server"></asp:TextBox><br /> 

     <asp:Label ID="dateLabel" runat="server" Text="Date"></asp:Label> 
     <asp:Calendar ID="date" runat="server"></asp:Calendar><br /> 

     <asp:Label ID="time" runat="server" Text="Time"></asp:Label> 
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:TextBox ID="timeBoxHr" runat="server"></asp:TextBox>&nbsp; <asp:TextBox ID="timeBoxMin" runat="server"></asp:TextBox> 
     <br /> 

     <asp:Label ID="location" runat="server" Text="Location"></asp:Label> 
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:TextBox ID="locationBox" runat="server"></asp:TextBox><br /> 

     <asp:Label ID="priority" runat="server" Text="Priority"></asp:Label>  
     &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;  
     <asp:DropDownList ID="priorityDrop" runat="server"> 
      <asp:ListItem>-</asp:ListItem> 
      <asp:ListItem>Yes</asp:ListItem> 
      <asp:ListItem>No</asp:ListItem> 
     </asp:DropDownList> 
     <br /> 
     <asp:Label ID="notification" runat="server" Text="Notification Type"></asp:Label>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 
     <asp:DropDownList ID="notificationDrop" runat="server"> 
      <asp:ListItem>12 hrs before</asp:ListItem> 
      <asp:ListItem>1 day Before</asp:ListItem> 
      <asp:ListItem>3 Day before</asp:ListItem> 
      <asp:ListItem>A Week before</asp:ListItem> 
     </asp:DropDownList> 

    </div> 
    <br /> 
    <asp:Button ID="Button2" runat="server" Text="Insert" onclick="Button2_Click" /> 
    <br /><br /><br /><br /> 
    <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Display" /> 
    <br /><br /><br /><br /> 
    <asp:GridView ID="GridView1" runat="server" 
     onselectedindexchanged="GridView1_SelectedIndexChanged"> 
    </asp:GridView> 
</div> 
</form> 

C#:

protected void Button2_Click(object sender, EventArgs e) 
    { 
     String time = timeBoxHr.Text + ":" + timeBoxMin.Text + ":00"; 
     using (SqlConnection conn = new SqlConnection(ConfigurationString)) 
     { 

      SqlCommand scommand = new SqlCommand(sql, conn); 
      scommand.CommandType = CommandType.StoredProcedure; 
      scommand.Parameters.Add("@ID", SqlDbType.Int).Value = titleBox.Text; 
      scommand.Parameters.Add("@Title", SqlDbType.VarChar, 50).Value = titleBox.Text; 
      scommand.Parameters.Add("@Description", SqlDbType.VarChar, 255).Value = descriptionBox.Text; 
      scommand.Parameters.Add("@Date", SqlDbType.Date).Value = date.SelectedDate.ToShortDateString(); 
      scommand.Parameters.Add("@Time", SqlDbType.Time).Value = time; 
      scommand.Parameters.Add("@Location", SqlDbType.VarChar, 50).Value = locationBox.Text; 
      scommand.Parameters.Add("@Priority", SqlDbType.VarChar, 20).Value = priorityDrop.Text; 
      scommand.Parameters.Add("@NotificationType", SqlDbType.VarChar, 30).Value = notificationDrop.Text; 
      try 
      { 
       if (scommand.Connection.State == ConnectionState.Closed) 
       { 
        scommand.Connection.Open(); 
       } 
       scommand.ExecuteNonQuery(); 
      } 
      catch (Exception) 
      { 
      } 
      finally 
      { 
       scommand.Connection.Close(); 
      } 
     } 
+0

我見非常專業的異常處理'catch(Exception){}'...爲什麼這個問題有java,android標籤? – Selvin

+0

如果你使用ASP.net和C#,你爲什麼要標記這個java和android?我已經改變了標籤 –

+0

也似乎我們在這裏有一個序列upvoter ...壞,壞的Adarash ... – Selvin

回答

0

我可以看到幾個問題,你可能想要的工作得到這個工作:

第一件事,第一個值0作爲ExecuteNonQuery回報意味着有0條插入,否則將給予插入的記錄數,因爲該過程不設置Set Nocount Off,現在要調試的實際問題:

  1. 通過Sql Server管理工作室運行該程序並檢查它是否有效,這將確保使用當前的一組值,程序正在工作,萬一您的主鍵不是標識或自動生成的,則刪除通過上述過程插入的行以避免異常

  2. 對於所有綁定的參數,請確保值的類型正確轉換/轉換,就像我看到控制拿起像文本框的值將被默認字符串,將其轉換爲整數,並分配給參數

檢查,如果這有助於在手解決問題

+0

關於文本框的類型:將字符串發送到一個整數列是可以的。唯一的規則是該字符串只能包含數字。所以,對於存儲過程5和「5」是一樣的 – Khazratbek