2014-02-07 164 views
0

我使用下面的代碼來允許客戶端向表中插入新問題。我正在使用詳細信息視圖來允許插入數據,我已經在我的SQL數據庫中創建了一個存儲過程,該存儲過程應該被觸發並允許將數據插入到數據庫中,爲什麼程序沒有將信息添加到數據庫中表,使用存儲過程插入的SQLDataSource

<asp:DetailsView ID="DetailsView1" runat="server" DefaultMode="Insert" 
     AutoGenerateRows="False" DataSourceID="Question" DataKeyNames="questionID"> 
     <CommandRowStyle BackColor="#D1DDF1" /> 
     <EditRowStyle BackColor="#F1F1F2" /> 
     <FieldHeaderStyle BackColor="#546E96" BorderColor="#465767" ForeColor="White" /> 

     <Fields> 
      <asp:TemplateField HeaderText="QuestionNo" SortExpression="QuestionNo"> 
       <EditItemTemplate> 
        <asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("QuestionNo") %>'></asp:TextBox> 
       </EditItemTemplate> 
       <InsertItemTemplate> 
        <asp:ListBox ID="ListBox2" runat="server" DataSourceID="SqlAvailableQuestionNumbers" 
         DataTextField="questionno" DataValueField="questionno" Rows="1" SelectedValue='<%# Bind("QuestionNo") %>' 
         Width="75px"></asp:ListBox> 
       </InsertItemTemplate> 
       <ItemTemplate> 
        <asp:Label ID="Label2" runat="server" Text='<%# Bind("QuestionNo") %>'></asp:Label> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="QuestionType" SortExpression="QuestionType"> 
       <EditItemTemplate> 
        <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("QuestionType") %>'></asp:TextBox> 
       </EditItemTemplate> 
       <InsertItemTemplate> 
        <asp:ListBox ID="ListBox3" runat="server" DataSourceID="LinqGetQuestionTypes" DataTextField="name" 
         DataValueField="questionTypeID" Rows="1" SelectedValue='<%# Bind("QuestionType") %>'> 
        </asp:ListBox> 
       </InsertItemTemplate> 
       <ItemTemplate> 
        <asp:Label ID="Label3" runat="server" Text='<%# Bind("QuestionType") %>'></asp:Label> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:TemplateField HeaderText="question" SortExpression="question"> 
       <EditItemTemplate> 
        <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("question") %>'></asp:TextBox> 
       </EditItemTemplate> 
       <InsertItemTemplate> 
        <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("question") %>' Rows="6" 
         TextMode="MultiLine"></asp:TextBox> 
       </InsertItemTemplate> 
       <ItemTemplate> 
        <asp:Label ID="Label4" runat="server" Text='<%# Bind("question") %>'></asp:Label> 
       </ItemTemplate> 
      </asp:TemplateField> 
      <asp:CommandField ShowInsertButton="True" /> 
     </Fields> 
     <HeaderStyle BackColor="#546E96" ForeColor="White" /> 
     <RowStyle BackColor="#F1F1F2" /> 
    </asp:DetailsView> 
    <asp:DropShadowExtender ID="DetailsView1_DropShadowExtender" runat="server" 
    Enabled="True" TargetControlID="DetailsView1"> 
</asp:DropShadowExtender> 


    <asp:SqlDataSource ID="Question" runat="server" 
     ConnectionString="<%$ ConnectionStrings:DBConnectionString %>" 
     InsertCommand="insertNewQuestionSub" InsertCommandType="StoredProcedure" SelectCommand="select 
[QuestionType] 
     ,[QuestionNo] 
     ,[question] 
     from Questions"> 
     <InsertParameters> 
      <asp:Parameter Name="QuestionType" Type="Int32" /> 
      <asp:Parameter Name="QuestionNo" Type="Int32" /> 
      <asp:Parameter Name="question" Type="String" /> 

     </InsertParameters> 
    </asp:SqlDataSource> 

存儲過程

ALTER PROCEDURE [dbo].[insertQuestion] 
    -- Add the parameters for the stored procedure here 

    @QuestionType int, 
    @QuestionNo int, 
    @question nvarchar 
AS 
BEGIN 
    -- SET NOCOUNT ON added to prevent extra result sets from 
    -- interfering with SELECT statements. 
    SET NOCOUNT ON; 

    -- Insert statements for procedure here 
    INSERT INTO Questions (QuestionType,QuestionNo,question) 
    VALUES (@QuestionType,@QuestionNo,@question)) 
END 
+0

你有什麼錯誤嗎?在asp.net代碼或SQL中當你嘗試手動執行SP? – 2014-02-07 13:26:54

+0

我沒有得到任何錯誤代碼只是將空行添加到表中 – user3086751

+0

當您運行sql profiler時,是否看到sp執行?我強烈建議將SQL查詢放在代碼後面。分離顯示和業務邏輯的許多原因,並且通過放置中斷點更容易調試 – Feuerwehrmann

回答

0

貌似insertNewQuestionSub是叫你的SQL數據源中的PROC,並從數據庫中列出的proc已經insertQuestion。你也有insertNewQuestionSub去不同的表?這可能是問題