GridView row editing - c#

I have a gridview with five BoundFields.when inserting values to the grid i have got textbox fields to the cells and it's working fine.my issue is when editing a row i only need 3 cells to be editable and other two cells should left read only.how can i achieve this?
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False"
CellPadding="4" ForeColor="#333333" GridLines="None" BackColor="WhiteSmoke" CssClass="grid-view"
AlternatingRowStyle-CssClass="alt" PageSize="8"
onrowcancelingedit="GridView1_RowCancelingEdit"
onrowdeleting="GridView1_RowDeleting" onrowediting="GridView1_RowEditing"
onrowupdating="GridView1_RowUpdating"
onrowdatabound="GridView1_RowDataBound"
onrowcommand="GridView1_RowCommand" onrowcreated="GridView1_RowCreated">
<RowStyle BackColor="#EFF3FB" />
<PagerSettings PageButtonCount="5" />
<Columns>
<asp:TemplateField>
<HeaderTemplate>
<asp:LinkButton ID="btnInsert" runat="server"
Text="Insert" CommandName="Insert" ForeColor="White" />
</HeaderTemplate>
</asp:TemplateField>
<asp:CommandField HeaderText="Edit-Update" ShowEditButton="True" />
<asp:BoundField DataField="DeptID" HeaderText="Department ID"
ReadOnly="True" />
<asp:BoundField DataField="DeptCode" HeaderText="Department Code"/>
<asp:BoundField DataField="DeptDescription" HeaderText="Description" />
<asp:CommandField HeaderText="Delete" ShowDeleteButton="True" />
</Columns>
<PagerStyle CssClass="pgr"></PagerStyle>
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#2461BF" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView>
Code for inserting values to grid:
protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Insert")
{
DataTable DT = new DataTable();
DT = PinCDAO.GetDepartments();
DataRow DR = DT.NewRow();
DT.Rows.InsertAt(DR, 0);
GridView1.EditIndex = 0;
GridView1.DataSource = DT;
GridView1.DataBind();
((LinkButton)GridView1.Rows[0].Cells[1].Controls[0]).Text = "Insert";
}
}
Row уditing code:
GridView1.EditIndex = e.NewEditIndex;
Inserting and updating:
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
if (GetDeptCode())
{
lblMessage.Text = "Already Contains a Department Code with the Same Name";
}
else if (CheckCodeLength())
{
lblMessage.Text = "Department Code Character Length is 3";
}
else
{
if (((LinkButton)GridView1.Rows[0].Cells[1].Controls[0]).Text == "Insert")
{
DepertmentProperties DP = new DepertmentProperties();
DP.DeptCode = ((TextBox)GridView1.Rows[0].Cells[3].Controls[0]).Text.ToString();
DP.DeptDescription = ((TextBox)GridView1.Rows[0].Cells[4].Controls[0]).Text.ToString();
PinCDAO.InsertDepartment(DP);
}
else
{
DepertmentProperties DP = new DepertmentProperties();
DP.DeptID = Convert.ToInt32(GridView1.Rows[e.RowIndex].Cells[2].Text.ToString());
DP.DeptCode = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text.ToString();
DP.DeptDescription = ((TextBox)GridView1.Rows[e.RowIndex].Cells[4].Controls[0]).Text.ToString();
PinCDAO.UpdateDepartment(DP);
}
GridView1.EditIndex = -1;
BindData();
lblMessage.Text = "";
}
}

Set BoundField's ReadOnly property to "true"

Related

Editing a Bound Gridview Error

