Click here to Skip to main content
15,888,816 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I need to delete the selected row where there is a hidden column activityID and taskID which I set visible to false because I need their value to delete it from the database.
I have to delete from database using QuestionNo , activityID and taskID .

ActivityID and TaskID is hidden , GridView actually looks like this :

QuestionNo,ActivtiyID,TaskID,QuestionContent , deletebutton

My aspx codes , i have converted the columns to templatefield from boundfield and i keep getting null values when trying to retrieve the values of the columns ( for example: QuestionNo , ActivityID and TaskID .What exactly is the correct way to do this? i am using Entitiy Framework to do this.

My Code looks like this :

C#
protected void Page_Load(object sender, EventArgs e)
       {
           if (!Page.IsPostBack)
           {
               BindQuestions();


           }
       }

       protected void BindQuestions()
       {
            gvQuestion.DataSource = daoQuestion.GetList();
            gvQuestion.DataBind();
       }

       protected void LinkButton1_Click(object sender, EventArgs e)
       {


       }

       protected void gvQuestion_RowDeleting1(object sender, GridViewDeleteEventArgs e)
       {
           gvQuestion.DeleteRow(e.RowIndex);

            Model.question del = new Model.question();

           int q = Convert.ToInt32(gvQuestion.Rows[e.RowIndex].Cells[0].ToString());
           int a = Convert.ToInt32(gvQuestion.Rows[e.RowIndex].Cells[1].ToString()); // Hidden Column
           int t = Convert.ToInt32(gvQuestion.Rows[e.RowIndex].Cells[2].ToString()); // Hidden Column

           del.QuestionNo = q;
           del.ActivityID = a;// Value of ActivityID column in GV
           del.TaskID = t; // Value of TaskID column in GV


           daoQuestion.Delete(del);
           daoQuestion.Save();
       }`
Posted
Updated 6-Jun-13 20:59pm
v2
Comments
jyan32193 7-Jun-13 1:44am    
i tried using datakeys as well , values are still null , i already converted boundfield to templatefield
Rabbil 7-Jun-13 1:57am    
Are you getting the respective values on the variables ??
jyan32193 7-Jun-13 2:07am    
nope i am not getting any values , they all returns me 0 value . am i retrieving correctly?

For deletion operation using Ado.net Entity framework...
Try using..
C#
daoQuestion.Attach(del);///Firstly, Attach the entity object to context
DeleteObject(del); ///specifies the object to delete
SaveChanges();
 
Share this answer
 
Comments
jyan32193 7-Jun-13 2:08am    
no such definition for attach and , the problem is i need to retrieve the values first in order to delete them based on their values ID .. but the values are returning me 0
Rabbil 7-Jun-13 2:27am    
In what controls are you binding the data in gridview ?
You can try using
int q = ((yourControlnametoCast)GVOptions.Rows[0].FindControl("controlNameOfQinYourGrid")).Text.Trim(); ///this will find the value from the respective control of your grid
after above line
del.QuestionNo = q; ///now check once again whether u are getting the value or not.... ?
jyan32193 7-Jun-13 2:37am    
sorry i am new to c# , i didn't use any controls to bind data , i populate the grid view using entity data source .
Rabbil 7-Jun-13 2:51am    
Then it will return null value only....
Better,you can refer here for you overall requirement.. http://techbrij.com/insert-update-delete-gridview-entity-framework
jyan32193 7-Jun-13 3:01am    
i have updated the codes above if u're referring to how i get my gridview data , duno if its wad u want to see
You can use RowCommand event for deleting row from gridview
use as

ASP.NET
<asp:LinkButton ID="l1" runat="server" Text="Remove" %>'  CommandName="Remove"  CommandArgument='<%#Eval("id") %>'></asp:LinkButton>


and on rowcommand event
C#
void GVDetail_RowCommand_RowCommand(Object sender, GridViewCommandEventArgs e)
{
if(e.CommandName=="Remove")
{
GridViewRow row = (GridViewRow)(((LinkButton)e.CommandSource).NamingContainer);
            int index = row.RowIndex;
            string id=row.Cells[0].Text;//this will retrive column 1 value     
//var id = Int32.Parse(e.CommandArgument);
     GVDetail.DeleteRow(int.Parse(id));
GVDetail.DataBind();


 }
}
 
Share this answer
 
v2
Comments
jyan32193 7-Jun-13 2:44am    
hi i have tried this , id returns null
jyan32193 7-Jun-13 2:44am    
the problem is retrieving the columns values .. keeps giving me null
uspatel 7-Jun-13 2:50am    
if you are taking unqid in your grid view then you can try
GridViewRow row = (GridViewRow)(((Button)e.CommandSource).NamingContainer);
int index = row.RowIndex;
string id=row.Cells[0].Text;//this will retrive column 1 value
uspatel 7-Jun-13 2:52am    
try updated 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