【发布时间】:2014-04-28 05:42:09
【问题描述】:
我有一个 gridview,其中包含列(Id、Task、Reward、Time Allotted、Uploader)..我可以成功地将数据插入到 gridview 中,直到第 13 页的 taskId 为 118,但之后数据开始显示在TaskId 7 之后的第 1 页。
所以序列就像 ....5,6,7,119,120,121,122,8,9,10..... 所以所有的新数据都在 7 和 8 之间插入。为什么会这样?我该如何纠正这个问题?
我不认为这是代码错误,但仍然是我的:
protected void btnPost_Click(object sender, EventArgs e)
{
string CS = ConfigurationManager.ConnectionStrings["EasyRozMoney_ConnectionString"].ConnectionString;
using (SqlConnection con = new SqlConnection(CS))
{
SqlCommand cmd = new SqlCommand("spTasks", con);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("@Title", txtTitle.Text);
cmd.Parameters.AddWithValue("@Body", txtBody.Text);
cmd.Parameters.AddWithValue("@Reward", txtRewards.Text);
cmd.Parameters.AddWithValue("@TimeAllotted", txtTime.Text);
cmd.Parameters.AddWithValue("@PosterName", txtPoster.Text);
con.Open();
cmd.ExecuteNonQuery();
lblStatus.Text = "Task Posted Successfully.";
}
这里是网格视图:
<asp:GridView ID="GridView1" runat="server" Width="936px" AllowPaging="True" AutoGenerateColumns="False" CellPadding="3" DataSourceID="SqlDataSource1" OnRowCommand="GridView1_RowCommand" style="text-align: center" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellSpacing="2" OnSelectedIndexChanged="GridView1_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="TaskId" HeaderText="TaskId" InsertVisible="False" ReadOnly="True" SortExpression="TaskId" />
<asp:BoundField DataField="Title" HeaderText="Title" SortExpression="Title" />
<asp:BoundField DataField="Body" HeaderText="Body" SortExpression="Body" Visible="false" />
<asp:BoundField DataField="Reward" HeaderText="Reward(Rs)" SortExpression="Reward" />
<asp:BoundField DataField="TimeAllotted" HeaderText="Time(Min)" SortExpression="TimeAllotted" />
<asp:BoundField DataField="PosterName" HeaderText="Uploader" SortExpression="PosterName" />
<asp:ButtonField ButtonType="Button" CommandName="Select" Text="Perform Task" ControlStyle-ForeColor="White" ControlStyle-Font-Bold="true">
<ControlStyle BackColor="#CC6600" Font-Bold="True" ForeColor="White"></ControlStyle>
</asp:ButtonField>
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#CC6600" Font-Bold="True" ForeColor="White" />
<PagerStyle ForeColor="#8C4510" HorizontalAlign="Center" />
<RowStyle BackColor="#FFF7E7" ForeColor="#8C4510" />
<SelectedRowStyle BackColor="#738A9C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#FFF1D4" />
<SortedAscendingHeaderStyle BackColor="#B95C30" />
<SortedDescendingCellStyle BackColor="#F1E5CE" />
<SortedDescendingHeaderStyle BackColor="#93451F" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:EasyRozMoney_ConnectionString %>" SelectCommand="SELECT * FROM [Task]"></asp:SqlDataSource>
我的sql表如下:
Create table Task
(
TaskId int Identity(1,1),
Title varchar(100),
Body varchar(500),
Reward decimal(4,2),
TimeAllotted int,
PosterName varchar(100)
)
我的存储过程:
Create proc spTasks
@Title varchar(100),
@Body varchar(500),
@Reward decimal(4,2),
@TimeAllotted int,
@PosterName varchar(100)
as
begin
Insert into Task values(@Title,@Body,@Reward,@TimeAllotted,@PosterName)
end
【问题讨论】:
-
你能分享你用来绑定你的gridview和数据的代码吗?
-
@DamienJoe 编辑了我的问题
-
使用任务 ID 数据类型 (int) 作为主键,我认为您必须在数据类型 varchar 中使用。
-
@NayeemMansoori 检查我编辑的问题。它清楚地指出我制作了一个带有 taskid 作为 int 数据类型的表