I'm implementing an "edit" feature on my Gridview bound to my "books" table. I'm receiving an error on RowEditingEvent wasn't handled. I don't understand where I'm going wrong or if I'm missing a reference somewhere but it seems to me that everything is handled.
public partial class AddEdit_Text_Books : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!this.IsPostBack)
{
DataTable dt = new DataTable();
string query = "select * from textBooks ";
using (SqlConnection connection = new SqlConnection(ConfigurationManager.ConnectionStrings["HUTDMSConnectionString"].ToString()))
using (SqlDataAdapter adapter = new SqlDataAdapter(query, connection))
adapter.Fill(dt);
ViewState["allBooks"] = dt;
this.BindGrid();
}
}
protected void BindGrid()
{
GridView1.DataSource = ViewState["allBooks"] as DataTable;
GridView1.DataBind();
}
protected void OnRowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
this.BindGrid();
}
protected void OnUpdate(object sender, EventArgs e)
{
GridViewRow row = (sender as LinkButton).NamingContainer as GridViewRow;
string name = (row.Cells[0].Controls[0] as TextBox).Text;
string country = (row.Cells[1].Controls[0] as TextBox).Text;
DataTable dt = ViewState["allBooks"] as DataTable;
ViewState["allBooks"] = dt;
GridView1.EditIndex = -1;
this.BindGrid();
}
protected void OnCancel(object sender, EventArgs e)
{
GridView1.EditIndex = -1;
this.BindGrid();
}
}
}
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AutoGenerateColumns="False" DataKeyNames="BookID" Width="1482px" CellPadding="4" ForeColor="#333333" GridLines="None">
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
<Columns>
<asp:BoundField DataField="thirteenISBN" HeaderText="thirteenISBN" SortExpression="thirteenISBN" />
<asp:BoundField DataField="CourseID" HeaderText="CourseID" SortExpression="CourseID" />
<asp:BoundField DataField="BookTitle" HeaderText="BookTitle" SortExpression="BookTitle" />
<asp:BoundField DataField="Ancillary" HeaderText="Ancillary" SortExpression="Ancillary" />
<asp:BoundField DataField="BookActive" HeaderText="BookActive" SortExpression="BookActive" />
<asp:BoundField DataField="ActiveDate" HeaderText="ActiveDate" SortExpression="ActiveDate" />
<asp:BoundField DataField="InactiveDate" HeaderText="InactiveDate" SortExpression="InactiveDate" />
<asp:BoundField DataField="Author" HeaderText="Author" SortExpression="Author" />
<asp:BoundField DataField="BookID" HeaderText="BookID" InsertVisible="False" ReadOnly="True" SortExpression="BookID" />
<asp:BoundField DataField="Notes" HeaderText="Notes" SortExpression="Notes" />
<asp:BoundField DataField="eISBN" HeaderText="eISBN" SortExpression="eISBN" />
<asp:BoundField DataField="ebookAvailable" HeaderText="ebookAvailable" SortExpression="ebookAvailable" />
<asp:BoundField DataField="VendorISBN" HeaderText="VendorISBN" SortExpression="VendorISBN" />
<asp:BoundField DataField="tenISBN" HeaderText="tenISBN" SortExpression="tenISBN" />
<asp:BoundField DataField="EditionAndDate" HeaderText="EditionAndDate" SortExpression="EditionAndDate" />
<asp:BoundField DataField="Publisher" HeaderText="Publisher" SortExpression="Publisher" />
<asp:BoundField DataField="Imprint" HeaderText="Imprint" SortExpression="Imprint" />
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton Text="Edit" runat="server" CommandName="Edit" />
</ItemTemplate>
<EditItemTemplate>
<asp:LinkButton Text="Update" runat="server" OnClick="OnUpdate" />
<asp:LinkButton Text="Cancel" runat="server" OnClick="OnCancel" />
</EditItemTemplate>
</asp:TemplateField>
</Columns>
<EditRowStyle BackColor="#999999" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<SortedAscendingCellStyle BackColor="#E9E7E2" />
<SortedAscendingHeaderStyle BackColor="#506C8C" />
<SortedDescendingCellStyle BackColor="#FFFDF8" />
<SortedDescendingHeaderStyle BackColor="#6F8DAE" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:HUTDMSConnectionString %>" SelectCommand="SELECT [thirteenISBN], [CourseID], [BookTitle], [Ancillary], [BookActive], [ActiveDate], [InactiveDate], [Author], [BookID], [Notes], [eISBN], [ebookAvailable], [VendorISBN], [tenISBN], [EditionAndDate], [Publisher], [Imprint] FROM [textBooks]"></asp:SqlDataSource>
You have not registered onediting event in gridview definition.
Add OnRowEditing ="OnRowEditing" in below gridview definition.
<asp:GridView ID="GridView1" runat="server" AllowPaging="True"
AutoGenerateColumns="False" DataKeyNames="BookID" Width="1482px"
CellPadding="4" ForeColor="#333333" GridLines="None" OnRowEditing ="OnRowEditing">

How can remove/replace substring from GridView Column?

