Hi user93,
I have modified your code. So you need to change as per your logic and pass proper parameter and need to change the select statement if required.
C#
protected void btnadd_Click(object sender, EventArgs e)
{
// If file present in FileUpload control then below code executes.
for (int i = 0; i < Request.Files.Count; i++)
{
HttpPostedFile postedFile = Request.Files[i];
if (postedFile.ContentLength > 0)
{
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(postedFile.FileName);
string fileExtension = Path.GetExtension(postedFile.FileName);
if (fileExtension.ToUpper() == ".MP3")
{
bool flag1 = CheckFileExist(Session["sid"].ToString(), fileNameWithoutExtension + ".mp3", Session["type"].ToString(), Session["phname"].ToString(), Docname, TFiName, DrfName, true, Type, DrftFName);
// File[i] not exist then insert into database with details.
if (!flag1)
{
lblerrmsg.Visible = false;
lblfname.Text = "";
lbldocname.Text = "";
pnl1.Visible = true;
btnupdate.Enabled = false;
btnadd.Enabled = true;
string Subjects = "select ID, Subject from Subjectsstu where Module = '" + null + "'";
BindDropDownList(ddlsubjname, Subjects, "Subject", "ID", "--Select--");
BindGrid();
Clear();
// Insert code goes here.
}
else
{
lblerrmsg.Visible = true;
lblerrmsg.Text = fileNameWithoutExtension + ".mp3 Already Exist ...!";
}
}
}
}
}
private bool CheckFileExist(string SName, string FName, string Stype, string PName, string DName, string TFName, string DraftFileName, bool HasFile, string Type, string DraftName)
{
bool flag1 = false;
string qry = string.Empty;
if (Type.ToUpper() == "ADD")
{
if (Stype.ToUpper() == "DRAFT")
{
qry = "select ID,SubjectID,auditoName from Subjectstu where (SubjectID='" + SName + "' and auditoName='" + FName + "' and AuditoType='" + Stype + "' and Physician='" + PName + "') or (BDocName='" + DName + "') or (DDocName='" + DraftName + "') ";
}
else
{
qry = "select ID,SubjectID,auditoName from Subjectstu where (SubjectID='" + SName + "' and auditoName='" + FName + "' and AuditoType='" + Stype + "' and Physician='" + PName + "') or (BDocName='" + DName + "')";
}
}
else
{
if (Stype.ToUpper() == "DRAFT")
{
qry = "select ID,SubjectID,auditoName from Subjectstu where (BDocName = '" + DocName + "' and ID <> " + Session["ID"] + ") or (DraftDocumentName = '" + DName + "' and ID <> " + Session["ID"] + ") or (SubjectID='" + SName + "' and auditoName='" + FName + "' and AuditoType='" + Stype + "' and Physician='" + PName + "' and ID <> " + Session["ID"] + ")";
}
else
{
qry = "select ID,SubjectID,auditoName from Subjectstu where (BDocName = '" + DocName + "' and ID <> " + Session["ID"] + ") or (SubjectID='" + SubjName + "' and auditoName='" + FName + "' and AuditoType='" + Stype + "' and Physician='" + PName + "' and ID <> " + Session["ID"] + ")";
}
}
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings[1].ConnectionString);
SqlCommand cmd = new SqlCommand(qry, con);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
// File exist
flag1 = true;
}
else
{
// File not exist
flag1 = false;
}
return flag1;
}