Posted: 6/9/2011
i am a newbiest to c#web service
i have a web service provided by web site A, then web site B consume it
I am able to bind the data into the grid view, however, i am not able to update and i try and result in error...the error is
please help me see what wrong with my code
thanks in advance
this is my code
protected void Page_Load(object sender, EventArgs e) { if (!IsPostBack) { BindNominal(); } } protected void BindRoll() { WSRoll.WSUpdateRoll roll = new WSRoll.WSUpdateRoll(); GVRoll.DataSource = roll.getRoll(); GVRoll.DataBind(); } protected void GVRoll_RowEditing1(object sender, GridViewEditEventArgs e) { GVRoll.EditIndex = e.NewEditIndex; BindRoll(); } protected void GVRoll_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e) { GVRoll.EditIndex = -1; BindRoll(); } protected void GVRoll_RowUpdating(object sender, GridViewUpdateEventArgs e) { GridViewRow row = (GridViewRow)GVRoll.Rows[e.RowIndex]; int Roll_ID = Int32.Parse(GVRoll.DataKeys[e.RowIndex].Values.ToString()); TextBox TextBox1 = (TextBox)row.FindControl("TextBox1"); int status = Convert.ToInt32(GVRoll.DataKeys[e.RowIndex].Values[2); WSRoll.WSUpdateRoll rollup = new WSRoll.WSUpdateRoll(); GVRoll.DataSource = rollup.updateRoll(Roll_ID, TextBox1.Text,status); } }
Hi,
It looks as though at the time your are trying to retrieve the data, your GridView has no Rows or Cells. Check the Rows.Count and Rows(0).Cells.Count properties to check which one is failing.
Please make sure before this:
int status = Convert.ToInt32(GVRoll.DataKeys[e.RowIndex].Values[2);
When the Header and Footer rows are created, the row index is -1.
You can check for Data rows like this:
If (e.Row.RowType = DataControlRowType.DataRow) Then {some logic}
If still doesn't work, please post where you put your code and your aspx.
Regards,
Gjorgji Dimitrov
i am able to retrieve data , NOT able to update in the GRIDVIEW
user361145 said: i am able to retrieve data , NOT able to update in the GRIDVIEW
Hello there...
I'm suspecting on few reasons why you get this, before having more concrete answer, can you please elaborate more on the following two questions:
1. In which line the code breaks?2. Can we see where and how you bind the gridview with data?3. Can you show us the code in the updateRoll method or at least how does it works?
Thanks,Hajan
Hi again,
The datakeys collection itself may not be null, but it can contain zero elements. In this example, your grid doesn't set the DataKeyNames property, so this grid isn't tracking any datakeys (the collection will be empty). That is probably why you are getting an index error.
You should set the DataKeyNames property. In your code you also need to check to make sure the collection contains elements.
Use .cells[n].controls[n] to get a reference to the TextBoxes, FindControl() for example:
TextBox textBox = GridView1.Rows[e.RowIndex].Cells[2].Controls[0] as TextBox;
you'll need to change the cell index accordingly (index starts from 0), then use the .Text property to get the values... there is an example on the following link http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowupdating.aspx.