无法使用asp.net中的detailviews插入数据 [英] unable to insert data using detailviews in asp.net
本文介绍了无法使用asp.net中的detailviews插入数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想在asp.net中使用详细信息视图添加新记录所有代码运行良好但是添加了新记录,即id是表中的增量但没有输入数据为什么?我不知道,下面需要你的宝贵帮助是我的代码片段:
i want to add new record using details view in asp.net all the code runs well but at new record is added i.e id is increments in table but no data is entered why?? i do not know and need your valuable help below is my code snipt:
<asp:DetailsView ID="DetailsView1" runat="server" AllowPaging="True" AutoGenerateRows="False"
CellPadding="4" DataKeyNames="QuestionID" DataSourceID="SqlDataSource2" ForeColor="#333333"
GridLines="None" Height="50px" Width="100%"
oniteminserting="DetailsView1_ItemInserting"
onitemupdating="DetailsView1_ItemUpdating1">
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
<EditRowStyle BackColor="#999999" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<Fields>
<asp:BoundField DataField="QuestionID" HeaderText="QuestionID" InsertVisible="False"
ReadOnly="True" SortExpression="QuestionID" />
<asp:TemplateField HeaderText="Question" SortExpression="Question">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Questiontext") %>' TextMode="MultiLine"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("Questiontext") %>' TextMode="MultiLine"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label2" runat="server" Text='<%# Bind("Questiontext") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="Helptext" SortExpression="Helptext">
<EditItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Helptext") %>' TextMode="MultiLine"></asp:TextBox>
</EditItemTemplate>
<InsertItemTemplate>
<asp:TextBox ID="TextBox2" runat="server" Text='<%# Bind("Helptext") %>' TextMode="MultiLine"></asp:TextBox>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label3" runat="server" Text='<%# Bind("Helptext") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField HeaderText="AnswerType" SortExpression="AnswerType">
<EditItemTemplate>
<asp:DropDownList ID="DropDownList3" runat="server" SelectedValue='<%# Bind("Answertype") %>'>
<asp:ListItem Selected="True" Value="S">Single Choice</asp:ListItem>
<asp:ListItem Value="M">Multiple Choices</asp:ListItem>
<asp:ListItem Value="T">Text</asp:ListItem>
</asp:DropDownList>
</EditItemTemplate>
<InsertItemTemplate>
<asp:DropDownList ID="DropDownList2" runat="server" SelectedValue='<%# Bind("Answertype") %>'>
and in sqldatasource:
<asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
DeleteCommand="DELETE FROM [Questions] WHERE [QuestionID] = @QuestionID"
InsertCommand="INSERT INTO [Questions] ([Questiontext],[Helptext],[Surveyid],[Answertype]) VALUES (@qt, @ht, @sid,@atype)"
<asp:ListItem Value="S">Single Choice</asp:ListItem>
<asp:ListItem Value="M">Multiple Choices</asp:ListItem>
<asp:ListItem Value="T">Text</asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID="Label1" runat="server" Text='<%# Bind("Answertype") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" />
</Fields>
</asp:DetailsView>
>
>
<InsertParameters>
<asp:Parameter Name="qt" Type="String" />
<asp:QueryStringParameter Name="sid" Type="Int32" QueryStringField="SurveyId"/>
<asp:Parameter Name="ht" Type="String"/>
<asp:Parameter Name="atype" Type="String"/>
</InsertParameters>
please guide me to the error and a way to solve it!!!! many thnks in advance
please guide me to the error and a way to solve it!!!! many thnks in advance
推荐答案
ConnectionStrings:ConnectionString %>\"
DeleteCommand=\"DELETE FROM [Questions] WHERE [QuestionID] = @QuestionID\"
InsertCommand=\"INSERT INTO [Questions] ([Questiontext],[Helptext],[Surveyid],[Answertype]) VALUES (@qt, @ht, @sid,@atype)\"
< span class=\"code-attribute\"> <asp:ListItem Value=\"S\">Single Choice</asp:ListItem>
<asp:ListItem Value=\"M\">Multiple Choices</asp:ListItem>
<asp:ListItem Value=\"T\">Text</asp:ListItem>
</asp:DropDownList>
</InsertItemTemplate>
<ItemTemplate>
<asp:Label ID=\"Label1\" runat=\"server\" Text='<%# Bind(\"Answertype\") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:CommandField ShowDeleteButton=\"True\" ShowEditButton=\"True\" ShowInsertButton=\"True\" />
</Fields>
</asp:DetailsView>
ConnectionStrings:ConnectionString %>" DeleteCommand="DELETE FROM [Questions] WHERE [QuestionID] = @QuestionID" InsertCommand="INSERT INTO [Questions] ([Questiontext],[Helptext],[Surveyid],[Answertype]) VALUES (@qt, @ht, @sid,@atype)" <asp:ListItem Value="S">Single Choice</asp:ListItem> <asp:ListItem Value="M">Multiple Choices</asp:ListItem> <asp:ListItem Value="T">Text</asp:ListItem> </asp:DropDownList> </InsertItemTemplate> <ItemTemplate> <asp:Label ID="Label1" runat="server" Text='<%# Bind("Answertype") %>'></asp:Label> </ItemTemplate> </asp:TemplateField> <asp:CommandField ShowDeleteButton="True" ShowEditButton="True" ShowInsertButton="True" /> </Fields> </asp:DetailsView>
>
>
<InsertParameters>
<asp:Parameter Name="qt" Type="String" />
<asp:QueryStringParameter Name="sid" Type="Int32" QueryStringField="SurveyId"/>
<asp:Parameter Name="ht" Type="String"/>
<asp:Parameter Name="atype" Type="String"/>
</InsertParameters>
please guide me to the error and a way to solve it!!!! many thnks in advance
please guide me to the error and a way to solve it!!!! many thnks in advance
You should fire event \"inserting\" on sqldatasource.
Go to properties of Sqldatasource2, click on Inserting event,
In .cs page,
You should fire event "inserting" on sqldatasource.
Go to properties of Sqldatasource2, click on Inserting event,
In .cs page,
protected void SqlDataSource1_Inserting(object sender, SqlDataSourceCommandEventArgs e)
{
SqlParameter insertedKey = new SqlParameter("@QuestionID", SqlDbType.Int);
insertedKey.Direction = ParameterDirection.Output;
e.Command.Parameters.Add(insertedKey);
}
这篇关于无法使用asp.net中的detailviews插入数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文