SQLDataSource使用存储过程进行插入

时间:2014-02-07 12:57:42

标签: asp.net sql vb.net

我使用下面的代码允许客户端在表格中插入新问题。我正在使用详细信息视图来允许插入数据,我已经在我的SQL数据库中创建了一个存储过程,应该被触发并允许将数据插入到数据库中,任何想法为什么程序没有将信息添加到数据库中表,

    <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

1 个答案:

答案 0 :(得分:0)

看起来像insertNewQuestionSub是在SQL数据源中调用的proc,并且从DB列出的proc是insertQuestion。你是否也有insertNewQuestionSub转到另一个表?这可能是问题