C# Tutorials and offshore development in India
    Tutorials   Resources   Forum   Reviews   Communities   Interview   Jobs   Projects   Training   Your Ad Here    
Silverlight Games | Mentor | Code Converter | Articles | Code Factory | Computer Jokes | Members | Peer Appraisal | IT Companies | Bookmarks | Polls | Revenue Sharing | Lobby | Gift Shop |


Prizes & Awards
My Profile



Active Members
TodayLast 7 Days more...






Forums » .NET » ASP.NET »

Datagrid Update Command


Posted Date: 10 Oct 2008      Posted By: Cathrin      Member Level: Gold     Points: 1   Responses: 6



Hi friends,

I have a problem with datagrid update command.
When I update a row in datagrid it is getting updated in the database but is not visible in the Datagrid immediately.Only When I click the update command the new value is seen in the datagrid.
When I debug and return to the browser the new value is getting bound,there is no need of clicking the update command to see the new value in the grid.How could this happen?
this is my code :
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
binddata();
}
}
protected void DataGrid1_EditCommand(object source, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = e.Item.ItemIndex;
binddata();
}
protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
{
TextBox Id = (TextBox)e.Item.Cells[1].Controls[0];
TextBox State = (TextBox)e.Item.Cells[2].Controls[0];
TextBox places = (TextBox)e.Item.Cells[3].Controls[0];
con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=c:\\Documents and Settings\\User\\My Documents\\Test.mdb");
con.Open();
int idno;
idno = Convert.ToInt32(Id.Text);
String update = "Update State set Places = '" + places.Text + "' where Id ='" + idno + "'";
cmd = new OleDbCommand (update, con);
cmd.ExecuteNonQuery();
DataGrid1.EditItemIndex = -1;
binddata();
}
protected void binddata()
{
con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=c:\\Documents and Settings\\User\\My Documents\\Test.mdb");
con.Open();
String state = "Select * from State";
da = new OleDbDataAdapter(state, con);
ds = new DataSet();
da.Fill(ds);
DataGrid1.DataSource = ds;
DataGrid1.DataBind();
}
protected void DataGrid1_CancelCommand(object source, DataGridCommandEventArgs e)
{
DataGrid1.EditItemIndex = -1;
binddata();
}
}





Responses

Author: Abhay    10 Oct 2008Member Level: DiamondRating: 2 out of 52 out of 5     Points: 6

add a line code and try

protected void DataGrid1_UpdateCommand(object source, DataGridCommandEventArgs e)
{
TextBox Id = (TextBox)e.Item.Cells[1].Controls[0];
TextBox State = (TextBox)e.Item.Cells[2].Controls[0];
TextBox places = (TextBox)e.Item.Cells[3].Controls[0];
con = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data source=c:\\Documents and Settings\\User\\My Documents\\Test.mdb");
con.Open();
int idno;
idno = Convert.ToInt32(Id.Text);
String update = "Update State set Places = '" + places.Text + "' where Id ='" + idno + "'";
cmd = new OleDbCommand (update, con);
cmd.ExecuteNonQuery();
DataGrid1.EditItemIndex = -1;
DataGrid1.columns.clear();/// add this and try
binddata();
}

Thanks and Regards,
Abhay



Author: Anitha    10 Oct 2008Member Level: BronzeRating: 2 out of 52 out of 5     Points: 2

Hi,

did u give "datakeynames" property of the datagrid as primary key field of the database.

all yr code are correct.



Author: Palanivel    10 Oct 2008Member Level: SilverRating: 2 out of 52 out of 5     Points: 3

This is very basic thing. Datagrid displays static contents only. so whenever you update the database you need to bind the datagrid. so you just call binddata(); method to solve this problem. this may usefull for pagination also


Author: Palanivel    10 Oct 2008Member Level: SilverRating: 2 out of 52 out of 5     Points: 3

This is very basic thing. Datagrid displays static contents only. so whenever you update the database you need to bind the datagrid. so you just call binddata(); method to solve this problem. this may usefull for pagination also


Author: Cathrin    10 Oct 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 3

Hi Anitha,
I have not used datakeynames property..Will that be a cause of my problem..How should I use the datakeynames property?Could You give me an example?



Author: Cathrin    10 Oct 2008Member Level: GoldRating: 2 out of 52 out of 5     Points: 1

Hi Palani,
Thanks for replying .I have bound the datagrid after updating in the database na?



Post Reply

 This thread is locked for new responses. Please post your comments and questions as a separate thread.
If required, refer to the URL of this page in your new post.


Next : GridView
Previous : Domain Name
Return to Discussion Forum
Post New Message
Category: ASP.NET

Related Messages



dotNet Slackers

About Us    Contact Us    Privacy Policy    Terms Of Use