单击okScheduleBtn
按钮时,gridview中的值未插入数据库表。
CodeBehind:
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
}
protected void okScheduleBtn_Click(object sender, EventArgs e)
{
{
try
{
using (SqlConnection conn = new SqlConnection(connstr1))
{
conn.Open();
for (int i = 0; i < ProofsDueGridView.Rows.Count; i++)
{
SqlCommand cmd = new SqlCommand("INSERT INTO [dbo].[abcd] ([aaa], [bbb], [ccc], [ddd], [eee], [fff], [ggg], [hhh], [iii) values (@[aaa], @[bbb], @[ccc], @[ddd], @[eee], @[fff], @[ggg], @[hhh], @[iii])", conn);
cmd.Parameters.Add("@[aaa]", SqlDbType.Int).Value = ProofsDueGridView.Rows[i].Cells[0];
cmd.Parameters.Add("[@bbb]", SqlDbType.Date).Value = ProofsDueGridView.Rows[i].Cells[1];
cmd.Parameters.Add("@[ccc]", SqlDbType.DateTime).Value = ProofsDueGridView.Rows[i].Cells[2];
cmd.Parameters.Add("@[ddd]", SqlDbType.Char).Value = ProofsDueGridView.Rows[i].Cells[3];
cmd.Parameters.Add("@[eee]", SqlDbType.VarChar).Value = ProofsDueGridView.Rows[i].Cells[4];
cmd.Parameters.Add("[@fff]", SqlDbType.VarChar).Value = ProofsDueGridView.Rows[i].Cells[5];
cmd.Parameters.Add("@[ggg]", SqlDbType.Char).Value = ProofsDueGridView.Rows[i].Cells[6];
cmd.Parameters.Add("@[hhh]", SqlDbType.Char).Value = ProofsDueGridView.Rows[i].Cells[7];
cmd.Parameters.Add("@[iii]", SqlDbType.Decimal).Value = ProofsDueGridView.Rows[i].Cells[8];
cmd.ExecuteNonQuery();
}
conn.Close();
}
//ProofsDueGridView.Rows.Clear();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}
}
ASPX:
<form id="form1" runat="server">
<table>
<tr>
<td class="tablefont">
<asp:GridView ID="ProofsDueGridView" runat="server" AllowSorting="True" Width="75%"
AutoGenerateColumns="False" DataKeyNames="Job No." DataSourceID="SqlDataSource1" OnSelectedIndexChanged="ProofsDueGridView_SelectedIndexChanged">
<Columns>
<asp:BoundField DataField="aaa" HeaderText="aaa" ReadOnly="True" SortExpression="aaa">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="bbb" HeaderText="bbb" ReadOnly="True" SortExpression="bbb">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="ccc" HeaderText="ccc" ReadOnly="True" SortExpression="ccc">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="ddd" HeaderText="ddd" SortExpression="ddd">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="eee" HeaderText="eee" SortExpression="eee">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="fff" HeaderText="fff" SortExpression="fff">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="ggg" HeaderText="ggg" SortExpression="ggg">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="hhh" HeaderText="hhh" SortExpression="hhh">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
<asp:BoundField DataField="iii" HeaderText="iii" SortExpression="iii">
<HeaderStyle HorizontalAlign="Left" />
<ItemStyle HorizontalAlign="Left" />
</asp:BoundField>
</Columns>
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:lllConnectionString %>"
SelectCommand="SELECT distinct hh AS[aaa], CONVERT(VARCHAR(10), bbb, 101) AS[bbb], CONVERT(VARCHAR(10), ccc, 101) AS[ccc], ll AS[ddd], oo AS [eee], cc AS [fff], ss AS [ggg], qq AS [hhh], dd AS [iii] FROM ppp )"></asp:SqlDataSource>
<asp:Button ID="okScheduleBtn" runat="server" Text="OK TO SCHEDULE" />
</td>
</tr>
</table>
</form>
为什么值没有插入数据库?