I have below GridView:
I want to remove 0:00:00 from date column, if exists. I change dataformatstring but that remove 9:09:15 too.
I use auto-generate fields and DataBind on .cs file.
I tried Replace, but does not work:
e.Row.Cells[0].Text = e.Row.Cells[0].Text.Replace("0:00:00"," ");
How can I do that?
Gridview:
<asp:GridView ID="GridView4" runat="server" BackColor="White"
BorderColor="#999999" BorderStyle="None" BorderWidth="1px" CellPadding="3"
GridLines="Vertical" Width="98%"
Height="100%" onrowdatabound="GridView4_RowDataBound" RowStyle-HorizontalAlign="Center" >
<AlternatingRowStyle BackColor="#DCDCDC" />
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#0000A9" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#000065" />
</asp:GridView>
Code:
protected void Page_Load(object sender, EventArgs e)
{
try
{
OleDbConnection Connection6;
using (Connection6 = new OleDbConnection("Provider=MSDAORA.1;Data Source=XXXXXX:1521/orcl;Persist Security Info=True;Password=XXXXXXX;User ID=XXXXXX;"))
{
string sqlQuery = "select * from db.user";
using (OleDbDataAdapter cmd = new OleDbDataAdapter(sqlQuery, Connection6))
{
Connection6.Open();
DataTable dt = new DataTable();
cmd.Fill(dt);
GridView4.DataSource = dt;
GridView4.DataBind();
Connection6.Close();
}
}
}
catch (Exception)
{
}
}
You can use RowDataBound event, then label control using findcontrol
<asp:GridView ID="GridView1" AutoGenerateColumns="false" runat="server"
onrowdatabound="GridView1_RowDataBound">
<Columns>
<asp:TemplateField HeaderText="Date">
<ItemTemplate>
<asp:Label ID="lblDate" Text='<%# Eval("Date") %>' runat="server"></asp:Label>
.....
</Columns>
</asp:GridView>
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
var lbl = e.Row.FindControl("lblDate") as Label;
lbl.Text = lbl.Text.Replace("0:00:00", "");
}
}
If you don't have Template Field and using Bound Field then Use
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if (e.Row.RowType == DataControlRowType.DataRow)
{
if(e.Row.Cells[0].Text.Contains("0:00:00"))
e.Row.Cells[0].Text = e.Row.Cells[0].Text.Replace("0:00:00", "");
}
}

GridView Order of Operations

