Click here to Skip to main content
15,907,913 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
protected void DataList1_UpdateCommand(object source, DataListCommandEventArgs e)
{
    int Id;
    string name = string.Empty;
    string details = string.Empty;
    string website = string.Empty;            
    string address = string.Empty;
    string TextImg = string.Empty;
    //FileUpload imagepath=FileUpload.ReferenceEquals;
    SqlCommand cmd = new SqlCommand();


    try
    {
        name = ((TextBox)(e.Item.FindControl("txtEditName"))).Text;
        details = ((TextBox)(e.Item.FindControl("txtEditDetails"))).Text;
        address = ((TextBox)(e.Item.FindControl("txtEditaddress"))).Text;
        website = ((TextBox)(e.Item.FindControl("txtEditWebsite"))).Text;
        TextImg = ((TextBox)(e.Item.FindControl("txtEditImgpath"))).Text;
        
        //areaid = ((TextBox)(e.Item.FindControl("txtEditareaid"))).Text;
        FileUpload imagepath = ((FileUpload)(e.Item.FindControl("FileUploadImg")));

        
        Id = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex].ToString());
        con.Open();
        cmd = new SqlCommand("Team_SocialActivities_Edit", con);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.Parameters.AddWithValue("@Id", Id);
        cmd.Parameters.AddWithValue("@Name", name);
        cmd.Parameters.AddWithValue("@Details", details);
        cmd.Parameters.AddWithValue("@Address", address);
        cmd.Parameters.AddWithValue("@Website", website);
        
        //cmd.Parameters.AddWithValue("@Area_ID", areaid);
        Guid FileName = Guid.NewGuid();
        if (imagepath.FileName != "")
        {
            imagepath.SaveAs(Server.MapPath("~/Images/" + FileName + ".png"));
            cmd.Parameters.AddWithValue("@ImagePath", "~/Images/" + FileName + ".png");
        }
        else
        {
            cmd.Parameters.AddWithValue("@ImagePath", TextImg);
        }
        cmd.ExecuteNonQuery();
        DataList1.EditItemIndex = -1;
        BindData();
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Book record has been updated successfully');", true);
    }
    catch (Exception ex)
    {
        ScriptManager.RegisterStartupScript(this, this.GetType(), "Message", "alert('Oops!! Error occured : " + ex.Message.ToString() + "');", true);
    }
Posted
v2
Comments
Which line gives you this exception?
Member 11177087 2-Jan-15 2:41am    
Id = Convert.ToInt32(DataList1.DataKeys[e.Item.ItemIndex].ToString());

Here came the error please give me suggestion
Have you declared the DataKey in DataList?
Arjsrya 2-Jan-15 2:54am    
Did you check "e.item.ItemIndex" return anything?

Just check if it is null or not then try
Member 11177087 3-Jan-15 0:37am    
"e.item.ItemIndex" it is return null

1 solution

Most likely, you have not declared the DataKey in DataList Markup.

That's why it could not able to find the Key in that index.
 
Share this answer
 

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