Quantcast
Channel: Visual Studio and Visual Web Developer Express
Viewing all articles
Browse latest Browse all 3509

how to delete records using delete link button in a grid view that delete from database as well

$
0
0

This is how my gridview looks like:

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 , it actually looks like this :

QuestionNo,ActivtiyID,TaskID,QuestionContent then delete.

So here is my code:

protected void gvQuestion_RowCommand(object sender, GridViewCommandEventArgs e)
{



if (e.CommandName == "delete")
{

/* int aid = Convert.ToInt32(dropListActivity.SelectedItem.Value);
int tid = Convert.ToInt32(dropListTask.SelectedItem.Value);
int qno = Convert.ToInt32(gvQuestion.SelectedRow.Cells[0].Text); */
Model.question del = new Model.question();

int a = Convert.ToInt32(gvQuestion.SelectedRow.Cells[1].Text);
int t = Convert.ToInt32(gvQuestion.SelectedRow.Cells[2].Text);


del.ActivityID = a;// Value of ActivityID column in GV
del.TaskID = t; // Value of TaskID column in GV
del.QuestionNo = Convert.ToInt32(gvQuestion.SelectedRow.Cells[0].Text);

daoQuestion.Delete(del);

}

daoQuestion.Save();
}

protected void gvQuestion_RowDeleted(object sender, GridViewDeletedEventArgs e)
{

}

protected void LinkButton1_Click(object sender, EventArgs e)
{



}

protected void gvQuestion_RowDeleting(object sender, GridViewDeleteEventArgs e)
{

}

I followed the guide here : http://www.codeproject.com/Articles/111037/Delete-Data-in-GridView-Using-Template-Button

But I am using the entity framework to delete, however I get the error of System.FormatException: Input string was not in a correct format.`, and the error lies in the delete statement. I searched for the error, and it says it might be a null value. How do I solve this problem? and i debugged for check the values , it appears as Null , am i getting the value correctly? i need to delete the selected row records from database as well.

Btw , i populated this grid view from Database (using EF) as well.


Here is my aspx.cs:

<asp:GridView ID="gvQuestion" runat="server"
AutoGenerateColumns="False"
onselectedindexchanged="gvQuestion_SelectedIndexChanged"
onrowcommand="gvQuestion_RowCommand" onrowdeleted="gvQuestion_RowDeleted"
DataKeyNames="ActivityID,TaskID" >
<Columns>
<asp:BoundField DataField="QuestionNo" HeaderText="QuestionNo"
InsertVisible="False" ReadOnly="True" SortExpression="QuestionNo" />
<asp:BoundField DataField="ActivityID" HeaderText="ActivityID"
SortExpression="ActivityID" Visible="False" />
<asp:BoundField DataField="TaskID" HeaderText="TaskID"
SortExpression="TaskID" Visible="False" />
<asp:BoundField DataField="joined" HeaderText="QuestionContent"
SortExpression="joined" >
<ControlStyle Width="150px" />
</asp:BoundField>
<asp:TemplateField>
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server" CommandName="Delete"
onclick="LinkButton1_Click">Delete</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>

i also tried using DataKeys but to no avail. Help is needed , thanks.

UPDATED :
I TRIED:

protected void gvQuestion_RowCommand(object sender, GridViewCommandEventArgs e)
{
if (e.CommandName == "Delete")
{
int index = Convert.ToInt32(e.CommandArgument);

GridViewRow row = gvQuestion.Rows[index];

Response.Write(row.Cells[1].Text);
}

Index gives null value ... this problem have been bothering me for days . What actually goes wrong here? ..


Viewing all articles
Browse latest Browse all 3509

Trending Articles