No error as such, but the way I have it setup, the GridView renders on Page_Load, it would seem that then the rest of operations that I have setup don't seem to fire.
Any ideas on the order? Been at it for hours now :(
<asp:GridView ID="GridView1" runat="server" ShowHeaderWhenEmpty="True" EmptyDataText="No Records Found" AutoGenerateColumns="False" DataSourceID="showOrders" OnSelectedIndexChanged="GridView1_SelectedIndexChanged" BackColor="#DEBA84" BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px" CellPadding="3" CellSpacing="2" Height="74px" Width="394px">
<Columns>
<asp:TemplateField HeaderText ="Pizza Type" SortExpression="pizza_id">
<EditItemTemplate>
<asp:TextBox ID="TextBox1" runat="server" Text='<%#Bind("pizza_id") %>'></asp:TextBox>
</EditItemTemplate>
<ItemTemplate>
<asp:Label ID="mylabel" runat="server" Text='<%#Bind("pizza_id") %>'></asp:Label>
</ItemTemplate>
</asp:TemplateField>
<asp:BoundField DataField="quantity" HeaderText="Quanity" SortExpression="quantity" />
<asp:BoundField DataField="pizza_size" HeaderText="Pizza Size" SortExpression="pizza_size" />
</Columns>
<FooterStyle BackColor="#F7DFB5" ForeColor="#8C4510" />
<HeaderStyle BackColor="#A55129" 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="showOrders" runat="server" ConnectionString="<%$ ConnectionStrings:Database %>"></asp:SqlDataSource>
Code Behind:
public partial class order_details : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["Database"].ConnectionString;
protected void Page_Load(object sender, EventArgs e)
{
string username = User.Identity.Name;
showOrders.SelectParameters.Add("username", username);
showOrders.SelectCommand = "SELECT [pizza_id], [quantity], [pizza_size] FROM [orders] WHERE ([username]=#username)";
}
protected void GridView1_Databound(object sender, EventArgs e)
{
foreach (GridViewRow row in GridView1.Rows)
{
row.Visible = row.RowIndex.Equals(1);
}
}
protected void GridView1_RowDataBound(object sender, GridViewRowEventArgs e)
{
if(e.Row.RowType == DataControlRowType.DataRow)
{
string value = e.Row.Cells[7].Text;
Label myLabel = (Label) e.Row.FindControl("myLabel");
if (value == "1")
{
myLabel.Text = "Big Cheese";
}
else if (value == "2")
{
myLabel.Text = "BBQ Beef";
}
else if (value == "3")
{
myLabel.Text = "Chicken and Pineapple";
}
else if (value == "4")
{
myLabel.Text = "Pepperoni Feast";
}
else if (value == "5")
{
myLabel.Text = "Vegetarian";
}
}
Any help would be appreciated.
Thanks Kindly.
You need to wire up the offending events:
<asp:GridView ID="GridView1" runat="server"
ShowHeaderWhenEmpty="True"
EmptyDataText="No Records Found"
AutoGenerateColumns="False"
DataSourceID="showOrders"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
BackColor="#DEBA84"
BorderColor="#DEBA84" BorderStyle="None" BorderWidth="1px"
CellPadding="3" CellSpacing="2"
Height="74px" Width="394px"
OnRowDataBound="GridView1_RowDataBound"
OnDataBound="GridView1_Databound">

Why did my gridview disappear after editing some gridviewroweditor

I'm trying to remove some of my columns in my gridview. Then this method was provided by someone which i tried
protected void yourGrid_RowCreated(object sender, GridViewRowEventArgs e)
{
e.Row.Cells[7].Visible = false;
e.Row.Cells[8].Visible = false;
e.Row.Cells[9].Visible = false;
}
And
GWCase.DataSource = ds;
GWCase.DataBind();
GWCase.Columns[7].Visible = false;
GWCase.Columns[8].Visible = false;
GWCase.Columns[9].Visible = false;
The binding of the gridview and the SQL server are done at the page load. This is how my pageload looks like
protected void Page_Load(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source = localhost; Initial Catalog = MajorProject; Integrated Security= SSPI";
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter da = new SqlDataAdapter("SELECT memberreportid, typeofcrime, crdatetime, address, detail, incidentdate, incidenttime, property, victim, suspect from memberreport", conn);
da.Fill(ds);
GWCase.DataSource = ds;
GWCase.DataBind();
conn.Close();
ddlpid1.Visible = false;
ddlpid2.Visible = false;
ddlpid3.Visible = false;
ddlpid4.Visible = false;
ddlpid5.Visible = false;
ddlpid6.Visible = false;
ddlpid7.Visible = false;
ddlpid8.Visible = false;
ddlpid9.Visible = false;
ddlpid10.Visible = false;
}
There i attempted to change my
EventArgs to GridViewRowEventArgs.
I ran the code and it didnt work, in fact my gridview disappear. I went to change it back to the original eventargs but the gridview still disappears.
This is the source code of my gridview
<asp:GridView ID="GWCase" runat="server" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" Width="100%" AutoGenerateSelectButton="True" OnSelectedIndexChanged="GWCase_SelectedIndexChanged">
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
</asp:GridView>
It seems like by changing the EventArgs causes the whole gridview to fail. I deleted my aspx pages and recreate and copy the same codes and the gridview still disappears.
If you want more control over which columns get rendered to the GridView, then you can use the Columns property of the GridView, like this:
<asp:GridView ID="GWCase" runat="server" BackColor="#CCCCCC" BorderColor="#999999" BorderStyle="Solid" BorderWidth="3px" CellPadding="4" CellSpacing="2" ForeColor="Black" Width="100%" AutoGenerateSelectButton="True" OnSelectedIndexChanged="GWCase_SelectedIndexChanged">
<FooterStyle BackColor="#CCCCCC" />
<HeaderStyle BackColor="Black" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#CCCCCC" ForeColor="Black" HorizontalAlign="Left" />
<RowStyle BackColor="White" />
<SelectedRowStyle BackColor="#000099" Font-Bold="True" ForeColor="White" />
<SortedAscendingCellStyle BackColor="#F1F1F1" />
<SortedAscendingHeaderStyle BackColor="#808080" />
<SortedDescendingCellStyle BackColor="#CAC9C9" />
<SortedDescendingHeaderStyle BackColor="#383838" />
<Columns>
<asp:BoundField datafield="CustomerID" headertext="Customer ID"/>
<asp:BoundField datafield="CompanyName" headertext="Company Name"/>
<asp:BoundField datafield="Address" headertext="Address"/>
<asp:BoundField datafield="City" headertext="City"/>
<asp:BoundField datafield="PostalCode" headertext="Postal Code"/>
<asp:BoundField datafield="Country" headertext="Country"/>
</Columns>
</asp:GridView>
Note: So instead of hiding columns by the GWCase.Columns[7].Visible = false; syntax, you just do not define that column to be bound to the GridView in the Columns section.

gridview problem

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!
Instead of the ExecuteReader() use ExecuteNonQuery(), that might do the trick.
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().

Categories

Resources