I am using a datagrid in a webpage to update or delete data but get the above error.
here is the code of the datagrid to delete data:
(dgadvertise is the datagrid)
protected void dgadvetise_Delete(object source, DataGridCommandEventArgs e)
{
try
{
string AdName = cls.FetchSingle("Select AdName from AdDetails where AdID=" + dgadvetise.DataKeys[e.Item.ItemIndex], this.Page);
if (File.Exists(Server.MapPath("Ad_Picture/" + AdName + ".jpg")))
File.Delete(Server.MapPath("Ad_Picture/" + AdName + ".jpg"));
cls.DeleteData("ADetails", "Where AdID=" + dgadvetise.DataKeys[e.Item.ItemIndex], "Advertise Details Deleted", this.Page);
dgadvetise.EditItemIndex = -1;
BindGrid();
}
catch (Exception ex)
{
cls.MsgBox(ex.Message,this.Page);
}
}
Here is the app_Code/ClsCommon
public string FetchSingle(string sqlQuery, Page form)
{
string functionReturnValue;
functionReturnValue = "";
try
{
adp = new SqlDataAdapter(sqlQuery, conn);
tbl = new DataTable();
adp.Fill(tbl);
if (tbl.Rows.Count == 0)
{
return "";
}
functionReturnValue = tbl.Rows[0][0].ToString();
tbl.Dispose();
adp.Dispose();
}
catch (Exception ex)
{
MsgBox("Fetch Single Error: " + ex.Message, form);
return "";
}
return functionReturnValue;
}
public void DeleteData(string tblName, string searchCodn, string msg, Page form)
{
try
{
conn = new SqlConnection(ConfigurationManager.ConnectionStrings["nayumaConnectionString"].ToString());
conn.Open();
cmd = new SqlCommand("Delete " + tblName + " " + searchCodn, conn);
cmd.ExecuteNonQuery();
conn.Close();
if (msg != null)
MsgBox(msg, form);
}
catch (Exception ex)
{
MsgBox("Delete Data Error: " + ex.Message, form);
}
}
Anyone can help me to solve this problem.
Thanks in advance.