AspdotnetCodes.com
Search
Articles
Books
Resources
Asp.Net News
Downloads
Free Tech Magazines
Asp.Net Web Hosting
Archives
Softwares
Newsletter
Suggest Us
Link to Us
Feeds Subscription
Articles
Questions & Answers
Tips & Tricks
 RowUpdating Event not Updating Old Value with New Value
Posted by arlene canlas on  9/3/2007 6:17:39 AM Category: Asp.Net 2.0
Add to my favorites
Email to friend
<%@ Page Language="C#" Debug="true" MasterPageFile="MasterPage.Master" Title="Untitled Page" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Odbc" %>

<script runat="server">

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

protected void NewButton_Click(object sender, EventArgs e)
{

}

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
string BrokerCode, BrokerName;

BrokerCode = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;
BrokerName = ((TextBox)GridView1.Rows[e.RowIndex].Cells[3].Controls[0]).Text;

OdbcConnection cnn = new OdbcConnection(@"dsn=MyOdbc;server=localhost;database=pres;pwd=root;uid=root;integrated security=true");

cnn.Open();
OdbcCommand cmd = new OdbcCommand("UPDATE ttybroker SET bname=@BrokerName WHERE bcode=@BrokerCode", cnn);

cmd.Parameters.Add(new OdbcParameter("@BrokerName",BrokerName));
cmd.Parameters.Add(new OdbcParameter("@BrokerCode",BrokerCode));

cmd.ExecuteNonQuery();
cnn.Close();

GridView1.EditIndex = -1;
BindGrid();
}

protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
GridView1.EditIndex = -1;
BindGrid();
}

private void BindGrid()
{
DataSet ds = new DataSet();
OdbcDataAdapter da = new OdbcDataAdapter("SELECT * FROM ttybroker", @"dsn=MyOdbc;server=localhost;database=pres;pwd=root;uid=root;integrated security=true");
da.Fill(ds, "ttybroker");
DataView dv = ds.Tables[0].DefaultView;

if (ViewState["sortexpression"] != null)
{
dv.Sort = ViewState["sortexpression"].ToString() + " " + ViewState["sortdirection"].ToString();
}

// GridView1.DataSource = ds.Tables[0].DefaultView;
GridView1.DataSource = dv;
GridView1.DataBind();
}

protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
GridView1.EditIndex = e.NewEditIndex;
BindGrid();
}

protected void GridView1_PageIndexChanging(object sender, GridViewPageEventArgs e)
{
GridView1.PageIndex = e.NewPageIndex;
BindGrid();
}

</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:Label ID="Label1" runat="server" Font-Bold="True" Font-Names="Arial" ForeColor="#0000FF"
Text="Broker Information"></asp:Label><br />
&nbsp;
<asp:GridView ID="GridView1" runat="server" AllowPaging="True" AllowSorting="True"
AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None"
BorderWidth="1px" CellPadding="3" DataKeyNames="bcode" GridLines="Vertical" AutoGenerateDeleteButton="true" OnRowUpdating="GridView1_RowUpdating" OnRowEditing="GridView1_RowEditing">
<FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
<Columns>
<asp:CommandField ShowEditButton="True" />
<asp:BoundField DataField="bcode" HeaderText="Code" />
<asp:BoundField DataField="bname" HeaderText="Name" />
</Columns>
<RowStyle BackColor="#EEEEEE" ForeColor="Black" />
<SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
<HeaderStyle BackColor="#000084" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="#DCDCDC" />
</asp:GridView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString2 %>"
ProviderName="<%$ ConnectionStrings:ConnectionString2.ProviderName %>"
SelectCommand="SELECT bcode, bname FROM ttybroker"
UpdateCommand="UPDATE ttybroker SET bname = @bname WHERE bcode = @original_bcode"
DeleteCommand="DELETE FROM ttybroker WHERE bcode = @original_bcode"
OldValuesParameterFormatString="original_{0}">
<UpdateParameters>
<asp:Parameter Name="bcode" Type="string" />
<asp:Parameter Name="bname" Type="string" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="@original_bcode" Type="string" />
</DeleteParameters>
</asp:SqlDataSource>
<br />
<asp:Button ID="NewButton" runat="server" Text="New" OnClick="NewButton_Click" /><br />
<asp:Label ID="Label4" runat="server" Font-Names="Arial" Font-Size="10pt" ForeColor="#0000FF"></asp:Label><br />
&nbsp;<br />

</asp:Content>

Post Your Reply Post New Question

 Viewer's Reply
Updating Row in GridView control
Answered By Moderator1 on 6/17/2010 4:21:53 AM
Hi,

I found the problem in the below line,

BrokerCode = ((TextBox)GridView1.Rows[e.RowIndex].Cells[2].Controls[0]).Text;

The GridView cells are Zero based index. so BrokerCode is in second column which you need to retrieve as below

BrokerCode = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].Controls[0]).Text;
 
Post Your Reply If you still have problem, post it again under this reply for better response.

 
Sponsored by
Many .NET teams are underperforming, is yours?
Build Your Own ASP.NET 3.5 Web Site Using C# & VB, 3rd Edition - Free 219 Page Preview!
Programmers Heaven C# School Book - Free 338 Page eBook