Roy Johnson Roy Johnson - 3 months ago 12
ASP.NET (C#) Question

Datatable not updating after delete

I am having trouble with my datatable, at the moment my datatable is populated with values from my database. When i upload a new file into the database, my datatable would automatically update and show what it has been updated with. But now i have a button which deletes a record from the database which is in correlation with the datatable that i have. The problem right now is that after i have deleted the record, the datatable is not refreshing and it still shows the record there. I have verified that the record has been deleted, its just that my datatable wont update.(It will only update after i refreshed the page). Below are my codes for the datatable, and delete button

Data Table:

<asp:GridView ID="FileTableView" CssClass="datagrid" HeaderStyle-CssClass="datagridHeader" RowStyle-CssClass="datagridRows" runat="server" AutoGenerateColumns="False" DataKeyNames="fileid, filename">
<Columns>
<asp:TemplateField HeaderText="Master Folder">
<ItemTemplate>
<asp:LinkButton ID="FileLinkButton" CommandName="ShowPopup" OnCommand="File_Command" CommandArgument='<%# Eval("fileid") %>' runat="server" Text='<%# Eval("filename") %>'></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>


Filling data into the datatable

DataTable dtFile;
protected void Page_Load(object sender, EventArgs e)
{

if (!IsPostBack)
{
// Fill up file and folder data on the display
FillDataFile();
}
}

private void FillDataFile()
{
dtFile = new DataTable();
SqlDataReader reader = MBFile.GetFileToDisplay(Context.User.Identity.Name);
dtFile.Load(reader);
if (dtFile.Rows.Count > 0)
{
FileTableView.DataSource = dtFile;
FileTableView.DataBind();
}
}


Delete button

protected void File_Command(object sender, CommandEventArgs e)
{
string command = e.CommandName;
MBFile file;

switch (command)
{
case "ShowPopup":
System.Diagnostics.Debug.WriteLine("Running");
long fileid = Convert.ToInt64(e.CommandArgument.ToString());
System.Diagnostics.Debug.WriteLine("FileID: " + fileid);
file = MBFile.RetrieveFile(Context.User.Identity.Name, fileid);
LblFileID.Text = fileid.ToString();
LblFileName.Text = file.fileName;
LblFileType.Text = file.fileType;
LblFileSize.Text = file.fileSize.ToString();

ScriptManager.RegisterStartupScript(this, this.GetType(), "myModal", "showPopup();", true);
break;

case "Delete":
System.Diagnostics.Debug.WriteLine("Deleting");
MBFile.DeleteFile(Context.User.Identity.Name, Convert.ToInt64(LblFileID.Text));
FillDataFile();
break;

case "Download":
System.Diagnostics.Debug.WriteLine("Downloading");
DownloadFileContent(Context.User.Identity.Name, Convert.ToInt64(LblFileID.Text));
Page.ClientScript.RegisterStartupScript(Page.GetType(), "Delete Status", "<script language='javascript'>alert('" + "File has been deleted" + "')</script>");
break;
}
}

Answer

Inside FillDataFile there is this condition

if (dtFile.Rows.Count > 0)
{   
     FileTableView.DataSource = dtFile;
     FileTableView.DataBind();
}

If you have deleted the only one record present in the table then this condition prevents the rebind of your grid. In any case this condition is wrong per se. If you don't have records in the table you should bind the result anyway. Remove the if condition.