473,405 Members | 2,344 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

GridView Edit(Update)

Hi,

I have problem with my ASP.Net Code, I used GridView with Edit/Update on it. However the program seems not work correctly because it doesn't Update the OLD VALUE with the NEW VALUE.
Please help on this. thanks so much

<%@ 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;datab ase=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=ro ot;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>
Sep 3 '07 #1
4 2232
kenobewan
4,871 Expert 4TB
Do you refill the datagrid after updating?
Sep 3 '07 #2
Do you refill the datagrid after updating?
I used the following lines to refill the datagrid...

DataSet ds = new DataSet();
OdbcDataAdapter da = new OdbcDataAdapter("SELECT * FROM ttybroker",
@"dsn=MyOdbc;server=localhost;database=pres;pwd=ro ot;uid=root");
da.Fill(ds, "ttybroker");
DataView dv = ds.Tables[0].DefaultView;
Sep 4 '07 #3
sqlcon = new SqlConnection(strCon);
string sqlstr = "update 表 set 字段1='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',字段2='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',字段3='"

+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"+ GridView1.DataKeys [e.RowIndex].Value.ToString() + "'";
sqlcom=new SqlCommand(sqlstr,sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex = -1;
bind();
Sep 4 '07 #4
Hi,

Your code seems not clear to me. Could you send a clearer one... Thanks for your effort. Really appreaciate it... God Bless.

sqlcon = new SqlConnection(strCon);
string sqlstr = "update 表 set 字段1='" + ((TextBox)(GridView1.Rows[e.RowIndex].Cells[1].Controls[0])).Text.ToString().Trim() + "',字段2='"
+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[2].Controls[0])).Text.ToString().Trim() + "',字段3='"

+ ((TextBox)(GridView1.Rows[e.RowIndex].Cells[3].Controls[0])).Text.ToString().Trim() + "' where id='"+ GridView1.DataKeys [e.RowIndex].Value.ToString() + "'";
sqlcom=new SqlCommand(sqlstr,sqlcon);
sqlcon.Open();
sqlcom.ExecuteNonQuery();
sqlcon.Close();
GridView1.EditIndex = -1;
bind();
Sep 4 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: pmud | last post by:
Hi, I have an ASP.NET application using C# code. I am using a datagrid to display records from a database based on a user input, i.e a user enters a compnay name in text box & when he clicks a...
1
by: Jim McGivney | last post by:
In VS-05 on an aspx page I have a GridView with a select item column. When the select button is pressed a DetailsView control opens with the selected record displayed. The user can now edit the...
3
by: thebison | last post by:
Hi all, I hope someone can help with this relatively simple problem. I am building a timesheet application using ASP.NET C# with Visual Studio 2003.As it is only a protoype application, my...
0
by: et | last post by:
I set up a gridview to update according to a stored procedure. The parameters in the gridview <UpdateParametersare exactly the same as the parameters in the stored procedure, yet I receive the...
0
by: JayD | last post by:
I am using Visual Web Developer (vb.net) and have a gridview in a form. I can update records using standard EDIT/UPDATE buttons. However, there are two fields in every record (called ModifiedBy and...
0
by: troyblakely | last post by:
I have a gridview which is pulling data from a SqlDataSource, the select command queries a view and the update command is a stored procedure. I'm using a stored procedure because several tables...
1
by: pieandpeas | last post by:
Hi, i've been searching the internet for a good reference of how to add the update column functionality to a gridview, coding everything in the .vb behind the page stuff myself, e.g not using a...
0
by: Don | last post by:
I have a GridView inside an UpdatePanel. This GridView has a command field (edit, update, cancel). When I click edit I go into update mode, refreshing only the GridView, as you would expect. ...
11
by: SAL | last post by:
Hello, I have a Gridview control (.net 2.0) that I'm having trouble getting the Update button to fire any kind of event or preforming the update. The datatable is based on a join so I don't know...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.