views:

41

answers:

2

Hi guys, i have a problem with my gridview contains a textbox to set a date.

<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>

And it is my asp.cs file:

namespace 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();
            }
        }

    }
}

}

How i do to update the database when i set the date into my textbox? The query string is not correct. Thanks!

+2  A: 

Instead of the ExecuteReader() use ExecuteNonQuery(), that might do the trick.

GxG
the error is "Object reference not set to an instance of an object."
at what line in the code?
GxG
the error is in the string qry!
try using this instead of what you used TextBox textBox = (TextBox)row.Cells[3].FindControl(TextBox1.ClientID); But the control TextBox1 must have a runat="server" attribute
GxG
ok but the error is in the where -> ("' WHERE id_utente = '" + row.Cells[1].ToString() + "'";)
id_utente is the first field in the row of the gridview.
then that should be ...Cells[0].Value.ToString(). C# is a 0 based index language, which means every counter starts at 0(the first column in a row has the index 0, the second has the index 1, etc.)
GxG
ok ok thanks but the problem is the row selected in the gridview..i don't know how select the row in the gridview and then get the value of cell[0] and cell[3]..
Well there should be an event on the grid that allows to declare a procedure to handle row selection. In the designer, right click on the grid and select properties, in the properties window there is an icon with a lighting bolt on it, click that and search through the events, and double click on the black box next to SelectedRowChanged i think the event is called. it should auto generate an event handler in the cs file. there use the variable "e" like: e.Cells... or something similar
GxG
A: 

GxG is right. ExecuteReader() is use to fetch the single record(Column) while to DML(Data Manipulation Language, like INSERT,DELETE,UPDATE) we use ExecuteNonQuery().

KhanZeeshan
actually the reader brings all the lines a Select might return, and then you loop through the reader to get a single record, the columns are in the definition of the reader. to get a single value you may use ExecuteScalar() which brings only the returning value of a procedure, if you have an out parameter. :)
GxG
yes yes...right once again :), thanks for that i forgot that.
KhanZeeshan
If you want to access the first cell in grid then you should use "0" in Cells Array like ;row.Cells[0].ToString()
KhanZeeshan
that would only return a string something like "System.DataGrid.CellObject", or something similar, to access the displayed text you need to use row.Cells[0].Text, or to access the value row.Cells[0].Value.ToString()
GxG
@GxG thats common sense :), dont you think.
KhanZeeshan