gridview问题

时间:2010-10-12 07:13:23

标签: c# asp.net gridview textbox

我有一个问题,我的gridview包含一个文本框来设置日期。

<asp:gridview ID="Gridview1" runat="server" AllowPaging="True" 
            AutoGenerateColumns="False" 
            DataSourceID="AccessDataSource1" CellPadding="8" ForeColor="#333333" 
            GridLines="None" CellSpacing="5" Height="361px" Width="748px">
        <AlternatingRowStyle BackColor="White" />
        <Columns>
            <asp:BoundField DataField="nome" HeaderText="Nome Utente" 
                SortExpression="nome" >
                <ItemStyle HorizontalAlign="Center" />
            </asp:BoundField>
            <asp:BoundField DataField="titolo" HeaderText="Titolo Libro" 
                SortExpression="titolo" >
                <ItemStyle HorizontalAlign="Center" /></asp:BoundField>
            <asp:BoundField DataField="Expr1" HeaderText="Data Restituzione Prevista" 
                ReadOnly="True" SortExpression="Expr1" >
                <ItemStyle HorizontalAlign="Center" /></asp:BoundField>
            <asp:TemplateField  
                HeaderText="Data Restituzione" SortExpression="Expr2">
                <EditItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Eval("Expr2") %>'></asp:Label>
                </EditItemTemplate> 
                <ItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Eval("Expr2") %>' 
                        AutoPostBack="True" ontextchanged="TextBox1_TextChanged"></asp:TextBox>

                </ItemTemplate>
                <ItemStyle HorizontalAlign="Center" />
            </asp:TemplateField>
        </Columns>

        <FooterStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <HeaderStyle BackColor="#990000" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#FFCC66" ForeColor="#333333" HorizontalAlign="Center" />
        <RowStyle BackColor="#FFFBD6" ForeColor="#333333" />
        <SelectedRowStyle BackColor="#FFCC66" Font-Bold="True" ForeColor="Navy" />
        <SortedAscendingCellStyle BackColor="#FDF5AC" />
        <SortedAscendingHeaderStyle BackColor="#4D0000" />
        <SortedDescendingCellStyle BackColor="#FCF6C0" />
        <SortedDescendingHeaderStyle BackColor="#820000" />
        </asp:gridview>

这是我的asp.cs文件:

命名空间Utenti_Biblio {     public partial class prestiti:System.Web.UI.Page     {         bool changed = false;

    protected void Page_Load(object sender, EventArgs e)
    {
        selectRow();
    }

    public void selectRow()
    {
        foreach (GridViewRow row in this.Gridview1.Rows)
        {
            TextBox textBox = (TextBox)row.Cells[3].FindControl("TextBox1");
            string a = textBox.Text;
            if (a != "")
            {
                row.Cells[3].FindControl("TextBox1").Visible = false;
            }
        }
    }

    protected void Button2_Click(object sender, EventArgs e)
    {
        Response.Redirect("Default.aspx");
    }

    protected void TextBox1_TextChanged(object sender, EventArgs e)
    {
        changed = true;
    }

    // salva
    protected void Button1_Click(object sender, EventArgs e)
    {
        if (changed)
        {
            foreach (GridViewRow row in this.Gridview1.Rows)
            {
                TextBox textBox = (TextBox)row.Cells[3].FindControl("TextBox1");
                string qry = "UPDATE b_prestiti SET data_restituzione ='" + textBox.Text + "' WHERE id_utente = '" + row.Cells[1].ToString() + "'";
                OleDbConnection conn = new OleDbConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);
                OleDbCommand cmd = conn.CreateCommand();
                OleDbDataReader reader = null;
                conn.Open();
                cmd.CommandText = qry;
                reader = cmd.ExecuteReader();
                ClientScript.RegisterStartupScript(this.GetType(), "conferma", "alert('Data Restituzione inserita!');window.location='Default.aspx';", true);
                reader.Close();

                conn.Close();
            }
        }

    }
}

}

当我将日期设置到文本框中时,如何更新数据库? 查询字符串不正确。谢谢!

2 个答案:

答案 0 :(得分:2)

而不是ExecuteReader()使用ExecuteNonQuery(),这可能会成功。

答案 1 :(得分:0)

GxG是对的。 ExecuteReader()用于获取单个记录(Column),而DML(Data Manipulation Language,如INSERT,DELETE,UPDATE)则使用ExecuteNonQuery()。