Click here to Skip to main content
15,867,141 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
Hi,
I am trying to update a gridview through RowUpdating event. I am using the following code to update gridview but it is new being updated. Please help me identify the issue with the code.
C#
protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
   {
     GridViewRow row = grdCountry.Rows[e.RowIndex];

     ds = ((DataSet)(Session["dsKey"]));
     grdCountry.DataSource = ds;
     grdCountry.DataBind();
   }
Posted
Updated 30-May-10 20:19pm
v2

No line in the code shows that you are trying to update a record.
 
Share this answer
 
In your code, you have not updated your dataset from the UI so how can you can you get updated value on ui.
Do something like this
protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
 {
   //Retrieve the table from the session object.
   DataTable dt = (DataTable)Session["dsKey"];
   //Update the values.
   GridViewRow row = grdCountrygrdCountry.Rows[e.RowIndex];
   dt.Rows[row.DataItemIndex]["Id"] = ((TextBox)(row.Cells[1].Controls[0])).Text;
   dt.Rows[row.DataItemIndex]["Description"] = ((TextBox)(row.Cells[2].Controls[0])).Text;
   dt.Rows[row.DataItemIndex]["IsComplete"] = ((CheckBox)(row.Cells[3].Controls[0])).Checked;
   //Reset the edit index.
   grdCountry.EditIndex = -1;
   //Bind data to the GridView control.
   grdCountry.DataSource = dt;
   grdCountry.DataBind();
 }
 
Share this answer
 
Comments
Sandeep Mewara 31-May-10 7:34am    
There is a question to your answer. This is to notify you of that.
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
try
{
int id = Convert.ToInt32(((Label)(GridView1.Rows[e.RowIndex].FindControl("lbl_AssetID"))).Text);
//TextBox txtId = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtAssteId"));
//TextBox txtItemid = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtItemId"));
TextBox txtName = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtAssetName"));
TextBox txtAssetData = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtAssetData"));
DropDownList ddlType = (DropDownList)(GridView1.Rows[e.RowIndex].FindControl("ddlAsetType"));
DropDownList ddlSubType = (DropDownList)(GridView1.Rows[e.RowIndex].FindControl("ddlSubType"));
TextBox txtAssetFilepath = (TextBox)(GridView1.Rows[e.RowIndex].FindControl("txtAssetFilePath"));
//Session["txtItem"] = txtItemid.Text;
Session["AssetId"] = id;
string sqlUpdate = "Update assets set AssetType='" + ddlType.SelectedItem.Text + "',AssetSubType='" + ddlSubType.SelectedItem.Text + "',AssetName='" + txtName.Text + "',AssetData='" + txtAssetData.Text + "',AssetFilePath='" + txtAssetFilepath.Text + "' where AssetId='" + id + "'";
if (myConnection.State == ConnectionState.Closed)
{
myConnection.Open();
}
//OdbcCommand myCommand = new OdbcCommand(sqlUpdate, myConnection);
command = new MySqlCommand(sqlUpdate, myConnection);
command.CommandType = CommandType.Text;
command.CommandText = sqlUpdate;
command.ExecuteNonQuery();
myConnection.Close();
lblMsg.Visible = true;
lblMsg.Text = "Asset Updated Successfully.";
BindGrid();
Response.Redirect("AssetViewer.aspx?Sucess=" + "message");
}
catch (Exception ex)
{
lblMsg.Visible = true;
lblMsg.Text = ex.Message;
}
}

C#
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        GridView1.EditIndex = -1;
        BindGrid();
    }
 
Share this answer
 
Comments
Sandeep Mewara 4-Jun-10 2:10am    
Reason for my vote of 1
What is this?
Arent you aware of Pre tags?
@Brij

I am using the same code as u have identified. But when i update the specific record of the row say 3 it is still giving the exception that "There is no row at position 3.
Here i am posting the code which i am using.

<pre lang="C++">public partial class _Default : System.Web.UI.Page
 {
   SqlConnection con = new SqlConnection("Data Source=TSARavi\\SQL2005,1435;Initial Catalog=TestDB;User ID=testdb;Password=testdb1");
   DataSet ds;

   SqlDataAdapter da;
   DataTable taskTable;
   protected void Page_Load(object sender, EventArgs e)
   {
     if(!IsPostBack)
     {

        taskTable = new DataTable("CompanyDetails");

      
       //Persist the table in the Session object.
       Session["TaskTable"] = taskTable;

       //Bind data to the GridView control.
       BindData();




     }
   }
   protected void BindData()
   {
     con.Open();
     string qry = "Select * from CompanyDetails";
     ds = new DataSet();
     da = new SqlDataAdapter(qry, con);
     da.Fill(ds);
     grdCountry.DataSource = ds;
     grdCountry.DataBind();
     con.Close();

   }

   protected void EditRecord(object sender, GridViewEditEventArgs e)
   {
     grdCountry.EditIndex = e.NewEditIndex;

     BindData();
   }
   protected void CancelRecord(object sender, GridViewCancelEditEventArgs e)
   {
     grdCountry.EditIndex = -1;
     BindData();
   }
   protected void UpdateRecord(object sender, GridViewUpdateEventArgs e)
   {
    DataTable dt = (DataTable)Session["TaskTable"];
     GridViewRow row = grdCountry.Rows[e.RowIndex];

     dt.Rows[row.DataItemIndex]["CompanyName"] = ((TextBox)row.Cells[1].Controls[0]).Text;
     dt.Rows[row.DataItemIndex]["ContactName"] = ((TextBox)row.Cells[2].Controls[0]).Text;
     dt.Rows[row.DataItemIndex]["ContactTitle"] = ((TextBox)(row.Cells[3].Controls[0])).Text;
     dt.Rows[row.DataItemIndex]["Country"] = ((TextBox)(row.Cells[4].Controls[0])).Text;
     dt.Rows[row.DataItemIndex]["Phone"] = ((TextBox)(row.Cells[5].Controls[0])).Text;
     dt.Rows[row.DataItemIndex]["Address"] = ((TextBox)(row.Cells[6].Controls[0])).Text;
     dt.Rows[row.DataItemIndex]["City"] = ((TextBox)(row.Cells[7].Controls[0])).Text;


     grdCountry.DataSource = dt;
     grdCountry.DataBind();
   }


   protected void DeleteRecord(object sender, GridViewDeleteEventArgs e)
   {
     string autoid = grdCountry.DataKeys[e.RowIndex].Value.ToString();
   }


 }


 
Share this answer
 
Comments
Sandeep Mewara 31-May-10 7:36am    
Use comment feature to reply back the answer of the person you want to discuss with.
Brij 31-May-10 9:09am    
I think there is a major flaw in your code.
First in pageload you need to create a column in the datatable like
taskTable.Columns.Add("CompanyName", typeof(string));
for all columns.
Another thing I observed that some time you are binding the data to the dataset that you got from DB and few times you are binding from session table.
I didn't get the exact requirement.
For reference on rowupdating, have a look on the link
http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.gridview.rowupdating.aspx

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900