I Have a code in my form to upload supporting document.
what I am doing I am uploading the document and saving them in a database table and displaying back in the form as grid. now my issue is I m not able to delete documents from the grid when I click on delete button
below is my code for uploading the document
viewing the document in grid and deleting the document.
private void BindGrid_AR()
{
using (SqlConnection con = new SqlConnection(getconn()))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select Sno, RequestID, FileName from test where RequestStatus = 'xx' and RequestID = " + RID;
cmd.Connection = con;
con.Open();
gridView_AR.DataSource = cmd.ExecuteReader();
gridView_AR.DataBind();
con.Close();
}
}
}
protected void UploadDoc_AR(object sender, EventArgs e)
{
string filename = Path.GetFileName(FileUpload_AR.PostedFile.FileName);
using (Stream fs = FileUpload_AR.PostedFile.InputStream)
{
using (BinaryReader br = new BinaryReader(fs))
{
byte[] bytes = br.ReadBytes((Int32)fs.Length);
using (SqlConnection con = new SqlConnection(getconn()))
{
string query = "insert into test values (@RequestID, @FiletName, @ContentData, @CreatedBy, @CreatedDate, @ModifiedDate, @RequestStatus)";
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Connection = con;
cmd.Parameters.AddWithValue("@RequestID", RID);
cmd.Parameters.AddWithValue("@FiletName", filename);
cmd.Parameters.AddWithValue("@ContentData", bytes);
cmd.Parameters.AddWithValue("@CreatedBy", SPContext.Current.Web.CurrentUser.Email);
cmd.Parameters.AddWithValue("@CreatedDate", DateTime.Now);
cmd.Parameters.AddWithValue("@ModifiedDate", DateTime.Now);
cmd.Parameters.AddWithValue("@RequestStatus", "xx");
con.Open();
cmd.ExecuteNonQuery();
con.Close();
BindGrid_AR();
}
}
}
}
}
protected void DownloadFile_AR(object sender, EventArgs e)
{
int File_id = int.Parse((sender as LinkButton).CommandArgument);
byte[] bytes;
string fileName;
using (SqlConnection con = new SqlConnection(getconn()))
{
using (SqlCommand cmd = new SqlCommand())
{
cmd.CommandText = "select FileName, ContentData from test where RequestStatus = 'XX' and Sno=@SNId";
cmd.Parameters.AddWithValue("@SNId", File_id);
cmd.Connection = con;
con.Open();
using (SqlDataReader sdr = cmd.ExecuteReader())
{
sdr.Read();
bytes = (byte[])sdr["ContentData"];
fileName = sdr["FileName"].ToString();
}
con.Close();
}
}
HttpContext.Current.Response.Clear();
HttpContext.Current.Response.Buffer = true;
HttpContext.Current.Response.Charset = "";
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
// HttpContext.Current.Response.ContentType = contentType;
HttpContext.Current.Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName);
HttpContext.Current.Response.BinaryWrite(bytes);
HttpContext.Current.Response.Flush();
HttpContext.Current.Response.End();
}
protected void gridView_AR_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
GridViewRow row = (GridViewRow)gridView_AR.Rows[e.RowIndex];
Label lbldeleteid = (Label)row.FindControl("lblID");
using (SqlConnection con1 = new SqlConnection(getconn()))
{
con1.Open();
SqlCommand cmd = new SqlCommand("delete FROM test where Sno='" + Convert.ToInt32(gridView_AR.DataKeys[e.RowIndex].Value.ToString()) + "'", con1);
cmd.ExecuteNonQuery();
con1.Close();
}
BindGrid_AR();
}
My issue is I am not able to delete the supporting document from the grid not able to get the sno of the row item which I want to delete.
Second part I want to do is to place a server side validation for file upload control that if the user didnt upload any file he can not go forward I tried the validator control but that is only check on the client side means even I have some doucment in the database for that particular request it will always find blank and show message that please upload your file.
I want to check from the server side if there is no document uploaded for that request it will give message but if there are some document in the grid it should submit the form.
Thanks